Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Problem with StreamWriter

greetings and salutations,

I have attempted to use the StreamWriter class to writer text to files in a variety of encodings and I have followed your advice on closing the stream inside the finally block in a try/catch. However I experience a problem when I attempt to write a file to a disk that it full. First I recieve a 'Disk is full' exception, however, the problem begins when I try to close the stream, the stream attempts to flush it's contents and another 'Disk is full' exception is thrown. After the exception is handled if I attempt to access the same file an exception is thrown stating that the file is in use by another program.

Am I using the StreamWriter class incorrectly or have I discovered a serious flaw in the .NET framework, can you help me?

Here is the source code that I am using:

[CODE]StreamWriter writer = null;

try
{
writer = new StreamWriter("A:\monkey.txt", false, Encoding.UTF8);
writer.Write(txtBox.Text);
}
catch(IOException ioe)
{
MessageBox.Show(ioe.Message);
}
finally
{
if(writer != null)
{
try
{
writer.Close();
}
catch(IOException ioe)
{
}
}
}[/CODE]

I have also attempted to use the following code however is does the same thing:

[CODE]try
{
using(StreamWriter writer = new StreamWriter("A:\monkey.txt", false, Encoding.UTF8))
{
writer.Write(txtBox.Text);
}
}
catch(IOException ioe)
{
MessageBox.Show(ioe.Message);
}[/CODE]

Can you solve my problem?

rai_net
Sign In or Register to comment.