亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何檢查網絡/共享文件夾的穩定性?

如何檢查網絡/共享文件夾的穩定性?

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 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號