如何使用C#获取ISO I安装的驱动器号

时间:2015-07-16 19:24:52

标签: c# visual-studio-2013

我一直在研究自动安装程序,因为我必须重新安装Windows,我安装的软件之一是作为ISO的Visual Studio 2013.

我已经编写了一些安装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");
            }

2 个答案:

答案 0 :(得分:2)

添加DevicePath参数,这会导致返回MSFT_DiskImage#inputList属性中包含挂载点。

答案 1 :(得分:0)

标记的答案对我不起作用所以基于我在PowerShell ISO mounting & unmounting上找到的博客文章我决定使用C#编写这个代码作为一个很好的例子(需要参考System.Management.Automation& 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();
        }