简体   繁体   English

StreamWriter中的IOException

[英]IOException in StreamWriter

I am using StreamWriter serialization to overwrite to an existing xml file. 我正在使用StreamWriter序列化覆盖现有的xml文件。 Below is my code segment: 以下是我的代码段:

using(StreamWriter sw = new StreamWriter("\\hard disk\logs\test.xml"))
{
   this.Serializer.Serialize(sw,this.StateObject);
}

For some reason from time to time I keep getting IOException on the StreamWriter line, it is very general and it does not have inner exception when i tried to debug it. 由于某些原因,我经常在StreamWriter行上获得IOException,它非常普遍,在尝试调试它时没有内部异常。

StreamWriter line actually clear out the content of my test.xml file and make it to become 0 byte but yet it throws exception at the same time. 实际上,StreamWriter行清除了我的test.xml文件的内容,并使其变为0字节,但同时引发了异常。 Because of this exception the serialization never took place so I ended up with a 0 byte file. 由于这个异常,序列化从未发生,所以我最终得到了一个0字节的文件。 I can use the exception handler to correct this 0 byte but I am more interested in fixing the original issue of why StreamWriter throws exception in the first place. 我可以使用异常处理程序来更正这个0字节,但我对解决为什么StreamWriter首先抛出异常的原始问题更感兴趣。

Have you guys ever seen this behavior before from StreamWriter? 你们以前曾经从StreamWriter看到过这种行为吗? I ensure you that the path is valid, and that I verified it has permission and no other process are accessing that file (even if there is I would have seen that error in my stack trace) 我确保您该路径是有效的,并且已验证它是否具有权限,并且没有其他进程正在访问该文件(即使我在堆栈跟踪中看到该错误)

I wonder the fact that I'm running on Window CE has anything to do with it? 我想知道我在Window CE上运行与它有关吗? even though MSDN indicates Window CE supports this System.IO library 即使MSDN指示Window CE支持此System.IO库

Edit: 编辑:

Below is my stacktrace, the CopyData() function is the one that contains my serialization. 下面是我的堆栈跟踪,其中CopyData()函数是包含我的序列化代码的函数。 Looking at the value at the line System.IO.__Error.WinIOError(Int32 errorCode, String str) , I see my str value is "\\hard disk\\logs\\test.xml" and the errorCode is 2147483648 . 查看System.IO.__Error.WinIOError(Int32 errorCode, String str)行的值,我看到我的str值为“ \\ hard disk \\ logs \\ test.xml”,而errorCode为2147483648

at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.IO.StreamWriter.CreateFile(String path, Boolean append)
at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.StreamWriter..ctor(String path)
at Shs.ScanPanel.CA.DataManager.DataManagercr.CopyData(Object data)
at System.Threading.Timer.ring()

2147483648 is an undocumented error. 2147483648是一个未记录的错误。 It's the equaivalent of 0x80000000--which is not a defined value. 这是0x80000000的等效值-这不是一个定义的值。 StreamWriter only really documents IOException will occur if the filename is invalid. 如果文件名无效, StreamWriter仅会真正记录IOException。 If the filename is invalid, then the error is occuring for some undocumented reason. 如果文件名无效,则由于某些未记录的原因而发生错误。

I'd recommend trying to use other classes like FileStream or File ( File.Create or File.Open ) directly to try and reproduce the problem to see if they provide a more detailed error. 我建议尝试直接使用其他类,如FileStreamFileFile.CreateFile.Open )直接尝试重现该问题,以查看它们是否提供了更详细的错误。

I had a similar problem as described. 我遇到了类似的问题。 I was also reading/writing xml files. 我也在读/写xml文件。 It turns out the problem is because even though I was using using(var writer = XmlWriter.Create(streamWriter, settings)){ ... } 事实证明,问题是因为即使我正在使用(var writer = XmlWriter.Create(streamWriter,settings)){...}

the using statement was disposing of the xml writer without closing the stream and locking it. using语句是在不关闭流并将其锁定的情况下处理xml编写器的。

To fix this you need to change the xmlWriterSettings CloseOutput = true 要解决此问题,您需要更改xmlWriterSettings CloseOutput = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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