简体   繁体   English

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

[英]Running multiple commands from C# application

I want to run several commands via C# application like 我想通过C#应用程序运行一些命令,例如

Previously I had a batch file and I ran it using C# but now few of the commands can take inputs. 以前我有一个批处理文件,我使用C#运行了它,但是现在很少有命令可以接受输入。 But how to do that ? 但是该怎么做呢?

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

And in the batch file 并在批处理文件中

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%

But effectively they did not run get executed at all. 但实际上,它们根本没有运行就可以执行。 How to achieve the same where for the last command I can accept an input instead of hard coding 如何在最后一条命令可以接受输入而不是硬编码的地方实现相同的功能

"C:\Users\cnandy\Desktop\Test\Websites\AccountDeduplicationWeb"

?

Try this: 尝试这个:

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");

Alternatively, try this more direct way (which also implements the last thing you requested): 或者,尝试这种更直接的方法(它也实现了您要求的最后一件事):

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

If using the second way, it is recommended to let the user enter the path in a textbox, then grab the string from the textbox's Text property, where all the necessary extra back-slashes will be automatically appended. 如果使用第二种方法,建议让用户在文本框中输入路径,然后从文本框的Text属性中获取字符串,在该字符串中,所有必要的额外反斜杠将自动附加。

It should be mentioned that no cmd will show up running your batch file, or the commands. 应该提到的是,没有cmd将显示在运行您的批处理文件或命令中。

Start the batch file using Process object. 使用Process对象启动批处理文件。 You can use Environment variables to pass the values between processes. 您可以使用环境变量在进程之间传递值。 in this case, from C# to bat file you can pass values using environment variable. 在这种情况下,从C#到bat文件,您可以使用环境变量传递值。

c# C#

Environment.SetEnvironmentVariable("searchString","*.txt")

in bat file you can access the value as like below 在bat文件中,您可以如下访问值

dir %searchString%

To start the bat file from c# 从C#启动bat文件

System.Diagnostics.Process.Start("path\commands.bat");

Sample code to start notepad from C# with Batch file and Variables 使用批处理文件和变量从C#启动记事本的示例代码

System.Environment.SetEnvironmentVariable("fileName", "test.txt");
System.Diagnostics.Process.Start(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "/commands.bat");
Console.ReadLine();

Bat file 蝙蝠文件

@echo "staring note pad"
notepad %fileName%

You can still make a batch file as you used to. 您仍然可以像以前一样制作批处理文件。 Only change it needs is accepting variables. 它唯一需要的更改就是接受变量。

Something like 就像是

 CallYourBatch.bat "MyFileName"

Then in you batch file, you can accept a parameter 然后在批处理文件中,您可以接受一个参数

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

Similarly the same functionality can be used while forming your command text, if you must do it as part of C# code. 同样,如果必须将其作为C#代码的一部分,则可以在形成命令文本时使用相同的功能。

Also might i suggest using a CALL command to call your batch files? 我是否也建议使用CALL命令来调用您的批处理文件? More information on the same is at http://ss64.com/nt/call.html 有关相同内容的更多信息,请访问http://ss64.com/nt/call.html。

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

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