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

时间:2013-05-21 06:26:21

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

首先,我确保妥善处理和关闭所有内容(包括读者和作者)。我正在做的方法是我使用using语句;在此之前,我手动使用了作者和读者提供的处理和关闭方法。这两种方法都无法解决问题。 其次,我也怀疑这一点,我正在处理很多文件,包括阅读和写作。此外,每次运行程序时,程序都会深入到应该处理的文件列表中(即,我有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());
        }
    }

1 个答案:

答案 0 :(得分:2)

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;
}