views:

187

answers:

1

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 consistently
using (StreamWriter writer = new StreamWriter(@"c:\data\map.xml", false, new UTF8Encoding(false))) {
    xMap.Save(writer);
}
+2  A: 

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.CurrentDirectorywhen it fails and make sure it's what you think it is.

SLaks
I'm not getting any exception, it's failing silently. If the data I am saving contains only a small number of elements, it saves and I can see the file appear in the folder. If I then increase the dataset by loading from a larger source, and then click save again, no exception is thrown, but no file appears in the directory.
jeffora
Thanks, that was the problem. I thought the CurrentDirectory was supposed to remain as the directory from which the executable runs?
jeffora
No; the current directory can change. For example, the file dialogs can set the current directory.
SLaks