File Writing problem

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();
}
}
}
}
 
T

Truong Hong Thi

There should be no problem with the snippet. Could you post the real
code that cause the problem?
 
L

Lloyd Dupont

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>
 
T

Truong Hong Thi

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?
 
L

Lloyd Dupont

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 ;-)
 
Y

Yuan Ren[MSFT]

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
 
G

Guest

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.
 
Y

Yuan Ren[MSFT]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top