Process.Start Azure文件共享

Process.Start Azure文件共享

问题描述:

现在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文件共享的最佳方式?

什么是打开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

我要感谢@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();