如何解决网络连接丢失时,c#File.Exists结果错误
我必须检查映射的网络驱动器上的文件。
f.e。 P:\ myFolder \ myFile.dat
FileInfo fi = new FileInfo(myfile);
if (fi.Exists)
{
// Exists does not work if the network was interrupted.
// For whatever reason
// So now Try ... Catch,the FileInfo constructor can actually be omitted!
try
{
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(myfile);
}
catch (FileNotFoundException ex)
{
this.Version = new Version("1.0.0");
}
}
else
{
this.Version = new Version("1.0.0");
}
一切正常-直到拔下网络电缆为止。
FileInfo
仍然认为该文件存在。
为什么?
解决方法
文档说明您描述的行为:
首次检索属性时,FileInfo调用Refresh方法并缓存有关文件的信息。在后续通话中,您必须致电刷新以获取最新信息。
https://docs.microsoft.com/en-us/dotnet/api/system.io.fileinfo
,fi.Refresh(); worked !
@Sommmen
File.Exists(myfile)
也不起作用。
它有和FileInfo一样的问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。