繁体   English   中英

从C#应用程序运行多个命令

[英]Running multiple commands from C# application

我想通过C#应用程序运行一些命令,例如

以前我有一个批处理文件,我使用C#运行了它,但是现在很少有命令可以接受输入。 但是该怎么做呢?

我试过了

 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将显示在运行您的批处理文件或命令中。

使用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();

蝙蝠文件

@echo "staring note pad"
notepad %fileName%

您仍然可以像以前一样制作批处理文件。 它唯一需要的更改就是接受变量。

就像是

 CallYourBatch.bat "MyFileName"

然后在批处理文件中,您可以接受一个参数

SET fileName=%~1
aspnet_regiis -pdf "connectionStrings" %fileName%

同样,如果必须将其作为C#代码的一部分,则可以在形成命令文本时使用相同的功能。

我是否也建议使用CALL命令来调用您的批处理文件? 有关相同内容的更多信息,请访问http://ss64.com/nt/call.html。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM