File Writing problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Using the following code, I get a System.ObjectdisposedException. The message
is "Cannot write to a closed TextWriter."

What is wrong?

using System;
using System.IO;

namespace Test {
class Test {
public static void Main() {
using (StreamWriter sw = new StreamWriter("c:\\TestFile.txt")) {
sw.WriteLine("Testing.");
sw.Flush();
}
}
}
}
 
There should be no problem with the snippet. Could you post the real
code that cause the problem?
 
probably you forgot the curly brace after using() in your real code

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
 
In that case, sw.Flush would raise compilation error since sw is
out-of-scope. Maybe sw is actually declared somewhere else before
entering using block?
 
In that case, sw.Flush would raise compilation error since sw is
out-of-scope. Maybe sw is actually declared somewhere else before
entering using block?
yeah, right!
back to your suggestio to post the real code then ;-)
 
Hi,

Welcome to MSDN newsgroup!

In this scenario, I have performed your code and it seems work well. I
wonder whether the problem always is encountered when running the
application.

Otherwise, I suggest you run the application on other machines and find
whether the problem still occurs.

Thanks for your understanding!

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
 
This is the exact code I compiled and run.

When I run it in the debugger, it fails on the line
sw.WriteLine("Testing."). When I run it by double-clicking on the exe file
(in ...\bin\Debug), it works correctly without throwing an exception.

OS is Windows 2000 with Service Pack 4 and Dot Net Framework 1.1 SP1. Dev
environment is Visual Studio 2003 Version 7.1.3088.

Dot Net frameworks 1.0 and 2.0 are also installed on the system.
 
Hi,

Thanks for your reply!
¡°When I run it in the debugger, it fails on the line
sw.WriteLine("Testing.")¡±
Does that mean you are debugging the current project and then the error is
occurring? If this is the case, I think the current issue is caused by the
environment. So I suggest you perform the test in another machine and find
out whether the error still occurs each time.

Regards,

Yuan Ren [MSFT]
Microsoft Online Support
 
Back
Top