2017-10-26 4 views
2

Windows 이미지를 설치하는 C# 응용 프로그램이 있습니다. 사용자 인터페이스에서 시스템을 복사 할 디스크 (C : 또는 D : 또는 ...)를 선택해야합니다. 그것을 위해, 그것은 ok 다.C# 및 diskpart : 번호가 아닌 디스크 레이블로 선택하는 방법?

그런 다음 디스크를 포맷해야합니다. 나는 diskpart.exe로 C :와 연결된 좋은 물리적 디스크를 선택해야합니다. 그러나 diskpart를 사용하여 번호가있는 디스크를 선택합니다 : 디스크 0 또는 1 선택 또는

좋은 디스크 번호와 인터페이스에서 사용자가 선택한 문자를 어떻게 연결합니까?

Google에서 아무 것도 발견하지 못했습니다. wmi Win32_DiskDrive으로 정보를 찾으려고했으나 diskpart 세부 디스크와는 아무 공통점이 없습니다.

감사의

+0

코드를 추가 할 수 있습니다. 배열의 요소 색인을 가져 오는 방법 (https://stackoverflow.com/q/4388600/1997232)을 묻는 중입니까? – Sinatr

+0

레이블별로 DISKPART를 사용하여 WinPE에서 디스크를 선택해야합니다. 그러나 나는 이것을 어떻게하는지 모른다. 나는 편지를 가지고 누군가가 이미 라벨이있는 디스크를 선택하려고하는지 알고 싶습니다. – simsim

+0

powershell이나 cmd가 없는데 왜 질문에 powershell과 cmd가 붙어 있습니까? – Clijsters

답변

0

디스크와 논리 드라이브 (C :, D : 등), 당신의 정보를 얻을 수있는 맵하는 : 임의의 순서로

Win32_LogicalDisk 
Win32_LogicalDiskToPartition 
Win32_DiskPartition 

합니다. 이 테이블에는 모든 정보가 있습니다. C#에서 System.Management을 사용하면 쉽게 사용할 수 있습니다.

0

ManagementObjectSearcher을 사용하는 대신에 프로그래밍 방식으로 DiskPart.exe을 사용하고 있지만 내 코드는 정적 솔루션 (정규식의 경우 더 좋음)이 길지만 오랜 시간 동안 작동합니다.

그것은 높은 실행 권한 (새로운 요소> 응용 프로그램 매니페스트 파일을 추가하고 <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />requestedExecutionLevel을 변경 자세한 내용은. : https://stackoverflow.com/a/43941461/5830773)와 매니페스트 파일이 필요합니다

는 그런 다음 DiskPart.exe과 함께 드라이브 목록을 얻으려면 다음 코드를 사용할 수 있습니다 :

// 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]; 
     Console.WriteLine([email protected]"Volume {index} {label}:\"); 
    } 
} 

이 경우 DiskPart에서처럼 다음과 같은 출력을 제공하지만 당신은 당신의 필요에 맞게 사용자 정의 할 수 있습니다

Volume 0 C:\ 
Volume 1 D:\ 
Volume 2 F:\ 
Volume 3 G:\ 
Volume 4 I:\ 
Volume 5 H:\ 
,

지금 드라이브 문자를 검색하는 것은 분명하다 :

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; 
} 

사용법 : 당신은 잊었

Console.WriteLine(GetIndexOfDrive(@"D:\")); // returns 1 on my computer 
+0

도움 주셔서 대단히 감사합니다. 코드를 수정하려고합니다. – simsim

관련 문제