Dat*_*ase 3 c# file-io networking file
我有一个程序可以执行不同的操作,我的问题与网络映射驱动器或共享文件夹中的访问文件有关
程序可以从网络运行文件msi/exe(网络映射驱动器或共享文件夹)程序可以从网络复制文件(网络映射驱动器或共享文件夹)
在尝试运行或复制之前,如何检查文件是否可访问(如果网络断开连接或任何其他网络问题)?
是不够的 File.Exists();
这是我的代码示例:
public static bool FileIsOk(string path)
{
try
{
FileInfo finfo = new FileInfo(path);
if (finfo.Exists)
{
return true;
}
MessageBox.Show("file does not exist, or there is a problem with the network preventing access to the file!");
return false;
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
谢谢
File.Exists()应该没问题,但如果你开始一个大型的复制操作,如果在这个过程中连接断开,你就无法做很多事情,因此你需要确保为此编码.
您应该根据需要捕获IOException
并处理它.
编辑:捕获IOException的代码:
try
{
File.Copy(myLocalFile, myNetworkFile);
}
catch (IOException ioEx)
{
Debug.Write(myLocalFile + " failed to copy! Try again or copy later?");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10159 次 |
最近记录: |