Declaring new TextWriter with StreamWriter causes "System.IO.IOException: 'The process cannot access the file"

88 views Asked by At

I have the file being used by another process. However I need to be able to save to it using TextWriter (C# .NET). What can I do to get around this?

("The Process cannot access the File error" being thrown on new StreamWriter(filePathName)

        XmlSerializer _serializer = new XmlSerializer(typeof(T));
        using (TextWriter _writer = new StreamWriter(filePathName))
        {
            /* [Redacted - internal code] */
            _serializer.Serialize(_writer, XMLSettings);
        }

As seen above, I need to be able to create a TextWriter and pass that instance into the serializer.

1

There are 1 answers

0
David Browne - Microsoft On

I have the file being used by another process.

Then the other process is responsible for whether the file is opened exclusively, or whether other processes can read and or write to it. See eg the FileShare flags in FileStream's constructor.