文件已存在时创建XmlWriter时出错 [英] Error creating an XmlWriter when file already exists

查看:216
本文介绍了文件已存在时创建XmlWriter时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的代码中,我有这些行:

  XmlWriterSettings writerSettings = new XmlWriterSettings(); 
writerSettings.Indent = true;
XmlWriter writer = XmlWriter.Create(filename,writerSettings);
document.Save(作家);

filename 不存在。但是当它的时候,我得到这个错误(在第三行,而不是第四行):
$ b $ pre $ System.IO.IOException:在文件路径上共享冲突

如果文件已经存在,我想覆盖文件。如何做到这一点?

解决方案

如果仔细观察 IOException 一个共享违规。这意味着当您尝试访问此文件时,其他程序正在使用它。通常,阅读并不是什么大问题,但是写入文件可能会发生很多。你应该:


  • 试着找出其他程序是否正在使用这个文件,程序是什么,为什么它在做所以即可。某些程序(尤其是那些没有良好垃圾处理能力的语言编写的程序)正在访问文件,然后没有关闭IO流,从而锁定了文件。还有一些实用程序(如果我的记忆正确地为我服务),可以让您看到哪些进程正在使用某个文件 - 只需要将它放到Google上即可。

  • 有可能在您调试程序中,您可能会杀死进程或其他内容(有时会这样做), IO流可能未被关闭。为此,最简单的修复(就我所知)只是重新启动。

  • 或者,问题可能来自您自己的代码。但是,在编写C#和垃圾回收时,除了IO功能之外,通常还会防止出现这样的问题。您可能忘记关闭文件流。我有时会这样做,即使修复几乎是即时的,也需要相当长的一段时间才能找到错误的位置。如果您遍历您的程序,并利用手表跟踪您的IO操作,找到这样的错误应该相对简单。



祝你好运!


In my code, I have these lines:

XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Indent = true;
XmlWriter writer = XmlWriter.Create(filename, writerSettings);
document.Save(writer);

This works fine when filename does not exist. But when it does, I get this error (on the 3rd line, not the 4th):

System.IO.IOException: Sharing violation on path [the file path]

I want to overwrite the file if it already exists. How do I do this?

解决方案

If you look carefully at the IOException, it says that it's a "sharing violation". This means that while you are trying to access this file, another program is using it. Usually, it's not much of a problem with reading, but with writing to files this can happen quite a lot. You should:

  • Try to find out if some other program is using this file, what the program is, and why it's doing so. It's possible that some programs (especially those written in languages without good garbage handling capabilities) were accessing the file and then did not close the IO stream, thus locking up the file. There are also some utilities (if my memory serves me correctly) that allow you to see what processes are using a certain file - just google it.
  • There's a possibility that when you were debugging your program, you may have killed the process or something (I do that sometimes), and the IO stream may have not been closed. For this, the easiest fix (as far as I know) is just a reboot.
  • Alternatively, the issue may be coming from your own code. However, as you're writing in C#, and garbage collection, along with the IO capabilities, usually prevent such problems, you might have forgotten to close a file stream somewhere. I do this sometimes, and it takes quite a while to find the location of the bug, even though the fix is nearly instant. If you step through your program and utilize watches to keep track of your IO operations, it should be relatively simple to find such a bug.

Good luck!

这篇关于文件已存在时创建XmlWriter时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆