Question

I am trying to write text to my txt file. After the first write the application crash with error

Cannot write to a closed TextWriter

My list contains links that the browser opens and I want to save all of them in txt file (like a log).

My code:

FileStream fs = new FileStream(
                    "c:\\linksLog.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);

for (int i = 0; i < linksList.Count; i++)
{
    try
    {
        System.Diagnostics.Process.Start(browserType, linksList[i]);
    }
    catch (Exception) { }

    using (sw)
    {
        sw.WriteLine(linksList[i]);
        sw.Close();
    }

    Thread.Sleep((int)delayTime);

    if (!cbNewtab.Checked)
    {
        try
        {
            foreach (Process process in Process.GetProcesses())
            {
                if (process.ProcessName == getProcesses)
                {
                    process.Kill();
                }
            }
        }
        catch (Exception) { }
    }
}
Was it helpful?

Solution

You're in a for loop, but you close and dispose of your StreamWriter on the first iteration:

using (sw)
{
    sw.WriteLine(linksList[i]);
    sw.Close();
}

Instead, remove that block, and wrap everything in one using block:

using (var sw = new StreamWriter(@"C:\linksLog.txt", true)) {
    foreach (var link in linksList) {
        try {
            Process.Start(browserType, list);                        
        } catch (Exception) {}

        sw.WriteLine(link);

        Thread.Sleep((int)delayTime);

        if (!cbNewtab.Checked) {
            var processes = Process.GetProcessesByName(getProcesses);

            foreach (var process in processes) {
                try {
                    process.Kill();
                } catch (Exception) {}
            }
        }
    }
}

OTHER TIPS

The line

using (sw)

closes/disposes your StreamWriter.

Since you are looping, you dispose an already disposed StreamWriter.

Better to close the StreamWriter outside the loop, after all write operations are finished.

In addition, catching exceptions and ignoring the caught exception is almost always a bad idea. If you can't handle an exception, do not catch it.

The problem is that you are closing you Stream in the loop, should done only after...

FileStream fs = new FileStream("c:\\linksLog.txt", FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);

    for (int i = 0; i < linksList.Count; i++)
    {
        try
        {
            System.Diagnostics.Process.Start(browserType, linksList[i]);                        
        }
        catch (Exception)
        {

        }
        // Removed the using blocks that closes the stream and placed at the end of loop
        sw.WriteLine(linksList[i]);

        Thread.Sleep((int)delayTime);

        if (!cbNewtab.Checked)
        {
            try
            {
                foreach (Process process in Process.GetProcesses())
                {
                    if (process.ProcessName == getProcesses)
                    {
                        process.Kill();
                    }
                }
            }
            catch (Exception)
            { }
        }
    }

    sw.Close();

That's because you are, indeed, closing your stream in the middle of the loop. You have the using (sw) block in the middle, which will work fine in the first run through the for loop, and then crash. To fix it, just drop the sw.Close() call, and move the using to be outside the for loop:

Dont write the sw.Close() in your code because if the file is closed the code cannot read the file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top