Does StreamWriter work inconsistently with relative paths?
I am trying to save a LINQ XML document using a StreamWriter. Using the following code works fine when the document is small (~6kb on disk) but doesn't work when the file is larger (~66kb on disk). If I replace the relative path with an absolute path it works fine in both situations. Is there any reason why the relative path should fail with a larger file?
NB: I am not getting any exception, but no file is created/written to unless I use an absolute path (with the larger dataset - smaller dataset works fine with relative path)
XDocument xMap = new XDocument( ... );
// Works for small file but not large
using (StreamWriter writer = new StreamWriter("map.xml", false, new UTF8Encoding(false))) {
xMap.Save(writer);
}
// Works consiste开发者_如何学Pythonntly
using (StreamWriter writer = new StreamWriter(@"c:\data\map.xml", false, new UTF8Encoding(false))) {
xMap.Save(writer);
}
There is no reason that using a relative path would make it fail for large files.
Are you sure that the relative path ended up being where you think it is? If the relative path is on a network, or if its drive is full, that could explain it.
What exception are you getting?
EDIT: The current directory probably changed for some reason. Check the value of Environment.CurrentDirectory
when it fails and make sure it's what you think it is.
精彩评论