This is a sequel to my question here about trimming the fat off a log file.
I have this code:
private readonly FileStream _fileStream;
private readonly StreamWriter _streamWriter;
. . .
const int MAX_LINES_DESIRED = 1000;
string uriPath = GetExecutionFolder() + "\\Application.log";
string localPath = new Uri(uriPath).LocalPath;
if (!File.Exists(localPath))
{
File.Create(localPath);
}
_fileStream = File.OpenWrite(localPath);
// First, remove the earliest lines from the file if it's grown too much
StreamReader reader = new StreamReader(_fileStream);
. . .
Which fails on the last line shown with:
System.ArgumentException was unhandled
_HResult=-2147024809
_message=Stream was not readable.
Why is it not readable? I thought maybe because the file was empty, but I added a line to it, and still get the same err msg.
File.OpenWrite
creates a non-readable stream. If you're going to be reading and writing to the stream, you need to use File.Open(localPath, FileMode.Open, FileAccess.ReadWrite)
.
Additionally, you can just use FileMode.OpenOrCreate
, and that will remove the need for your File.Exists
conditional.
The error code in hex is FFFFFFFF80070075
which equates to E_INVALIDARG - One or more arguments are invalid
. See http://msdn.microsoft.com/en-us/library/cc704587.aspx for the definitions of the error codes.
Based on this, the stream not readable message, and looking at the code, it looks like the open call should specify the ReadWrite file access attribute on the call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With