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

问题描述 投票:1回答:1

在工作中我们有一个共享文件夹,我在那里做一些数据收集。从我的计算机我必须确保服务器在数据收集期间没有关闭。

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

连接和重新连接到网络驱动器/共享文件夹的最佳方法是什么?我会做点什么的

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;
}
c# network-programming shared-directory
1个回答
1
投票

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

在该功能中,我们使用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') ;
}
© www.soinside.com 2019 - 2024. All rights reserved.