我怎样才能得到我用c#挂载的ISO的驱动器号?

本文关键字:ISO 驱动器 | 更新日期: 2023-09-27 18:03:19

我一直在做一个自动安装程序,因为我必须重新安装Windows,我安装的一个软件是Visual Studio 2013,它是一个ISO。

我已经写了一些挂载ISO的代码,但是我不知道当我运行安装程序时如何返回驱动器号。

else if (soft == "Visual Studio 2013 Pro")
            {
                var isoPath = Loc.path + Loc.vs2013pro;
                using (var ps = PowerShell.Create())
                {
                    ps.AddCommand("Mount-DiskImage").AddParameter("ImagePath", isoPath).Invoke();
                }
                var proc = System.Diagnostics.Process.Start(Loc.path + Loc.vs2013pro);
                proc.WaitForExit();
                System.IO.File.Copy(Loc.path + @"'Visual Studio 2013 Pro'Serial.txt", folder + "/Serials/VS2013Pro Serial.txt");
            }

我怎样才能得到我用c#挂载的ISO的驱动器号?

添加PassThru参数,将返回MSFT_DiskImageDevicePath属性中包含挂载点

标记的答案不适合我,所以基于我在PowerShell ISO安装上发现的博客文章&我决定用c#编写这个代码,作为一个很好的例子。System.Management.Automation.Runspaces)。

        string isoPath = "C:''Path''To''Image.iso";
        using (var ps = PowerShell.Create())
        {
            //Mount ISO Image
            var command = ps.AddCommand("Mount-DiskImage");
            command.AddParameter("ImagePath", isoPath);
            command.Invoke();
            ps.Commands.Clear();
            //Get Drive Letter ISO Image Was Mounted To
            var runSpace = ps.Runspace;
            var pipeLine = runSpace.CreatePipeline();
            var getImageCommand = new Command("Get-DiskImage");
            getImageCommand.Parameters.Add("ImagePath", isoPath);
            pipeLine.Commands.Add(getImageCommand);
            pipeLine.Commands.Add("Get-Volume");
            string driveLetter = null;
            foreach (PSObject psObject in pipeLine.Invoke())
            {
                driveLetter = psObject.Members["DriveLetter"].Value.ToString();
                Console.WriteLine("Mounted On Drive: " + driveLetter);
            }
            pipeLine.Commands.Clear();
            //Unmount Via Image File Path
            command = ps.AddCommand("Dismount-DiskImage");
            command.AddParameter("ImagePath", isoPath);
            ps.Invoke();
            ps.Commands.Clear();
            //Alternate Unmount Via Drive Letter
            ps.AddScript("$ShellApplication = New-Object -ComObject Shell.Application;" + 
                "$ShellApplication.Namespace(17).ParseName('"" + driveLetter + ":'").InvokeVerb('"Eject'")");
            ps.Invoke();
            ps.Commands.Clear();
        }