我正在尝试创建将从cmd.exe执行命令的应用程序,但我不确定如何在命令行中从用户传递其他参数(密码)。我怎么能以编程方式做到这一点?
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.RedirectStandardInput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
using (StreamWriter sw = process.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("/c " + command); // run application
sw.WriteLine("/c mypassword"); // enter first parameter in application
}
}
现在它执行如下:
答案 0 :(得分:1)
add startInfo.Arguments = arguments
它只是一个字符串。这只是7za的一个例子:
string arguments = "-mx3 -m0=LZMA2";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //hides the window
startInfo.FileName = sevenZip;
startInfo.Arguments = arguments;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
答案 1 :(得分:0)
您只需要将Arguments属性添加到ProcessStartInfo对象中。 下面的代码就像魅力......
var startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Program Files\Internet Explorer\iexplore.exe";
startInfo.Arguments = "http://www.google.com";
var process = new Process();
process.StartInfo = startInfo;
process.Start();