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()