繁体   English   中英

Process.Start Azure文件共享

[英]Process.Start Azure File Share

现在,已经为Azure文件共享设置了SMB 3.0的所有内容,只需打开一个新的资源管理器窗口并导航到\\\\ myfileshare.file.core.windows.net \\ myfileshare即可。 我已经构建了一个ac#应用程序,该应用程序将用户名和密码保存到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.Exit立即着火了, 明白了为什么

打开Azure文件共享的最佳方法是什么?

打开Azure文件共享的最佳方法是什么?

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

因此,我仍然建议您可以使用命令使用Windows资源管理器挂载并打开它。

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

因此,退出方法将立即被触发。

这是解决方法。

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

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

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

如果该过程不存在,则可以删除挂载磁盘并暂停一段时间。

注意:

如果客户关闭浏览器,该过程将不会立即消失,它将等待Windows收集它。 需要一些时间。

更多详细信息,您可以参考以下代码:

        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.关闭浏览器后:

在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM