WriteLine hangs ... PLEAZ HALP MEEE!!!

  • Thread starter Thread starter Scott Roberts
  • Start date Start date
S

Scott Roberts

wASP said:
Hi,

I'm having a problem with the StreamWriter class in System.IO.

I can generate output to a webpage by attaching a string object to a label,
and viewing it OK on postback, but when I try to write it to a file (appended),
with stream IO, it hangs after about 4,180 bytes.

It goes something like this:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -=-=-=-=-=-=-
class my_class
{ private string logfile_path = @"I:\Virtual Webfolders\CtrlInit\Struct.txt";
private StreamWriter strm_wrtr;
private string dbl_line = "============================<br>";

private void my_fn ()
{ strm_wrtr = File.AppendText(logfile_path);

strm_wrtr.WriteLine(dbl_line);

// more code with WriteLines scattered throughout.

}
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= -=-=-=-=-=-=-



After it hangs, the file is locked - I cannot delete it - or change its contents
- until I restart IIS.

Does anyone have any ideas that might help this poor fool out?

I don't see any immediate problems with the code you posted (assuming you
have strm_wrtr.Close() somewhere). Are you sure it just hangs? No
exceptions? Can we see "// more code"?

Does anything at all get written to the file (i.e. once you stop IIS and
view the file is there anything in it)? Could it be a permissions problem
(i.e. the user account under which IIS is running doesn't have permission to
write to that directory)? If this were the case, you would get an exception
as opposed to a "hang", but if you are catching exceptions and not
re-throwing them it could appear to be a "hang". It's difficult to tell
without all of the code.
 
Hi,

I'm having a problem with the StreamWriter class in System.IO.

I can generate output to a webpage by attaching a string object to a label,
and viewing it OK on postback, but when I try to write it to a file (appended),
with stream IO, it hangs after about 4,180 bytes.

It goes something like this:
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
class my_class
{ private string logfile_path = @"I:\Virtual Webfolders\CtrlInit\Struct.txt";
private StreamWriter strm_wrtr;
private string dbl_line = "============================<br>";

private void my_fn ()
{ strm_wrtr = File.AppendText(logfile_path);

strm_wrtr.WriteLine(dbl_line);

// more code with WriteLines scattered throughout.

}
}
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-



After it hangs, the file is locked - I cannot delete it - or change its contents
- until I restart IIS.

Does anyone have any ideas that might help this poor fool out?

Thanks,

- wASP
 
I don't see any immediate problems with the code you posted (assuming you
have strm_wrtr.Close() somewhere). Are you sure it just hangs? No
exceptions? Can we see "// more code"?

Does anything at all get written to the file (i.e. once you stop IIS and
view the file is there anything in it)? Could it be a permissions problem
(i.e. the user account under which IIS is running doesn't have permission to
write to that directory)? If this were the case, you would get an exception
as opposed to a "hang", but if you are catching exceptions and not
re-throwing them it could appear to be a "hang". It's difficult to tell
without all of the code.

AAAAAAACK!!!

I didn't close the stream!

strm_wrtr.Close();

THAT fixed it.

I'm new to ASP.NET, so I'm still getting the hang of the basics.
This is the first experience that I've ever had with stream IO in C#/ASP.NET.

(BTW: I did have some problems with permisions, but I was able to fix that myself.)

Thanks Scott!

- wASP
 
wASP wrote:
AAAAAAACK!!!

I didn't close the stream!

strm_wrtr.Close();
<snip>

You should look at the "using" keyword, which can in your case be used
like this:

using (StreamWriter strm_wrtr = File.AppendText(logfile_path))
{
strm_wrtr.WriteLine(dbl_line);
}

in this context, "using" means the same as:

StreamWriter strm_wrtr = File.AppendText(logfile_path);
try
{
strm_wrtr.WriteLine(dbl_line);
}
finally
{
strm_wrtr.Dispose(); // calls close
}
 
Back
Top