C# - StreamReader/StreamWriter - 另一个进程使用的文件

标签 c# file-io io iostream read-write

首先,我确保正确处理并关闭所有内容(读取器和写入器)。我正在做的方法是使用 using 语句;在此之前,我手动使用了作者和读者提供的 dispose 和 close 方法。这两种方法都不能解决问题。 其次,也是我怀疑的一点,我正在处理大量的读取和写入文件。此外,每次运行该程序时,该程序都会更深入地运行到应该处理的文件列表中(即,我有 10 个文件,第一次运行该程序时,它将处理前 2 个文件,然后抛出错误;第二次运行该程序时,它将在抛出错误之前处理前 5 个错误,依此类推)。 那么我该如何解决这个问题/错误呢? 提前致谢!

编辑 - 附加代码

    public static void FileProcessing(string initPath, string targetPath, string startLine)
    {
        string line = null;
        string content = null;
        string nl = "\r\n";
        using (StreamReader reader = new StreamReader(initPath))
        {
            while (!reader.ReadLine().Contains(startLine)) ;

            while (!reader.EndOfStream)
            {
                line = reader.ReadLine();
                // Ignore empty line.
                if (line.Length > 0)
                {
                    if (!line.Contains("<div"))
                        content += line += nl;
                    else
                        break;
                }
            }
        }

        using (StreamWriter writer = new StreamWriter(targetPath))
        {
            writer.Write(content.Trim());
        }
    }

    /// <summary>
    /// Create a temporary text file that correspond to the calendar department page
    /// and return the path.
    /// </summary>
    public static string CreateFile(string path, string title)
    {
        string npath = path + title + ".txt";
        if (!File.Exists(npath))
            File.CreateText(npath);
        return npath;
    }

    /// <summary>
    /// Return the title of the page.
    /// </summary>
    /// <param name="path"></param>
    public static string GetTitle(string path)
    {
        string line;
        using (StreamReader reader = new StreamReader(path))
        {
            for (int i = 0; i < 31; ++i)
            {
                line = reader.ReadLine();
                // Ignore empty line.
                if (line.Length > 0)
                {
                    if (line.Contains("<h1>"))
                    {
                        return line.Slice(4, -5);
                    }
                }
            }
            return "null";
        }
    }


    /// <summary>
    /// Get content from temp path and
    /// output the processed text to destination path.
    /// </summary>
    /// <param name="tempPath"></param>
    /// <param name="title"></param>
    public static void WriteProcessedText(string tempPath, string targetPath)
    {
        string content = null;
        using (StreamReader reader = new StreamReader(tempPath))
        {
            string nl = "\r\n";
            string line = null;
            while (!reader.EndOfStream)
            {
                line = HtmlRemoval.StripTagsRegex(reader.ReadLine()) + nl;
                if (line.Contains("&nbsp;"))
                    line = line.Replace("&nbsp;", " ");
                content += line;
            }
        }

        using (StreamWriter writer = new StreamWriter(targetPath))
        {
            writer.Write(content.Trim());
        }
    }

最佳答案

File.CreateText(npath) 不仅在磁盘上创建文件名,而且还打开它。因此,要么在 CreateFile 方法中关闭它,要么将其更改为返回流。

public static string CreateFile(string path, string title)
{
    string npath = path + title + ".txt";
    if (!File.Exists(npath))
        File.CreateText(npath); //<-- File is not closed !!!
    return npath;
}

关于C# - StreamReader/StreamWriter - 另一个进程使用的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16663470/

相关文章:

c# - 用于提取方括号之间的值的正则表达式

R shiny file输入大文件

c - 纯c中的文本文件到字符串数组?

Java将新文件的数据附加到旧文件

c# - 在 Entity Framework 中区分 MySQL 和 SQL Server

c# - 如何获取 Yammer 中特定组的待定用户列表

c - C 中带有 termios : unreliable output capitalization 的串行 I/O

Python I/O : How to use the wsgi. 来自 io 模块的输入流

c# - 使用动态类型调用泛型类的静态方法c#

将文本文件中的小写字符转换为大写字母,反之亦然