简体   繁体   中英

IOException in StreamWriter

I am using StreamWriter serialization to overwrite to an existing xml file. 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 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. Because of this exception the serialization never took place so I ended up with a 0 byte file. 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.

Have you guys ever seen this behavior before from 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? even though MSDN indicates Window CE supports this System.IO library

Edit:

Below is my stacktrace, the CopyData() function is the one that contains my serialization. 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 .

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. It's the equaivalent of 0x80000000--which is not a defined value. StreamWriter only really documents IOException will occur if the filename is invalid. 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.

I had a similar problem as described. I was also reading/writing xml files. It turns out the problem is because even though I was using using(var writer = XmlWriter.Create(streamWriter, settings)){ ... }

the using statement was disposing of the xml writer without closing the stream and locking it.

To fix this you need to change the xmlWriterSettings CloseOutput = true

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