如何检查 Network/Shared 文件夹稳定性?

How to check Network/Shared Folder stability?

在工作中,我们有一个共享文件夹,我可以在其中收集一些数据。在我的电脑上,我必须确保服务器在数据收集期间没有关闭。

所以,我的方法是,在几分钟的间隔内,我将连接并重新连接到我的服务器几次(如果失败,则停止数据收集并等待或执行下一个任务)

连接和重新连接到网络 drive/shared 文件夹的最佳方式是什么? 我会做类似

的事情
public bool checkNet(UNCPath)
{
int connected = 0;
bool unstable = true;
while(unstable)
{
SubfunctionConnect(UNCPath); //connect to network using cmd 'net use' command
if(directory.exists(UNCPath) 
{
++connected;
}
else
{
connected = 0;
}
}
if(connected >= 3) unstable = false; //after 3 in arrow  successful connections then leave loop and proceed further tasks
return true;
}

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

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

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

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') ;
}