user3312266
user3312266

Reputation: 189

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

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.

Upvotes: -1

Views: 142

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89386

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.

Upvotes: 0

Related Questions