2017-07-31 105 views
0

現在SMB 3.0的所有內容都爲Azure文件共享設置,只需打開一個新的資源管理器窗口並導航到\\ myfileshare.file.core.windows.net \ myfileshare即可。我已經構建了一個c#應用程序,它將用戶名和密碼保存到一個天藍色的文件共享中,以備後用。Process.Start Azure文件共享

爲了使應用程序更加用戶友好(大部分將由SysAdmins使用)我想添加文件>打開Azure文件共享按鈕。這是我遇到麻煩的地方。

我將從一些給定的信息開始:​​uncPath是文件共享的全部內容。這裏是我試過的代碼:

Process.Start(uncPath, username, password.ToSecureString(), "."); 
--> Throws a Win32Exception, Username or Password incorrect 
--> (They are both correct, The Domain is throwing this off.) 

我永遠無法解決這個問題,所以我去了另一條路線。 NET使用文件共享,然後打開它。這工作,但我想在用戶存在的過程中取消映射共享。 (我不想離開映射驅動器周圍鋪設。)下面是代碼我曾嘗試:

/* --- MAP THE DRIVE --- */ 
Process p = new Process 
{ 
    StartInfo = new ProcessStartInfo 
    { 
     FileName = "net.exe", 
     Arguments = $"use {uncPath} /u:{username} {password}", 
     RedirectStandardOutput = true, 
     RedirectStandardError = true, 
     UseShellExecute = false, 
    } 
}; 
p.Start(); 


/* --- OPEN THE UNC PATH --- */ 
Process azureP = new Process 
{ 
    StartInfo = 
     { 
      FileName = "explorer.exe", 
      Arguments = uncPath, 
      UseShellExecute = false, 
     }, 
    EnableRaisingEvents = true, 
}; 
azureP.Start(); 

/* -- UNMAP THE DRIVE ON EXIT --- */ 
azureP.Exited += ((object proc, EventArgs procE) => 
{ 
    Process azurePExit = new Process 
    { 
     StartInfo = new ProcessStartInfo 
     { 
      FileName = "net.exe", 
      Arguments = $"use {uncPath} /delete", 
      RedirectStandardOutput = true, 
      RedirectStandardError = true 
     } 
    }; 
}); 

正如預期的那樣,立即azureP.Exited火災,我理解爲什麼

什麼是打開azure文件共享的最佳方式?

回答

1

什麼是打開azure文件共享的最佳方式?

在我看來,以訪問本地文件共享的最佳方法是使用SMB3.0安裝磁盤。

所以我仍然建議你可以使用命令裝載並使用Windows資源管理器打開它。

但是隨着代碼的顯示,azureP進程不會影響瀏覽器進程。

所以exit方法會立即被觸發。

這是解決方法。

我建議你可以得到AzureP進程啓動資源管理器進程ID。

然後您可以使用Process.GetProcessesByName(「explorer」)方法獲取所有當前的資源管理器進程 。

然後,您可以編寫一個while循環來檢查選定的進程是否根據進程ID打開了資源管理器。

如果該過程不存在,那麼您可以刪除安裝磁盤並暫停。

注意:

如果客戶關閉瀏覽器中,該進程將不會立即消失,它會等待窗口領取。這將需要幾次。

更多細節,你可以參考下面的代碼:

 Process azureP = new Process 
     { 
      StartInfo = 
     { 
     FileName = "explorer.exe", 
     Arguments = uncPath, 
     UseShellExecute = false, 

     }, 
      EnableRaisingEvents = true, 
     }; 

     azureP.Start(); 

     azureP.WaitForExit(); 
     //find the open explorer process 
     Process[] CurrentProcess1 = Process.GetProcessesByName("explorer"); 
     int Explorerprocessid = -1; 
     foreach (var item in CurrentProcess1) 
     { 
      if (azureP.StartTime < item.StartTime) 
      { 
       Console.WriteLine(item.Id); 
       Explorerprocessid = item.Id; 
      } 
     } 


     while (true) 
     { 
      Thread.Sleep(5000); 
      Process[] CurrentProcess2 = Process.GetProcessesByName("explorer"); 
      List<int> l1 = new List<int>(); 
      foreach (var item in CurrentProcess2) 
      { 
       l1.Add(item.Id); 
      } 
      if (l1.Contains(Explorerprocessid)) 
      { 
       Console.WriteLine("Continue"); 
      } 
      else 
      { 
       //Delete the mount 
       //Process azurePExit = new Process 
       //{ 
       // StartInfo = new ProcessStartInfo 
       // { 
       //  FileName = "net.exe", 
       //  Arguments = $"use {uncPath} /delete", 
       //  RedirectStandardOutput = true, 
       //  RedirectStandardError = true 
       // } 
       //}; 
       Console.WriteLine("Break"); 
       break; 
      } 
     } 

結果:

1.當程序開始運行:

enter image description here

2.After關閉資源管理器:

enter image description here

0

我要感謝@Brando Zhang的回答。此代碼運行良好,但我將測試2個解決方案。 Brando Zhang描述的解決方案1,但略有修改將掛載共享,打開共享,獲取瀏覽器進程,等待它關閉,然後拆卸共享。解決方案2將掛載共享,然後打開共享(感謝explorer.exe的工作方式)立即卸載共享。

解決方案1:

  Process netuseP = new Process 
      { 
       StartInfo = new ProcessStartInfo 
       { 
        FileName = "net.exe", 
        Arguments = $"use {uncPath} /u:{username} {password}", 
        RedirectStandardOutput = true, 
        RedirectStandardError = true, 
        UseShellExecute = false, 
       } 
      }; 
      netuseP.Start(); 


      /* --- OPEN THE UNC PATH --- */ 
      Process azureP = new Process 
      { 
       StartInfo = { 
        FileName = "explorer.exe", 
        Arguments = uncPath, 
        UseShellExecute = false, 
       }, 
       EnableRaisingEvents = true, 
      }; 
      azureP.Start(); 


      /* --- WAIT FOR THE PATH TO BE OPENED --- */ 
      azureP.Exited += ((object proc, EventArgs procE) => 
      { 
       /* --- GET THE EXPLORER.EXE PROCESS THAT IS RELATED TO THE AZURE STORAGE ACCOUNT --- */ 
       Process[] currentExplorers = Process.GetProcessesByName("explorer"); 
       Process explorerP = null; 
       foreach (Process p in currentExplorers) 
       { 
        if (azureP.StartTime < p.StartTime) 
        { 
         explorerP = p; 
        } 
       } 
       if (explorerP != null) 
       { 
        explorerP.Exited += ((object eProc, EventArgs eProcE) => 
        { 
         /* --- DEMOUNT THE FILE SHARE --- */ 
         netuseP = new Process 
         { 
          StartInfo = new ProcessStartInfo 
          { 
           FileName = "net.exe", 
           Arguments = $"use {uncPath} /delete", 
           RedirectStandardOutput = true, 
           RedirectStandardError = true, 
           UseShellExecute = false, 
          } 
         }; 
         netuseP.Start(); 
        }); 
       } 
      }); 

解決方案2:

  /* --- MAP THE DRIVE --- */ 
      Process netuseP = new Process 
      { 
       StartInfo = new ProcessStartInfo 
       { 
        FileName = "net.exe", 
        Arguments = $"use {uncPath} /u:{username} {password}", 
        RedirectStandardOutput = true, 
        RedirectStandardError = true, 
        UseShellExecute = false, 
       } 
      }; 
      netuseP.Start(); 


      /* --- OPEN THE UNC PATH --- */ 
      Process azureP = new Process 
      { 
       StartInfo = { 
        FileName = "explorer.exe", 
        Arguments = uncPath, 
        UseShellExecute = false, 
       }, 
       EnableRaisingEvents = true, 
      }; 
      azureP.Start(); 

      /* WAIT FOR WINDOWS TO OPEN THE SHARE */ 
      System.Threading.Thread.Sleep(2000); 


      /* DEMOUNT THE SHARE */ 
      netuseP = new Process 
      { 
       StartInfo = new ProcessStartInfo 
       { 
        FileName = "net.exe", 
        Arguments = $"use {uncPath} /delete", 
        RedirectStandardOutput = true, 
        RedirectStandardError = true, 
        UseShellExecute = false, 
       } 
      }; 
      netuseP.Start();