System.IO.IOException: file used by another process

前端 未结 10 2336
囚心锁ツ
囚心锁ツ 2020-12-14 16:59

I\'ve been working on this small piece of code that seems trivial but still, i cannot really see where is the problem. My functions do a pretty simple thing. Opens a file, c

10条回答
  •  失恋的感觉
    2020-12-14 17:24

    I realize that I is kinda late, but still better late than never. I was having similar problem recently. I used XMLWriter to subsequently update XML file and was receiving the same errors. I found the clean solution for this:

    The XMLWriter uses underlying FileStream to access the modified file. Problem is that when you call XMLWriter.Close() method, the underlying stream doesn't get closed and is locking the file. What you need to do is to instantiate your XMLWriter with settings and specify that you need that underlying stream closed.

    Example:

    XMLWriterSettings settings = new Settings();
    settings.CloseOutput = true;
    XMLWriter writer = new XMLWriter(filepath, settings);
    

    Hope it helps.

提交回复
热议问题