简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM