Question

First off, I made sure that I dispose and close everything (both reader and writer) properly. The method I'm doing is that I'm using the using statement; and before this, I manually used the dispose and close method provided by both the writer and reader. Both of these methods won't solve the problem. Secondly, also the point that I'm suspecting, I'm processing a lot of files both reading and writing. Further, every time I run the program, the program will run deeper into the list of the files that are supposed to be processed (ie, I have 10 files, the first time I run the program it will process the first 2 before throwing the error; the second time I run the program it will process the first 5 before throwing the error and so on). So how am I supposed fix this problem/bug? Thanks in advance!

Edit - Code attached

    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());
        }
    }
Was it helpful?

Solution

File.CreateText(npath) not only creates the filename on disk but also opens it. So either close it in your CreateFile method or change it to return a stream.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top