【问题标题】:Process.Start Azure File ShareProcess.Start Azure 文件共享
【发布时间】:2017-07-31 13:31:49
【问题描述】:

现在 SMB 3.0 的所有内容都已针对 Azure 文件共享进行设置,只需打开一个新的资源管理器窗口并导航到 \\myfileshare.file.core.windows.net\myfileshare 即可。我已经构建了一个 c# 应用程序,它将用户名和密码保存到一个 azure 文件共享中,以供以后使用。

为了使应用程序对用户更加友好(主要由系统管理员使用),我想添加一个文件 > 打开 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 文件共享的最佳方式是什么?

【问题讨论】:

    标签: c# azure process process.start


    【解决方案1】:

    打开 azure 文件共享的最佳方式是什么?

    在我看来,在本地访问文件共享的最佳方式是使用 SMB3.0 挂载作为磁盘。

    所以我还是建议你可以使用命令挂载并使用windows资源管理器打开它。

    但正如您的代码所示,azureP 进程不会影响资源管理器进程。

    所以退出方法会立即被触发。

    这是解决方法。

    我建议您可以获取 azureP 进程启动资源管理器进程 ID。

    然后你可以使用 Process.GetProcessesByName("explorer") 方法来获取 所有当前的资源管理器进程。

    然后您可以编写一个while循环来检查所选进程是否根据进程ID打开了资源管理器。

    如果该进程不存在,那么您可以删除挂载磁盘并中断while。

    注意:

    如果客户关闭资源管理器,该进程不会立即消失,它会等待窗口收集它。这需要一些时间。

    更多细节,您可以参考以下代码:

            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.程序开始运行时:

    2.关闭资源管理器后:

    【讨论】:

      【解决方案2】:

      我要感谢@Brando Zhang 的回答。此代码运行良好,但我将测试 2 个解决方案。解决方案 1 如 Brando Zhang 所述,但稍作修改将挂载共享,打开共享,获取资源管理器进程,等待它关闭,然后卸载共享。解决方案 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(); 
      

      【讨论】:

        猜你喜欢
        • 2016-07-03
        • 2022-06-30
        • 1970-01-01
        • 2021-12-08
        • 2020-07-12
        • 1970-01-01
        • 2022-10-19
        • 2021-03-09
        • 2019-07-11
        相关资源
        最近更新 更多