我需要开发一个应用程序(Windows窗体),它可以方便用户更改磁盘字母的操作。 我知道如何从cmd中做到这一点:
我知道如何从c#执行这些命令,但是我想在列表框中显示“List volume”命令的结果,以便用户可以选择他们想要更改字母的音量并按下按钮“改变信件“。
我的问题是如何执行第一项任务,我的意思是,在列表框中显示列表卷的结果。
以下是我的代码:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"C:\Windows\System32\diskpart.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
p.StandardInput.WriteLine("list volume");
string output = p.StandardOutput.ReadToEnd();
运行程序时,我看不到输出的值。 谢谢你的帮助!
答案 0 :(得分:0)
以下代码将返回您的驱动器列表。您的问题有点冗长,但您需要提前执行所有字符串解析以获取此字符串中的驱动器列表。
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();
var op = output.Split(new string[] { "DISKPART>" }, 3, StringSplitOptions.RemoveEmptyEntries)[1].Split(new string[] { Environment.NewLine },10, StringSplitOptions.RemoveEmptyEntries);
答案 1 :(得分:0)
您可以使用drives = DriveInfo.GetDrives().ToList();
//Don't know the name of your listBox.
listBox.ItemsSource = drives;
更轻松地获取当前的驱动器,然后将驱动器添加到listBox很简单:
Button
然后按下要更改driverinfo的private static void Btn_Click(object sender, System.Windows.RoutedEventArgs e)
{
string driveLetter = (string)listBox.SelectedItem;
int index = drives.FindIndex(x => x.Name == driveLetter);
//Perform the work of changing the driveletter using index or driveletter
drives = DriveInfo.GetDrives().ToList();
listBox.ItemsSource = drives;
}
。我不知道你是否需要驱动器或驱动器的索引,因此这段代码可以同时为你提供。
UILabel
我假设您已经知道如何更改驱动器的工作。