StreamWriter 与相对路径的工作不一致吗?
我正在尝试使用 StreamWriter 保存 LINQ XML 文档。当文档较小(磁盘上约 6kb)时,使用以下代码可以正常工作,但当文件较大(磁盘上约 66kb)时,使用以下代码就不起作用。如果我用绝对路径替换相对路径,则在两种情况下都可以正常工作。对于较大的文件,相对路径会失败吗?有什么原因吗?
注意:我没有收到任何异常,但除非我使用绝对路径,否则不会创建/写入任何文件(使用较大的数据集 - 较小的数据集可以很好地使用相对路径)
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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用相对路径没有理由会导致大文件失败。
您确定相对路径最终位于您认为的位置吗?如果相对路径位于网络上,或者驱动器已满,则可以解释这一点。
你遇到什么异常?
编辑:当前目录可能由于某种原因发生了更改。当失败时检查
Environment.CurrentDirectory
的值,并确保它是您认为的那样。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.