从我之前的问题中,我正在编写一个通过 CMD 执行多个文件的程序。
这是我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
namespace Convert
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
private void BtnSelect_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog Open = new OpenFileDialog();
Open.Filter = "RIFF/RIFX (*.Wav)|*.wav";
Open.CheckFileExists = true;
Open.Multiselect = true;
Open.ShowDialog();
LstFile.Items.Clear();
foreach (string file in Open.FileNames)
{
LstFile.Items.Add(file);
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
LstFile.Items.Clear();
}
private void BtnConvert_Click(object sender, RoutedEventArgs e)
{ Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.UseShellExecute = false;
foreach (string fn in LstFile.Items)
{
string fil = "\"";
string gn = fil + fn + fil;
p.Start();
p.StartInfo.Arguments = gn;
}
}
}
}
我用过
string fil = "\"";
string gn = fil + fn + fil;
在完整文件名周围添加引号,以防文件名包含空格。
我的问题是我的程序 Opens CMD Put 没有在其中传递任何参数。我检查了 filnames(list) 是否正常工作并且它们没有问题。查看示例,这是执行此操作的方法,但显然有些问题
设置
StartInfo.Arguements
在开始流程之前,我建议为您启动的每个流程创建一个新的流程类。
示例:
foreach (string fn in LstFile.Items)
{
string fil = "\"";
string gn = fil + fn + fil;
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = gn;
//You can do other stuff with p.StartInfo such as redirecting the output
p.Start();
// i'd suggest adding p to a list or calling p.WaitForExit();,
//depending on your needs.
}
如果你想调用cmd命令,请提出你的观点
"/c \"what i would type into the command Line\""
这是我快速完成的一个例子。它在记事本中打开文本文档
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = "/c \"New Text Document.txt\"";
p.Start();
p.WaitForExit();