如何从未分配的分区创建简单卷

问题描述 投票:1回答:1

我正在尝试在我的硬盘上打开一个位置来存储一些许可文件。

到目前为止,我已经尝试过diskpart。它看起来很容易使用,但我无法使用diskpart格式化未分配的分区。我找到了一种创建未分配空间的方法,但我必须将其格式化才能使用(如果我在这里错了,请纠正我。我对磁盘分区的东西真的很新)

这是我选择合适音量的方法。我从这里拿走它,它运作良好。链接:C# and diskpart: how to select by disk label and not by a number?和我正在使用的代码是这样的:

public int GetIndexOfDrive(string drive)
{
    drive = drive.Replace(":", "").Replace(@"\", "");

    // execute DiskPart programatically
    Process process = new Process();
    process.StartInfo.FileName = "diskpart.exe";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.Start();
    process.StandardInput.WriteLine("list volume");
    process.StandardInput.WriteLine("exit");
    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();

    // extract information from output
    string table = output.Split(new string[] { "DISKPART>" },         StringSplitOptions.None)[1];
    var rows = table.Split(new string[] { "\n" }, StringSplitOptions.None);
    for (int i = 3; i < rows.Length; i++)
    {
        if (rows[i].Contains("Volume"))
        {
            int index = Int32.Parse(rows[i].Split(new string[] { " " }, StringSplitOptions.None)[3]);
            string label = rows[i].Split(new string[] { " " }, StringSplitOptions.None)[8];

            if (label.Equals(drive))
            {
                return index;
            }
        }
    }

    return -1;
}

一旦我得到索引,我运行自己的代码,用这段代码缩小选定的卷:

Process DiskPartProc = new Process();                                  
DiskPartProc.StartInfo.CreateNoWindow = true;
DiskPartProc.StartInfo.UseShellExecute = false;                        
DiskPartProc.StartInfo.RedirectStandardOutput = true;                  
DiskPartProc.StartInfo.FileName = @"C:\Windows\System32\diskpart.exe"; 
DiskPartProc.StartInfo.RedirectStandardInput = true;                   
DiskPartProc.Start();
DiskPartProc.StandardInput.WriteLine("select volume "+index);
DiskPartProc.StandardInput.WriteLine("shrink desired=16 minimum=16");
DiskPartProc.StandardInput.WriteLine("exit");                          
string output = DiskPartProc.StandardOutput.ReadToEnd();               
DiskPartProc.WaitForExit();

一旦我这样做,结果是这样的:

http://prntscr.com/mjwg0t(仅限未分配分区的图片)

我可以右键单击它并从该未分配的分区创建新的简单卷,但我必须使用diskpart命令执行此操作。有人能告诉我必须使用哪些diskpart命令来实现这一目标吗?我怎样才能获得有关此卷的详细信息?

c# cmd disk-partitioning
1个回答
0
投票

我已经解决了我的问题。这是我的最终代码:

int index = GetIndexOfDrive(Path.GetPathRoot(@"E:\"));

Process DiskPartProc = new Process();
DiskPartProc.StartInfo.CreateNoWindow = true;
DiskPartProc.StartInfo.UseShellExecute = false;
DiskPartProc.StartInfo.RedirectStandardOutput = true;
DiskPartProc.StartInfo.FileName = @"C:\Windows\System32\diskpart.exe";
DiskPartProc.StartInfo.RedirectStandardInput = true;
DiskPartProc.Start();
DiskPartProc.StandardInput.WriteLine("select volume " + index);
DiskPartProc.StandardInput.WriteLine("shrink desired=16 minimum=16");
DiskPartProc.StandardInput.WriteLine("create partition primary size=16");
DiskPartProc.StandardInput.WriteLine("format fs=ntfs label=\"MyPlace\" quick");
DiskPartProc.StandardInput.WriteLine("exit");
string output = DiskPartProc.StandardOutput.ReadToEnd();
DiskPartProc.WaitForExit();

问题出在我自己的磁盘上。我已经放了另一个备用的并完成了我的所有测试并且它完美地运行,现在我可以在磁盘内创建磁盘卷并对其进行格式化然后我可以使用其卷ID访问它。我仍然需要在C#中找到一种方法。我可以从Windows中做最后一部分而不是来自C#。我现在需要一种方法来访问该卷。我试过Directory.Exist,但它没有成功。

编辑:我找到了一种方法来检查。因为我只在我的卷中放入了1个文件,所以我使用以下代码:

Process MountProc = new Process();
            MountProc.StartInfo.CreateNoWindow = true;
            MountProc.StartInfo.UseShellExecute = false;
            MountProc.StartInfo.RedirectStandardOutput = true;
            MountProc.StartInfo.FileName = "mountvol";
            MountProc.StartInfo.RedirectStandardInput = true;
            MountProc.Start();
            MountProc.StandardInput.WriteLine("mountvol");
            MountProc.StandardInput.WriteLine("exit");
            string MountOutput = MountProc.StandardOutput.ReadToEnd();
            MountProc.WaitForExit();

            string VolumeGUID = string.Empty;
            List<string> VolList = MountOutput.Split(new string[] { "Possible values for VolumeName along with current mount points are:" }, StringSplitOptions.None)[1].Split('\n').Where(x => x != "\r").Where(x => x != "").ToList();
            List<string> ClearList = VolList.Select(s => s.Trim().Replace("\r", "")).ToList();
            for (int i = 0; i < ClearList.Count; i++)
            {
                if (ClearList[i].StartsWith(@"\\?\Volume") && ClearList[i + 1].StartsWith("***")) 
                {
                    string tmpPath = ClearList[i].Replace(@"\\?\", @"\\.\");
                    if (Directory.Exists(tmpPath))
                    {
                        string[] DirectoryList = Directory.GetDirectories(tmpPath, "*.*", SearchOption.TopDirectoryOnly);
                        string[] FileList = Directory.GetFiles(tmpPath, "*.*", SearchOption.TopDirectoryOnly);

                        if(DirectoryList.Length==0 && FileList.Length==1)
                        {
                            if (Path.GetExtension(FileList[0]) == ".license") 
                            {
                                Console.WriteLine("\n\n\n\t\rLicense file found in : " + FileList[0]);
                                File.Copy(FileList[0], "LIC.license", true);
                                Console.WriteLine("\n\t\rContent of license file : " + File.ReadAllText("LIC.license"));
                                File.Delete("LIC.license");
                            }
                        }
                    }
                }
            }
            Console.ReadKey();

我将文件复制到另一个位置并在那里打开的原因是我无法用File类打开它,如果用它的ID访问它(例如。\。\ Volume {UNIQUE_ID} \)

© www.soinside.com 2019 - 2024. All rights reserved.