FileStream "cannot access private file"

Why do I get the above error message when I use using (fileStream = new FileStream(path, FileMode.Append, FileAccess.Write)), but the program works fine when I replace it with fileStream = File.Create(path);?

I want to add console output to an external file. Here is my code:

 //copy console output to file in bin\Debug folder
    class ConsoleCopy : IDisposable
    {
        FileStream fileStream;
        StreamWriter fileWriter;
        TextWriter doubleWriter;
        TextWriter oldOut;
        class DoubleWriter : TextWriter
        {
            TextWriter one;
            TextWriter two;
            public DoubleWriter(TextWriter one, TextWriter two)
            {
                this.one = one;
                this.two = two;
            }
            public override Encoding Encoding
            {
                get { return one.Encoding; }
            }
            //Clears all buffers for the current writer
            public override void Flush()
            {
                one.Flush();

                two.Flush();
            }
            public override void Write(char value)
            {
                **one.Write(value);**//error thrown here
                two.Write(value);
            }
        }
        public ConsoleCopy(string path)
        {
            oldOut = Console.Out;
            try
            {
                **//fileStream = File.Create(path);** //runs alright with this line
                fileWriter = new StreamWriter(fileStream);
                fileWriter.AutoFlush = true;
                doubleWriter = new DoubleWriter(fileWriter, oldOut);
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot open file for writing");
                Console.WriteLine(e.Message);
                return;
            }
            Console.SetOut(doubleWriter);
        }
        public void Dispose()
        {
            Console.SetOut(oldOut);
            if (fileWriter != null)
            {
                fileWriter.Flush();
                fileWriter.Close();
                fileWriter = null;
            }
            if (fileStream != null)
            {
                fileStream.Close();
                fileStream = null;
            }
        }
    }//end of consoleCopy

I call this method in my main method as such:

 //pass output to ConsoleCopy method for copying to .txt file
        using (var cc = new ConsoleCopy("mylogfile.txt"))
        {
           DateTime theDate = DateTime.UtcNow;

            string custom = theDate.ToString("d");

            Console.WriteLine("User has logged in on " + custom);
        }

UPDATE:

The error was previously shown on one.Write(value). I managed to solve this, though thanks to Peter. Thank you all for your contribution and help :)

+4
source share
2 answers

: , , , using ConsoleCopy, , ConsoleCopy FileStream . , , , . - FileStream Dispose(), using .

, , ? , - , , .

using (var cc = new ConsoleCopy("mylogfile.txt"))
{
    // At this time, your filestream is created with the using statement.
    // Writing happens here.
    // Your filestream is closed at the end of the using statement inside of the ConsoleCopy constructor.
} 
// At this point, ConsoleCopy tries to call Dispose(), which flushes and closes everything again. However, this throws an exception because the using statement already closed the FileStream.

- , Dispose() ,

**//fileStream = File.Create(path);** //runs alright with this line

fileStream = File.Open(path, System.IO.FileMode.Append, System.IO.FileAccess.Write);

using ?

+4

using? , , - .

, :

using (fileStream = new FileStream(path, FileMode.Append, FileAccess.Write))
{
  fileWriter = new StreamWriter(fileStream);
  fileWriter.AutoFlush = true;
  doubleWriter = new DoubleWriter(fileWriter, oldOut);
}
+1

All Articles