为了账号安全,请及时绑定邮箱和手机立即绑定

如何检查网络/共享文件夹的稳定性?

如何检查网络/共享文件夹的稳定性?

C#
米琪卡哇伊 2022-11-21 16:28:27
在工作中,我们有一个共享文件夹,我在其中收集了一些数据。在我的电脑上,我必须确保服务器在数据收集期间没有关闭。所以,我的方法是,在几分钟的时间间隔内,我将连接并重新连接到我的服务器几次(如果失败,则停止数据收集并等待或执行下一个任务)连接和重新连接到网络驱动器/共享文件夹的最佳方式是什么?我会做类似的事情public bool checkNet(UNCPath){int connected = 0;bool unstable = true;while(unstable){SubfunctionConnect(UNCPath); //connect to network using cmd 'net use' commandif(directory.exists(UNCPath) {++connected;}else{connected = 0;}}if(connected >= 3) unstable = false; //after 3 in arrow  successful connections then leave loop and proceed further tasksreturn true;}
查看完整描述

1 回答

?
湖上湖

TA贡献2003条经验 获得超2个赞

我正在维护一个与您的要求具有相似功能的项目。


在该功能中,我们使用FileSystemWatcher来监控特定 UNC 位置的各种操作。您可以实施OnError将在 UNC 路径不可用时触发的事件。


您可以查看上面的链接了解详细信息,这里仍然是一个简短的示例


using (FileSystemWatcher watcher = new FileSystemWatcher(@"\\your unc path"))

{

    // Watch for changes in LastAccess and LastWrite times, and

    // the renaming of files or directories.

    watcher.NotifyFilter = NotifyFilters.LastAccess

                         | NotifyFilters.LastWrite

                         | NotifyFilters.FileName

                         | NotifyFilters.DirectoryName;


    // Only watch text files.

    watcher.Filter = "*.txt";


    watcher.Created += (s, e) => { Console.WriteLine($"Created {e.Name}"); };

    watcher.Deleted += (s, e) => { Console.WriteLine($"Deleted {e.Name}"); };

    watcher.Error += (s, e) => { Console.WriteLine($"Error {e.GetException()}"); };



    // Begin watching.

    watcher.EnableRaisingEvents = true;


    // Wait for the user to quit the program.

    Console.WriteLine("Press 'q' to quit the sample.");

    while (Console.Read() != 'q') ;

}


查看完整回答
反对 回复 2022-11-21
  • 1 回答
  • 0 关注
  • 91 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信