【问题标题】:How to create simple volume from unallocated partition如何从未分配的分区创建简单卷
【发布时间】:2019-07-05 18:26:24
【问题描述】:

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

到目前为止,我已经尝试过 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 命令来实现这一目标吗? 以及如何获得有关此卷的详细信息?

【问题讨论】:

  • 我的磁盘是这样的:prntscr.com/mjwn4g 我不知道那个未分配的部分是从哪里来的,但我不能删除它。无论如何,当我使用您在链接中提供的方法时,我的磁盘将转换为此 prntscr.com/mjwnf6 我的问题是,如果您在没有未分配磁盘的磁盘中执行此操作会发生什么?当我使用自己的方法并缩小主分区时,我的磁盘看起来像这样prntscr.com/mjwo0z,但在这种情况下我还有另一个问题。当我像这样缩小并使用您拥有的方法时,它仍在使用第一个未分配的部分而不是我创建的那个。

标签: c# cmd disk-partitioning


【解决方案1】:

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

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();

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多