从c#应用程序运行多个命令
本文关键字:命令 运行 应用程序 | 更新日期: 2023-09-27 18:10:58
我想通过c#应用程序运行几个命令,如
以前我有一个批处理文件,我使用c#运行它,但现在很少有命令可以接受输入。但是怎么做呢?
I tried
Process cmdprocess = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
Environment.SetEnvironmentVariable("filename", FileName);
startinfo.FileName = @"C:'Users'cnandy'Desktop'St'2nd Sep'New_CN'New folder'Encrypt web.config_RSAWebFarm'Decrypt Connection String.bat";
startinfo.WindowStyle = ProcessWindowStyle.Hidden;
startinfo.CreateNoWindow = true;
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.UseShellExecute = false;
cmdprocess.StartInfo = startinfo;
cmdprocess.Start();
在批处理文件
中cd C:'Windows'Microsoft.NET'Framework64'v4.0.30319
aspnet_regiis -pi "CustomKeys2" "C:'Users'cnandy'Desktop'Encryption_keys'CustomEncryptionKeys.xml"
aspnet_regiis -pa "CustomKeys2" "NT AUTHORITY'NETWORK SERVICE"
aspnet_regiis -pdf "connectionStrings" %filename%
但实际上它们根本没有执行。如何实现相同的最后一个命令,我可以接受输入而不是硬编码
"C:'Users'cnandy'Desktop'Test'Websites'AccountDeduplicationWeb"
?
试试这个:
Process p = new Process()
{
StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
p.Start();
using (StreamWriter sw = p.StandardInput)
{
sw.WriteLine("First command here");
sw.WriteLine("Second command here");
}
p.StandardInput.WriteLine("exit");
或者,尝试这种更直接的方式(它也实现了您最后要求的事情):
string strEntry = ""; // Let the user assign to this string, for example like "C:'Users'cnandy'Desktop'Test'Websites'AccountDeduplicationWeb"
Process p = new Process()
{
StartInfo = new ProcessStartInfo("cmd.exe")
{
RedirectStandardInput = true,
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
}
};
p.Start();
p.StandardInput.WriteLine("cd C:''Windows''Microsoft.NET''Framework64''v4.0.30319");
p.StandardInput.WriteLine("aspnet_regiis -pi '"CustomKeys2'" '"C:''Users''cnandy''Desktop''Encryption_keys''CustomEncryptionKeys.xml'"");
p.StandardInput.WriteLine("aspnet_regiis -pa '"CustomKeys2'" '"NT AUTHORITY''NETWORK SERVICE'"");
p.StandardInput.WriteLine("aspnet_regiis -pdf '"connectionStrings'" " + strEntry);
p.StandardInput.WriteLine("exit"); //or even p.Close();
如果使用第二种方式,建议让用户在文本框中输入路径,然后从文本框的Text属性中获取字符串,其中所有必要的额外反斜杠将自动追加。
应该提到的是,没有cmd将显示运行您的批处理文件,或命令。
您仍然可以像以前一样创建批处理文件。唯一需要改变的是接受变量。
之类的 CallYourBatch.bat "MyFileName"
在你的批处理文件中,你可以接受一个参数
SET fileName=%~1
aspnet_regiis -pdf "connectionStrings" %fileName%
同样,如果你必须在c#代码中使用命令文本,也可以使用相同的功能。
我也可以建议使用CALL命令调用您的批处理文件?更多信息请访问http://ss64.com/nt/call.html
使用Process对象启动批处理文件。您可以使用环境变量在进程之间传递这些值。在这种情况下,从c#到bat文件,你可以使用环境变量传递值。
c#Environment.SetEnvironmentVariable("searchString","*.txt")
在bat文件中,你可以像下面这样访问这个值
dir %searchString%
从c#
启动bat文件System.Diagnostics.Process.Start("path'commands.bat");
从c#中使用批处理文件和变量启动记事本的示例代码
System.Environment.SetEnvironmentVariable("fileName", "test.txt");
System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/commands.bat");
Console.ReadLine();
Bat文件
@echo "staring note pad"
notepad %fileName%