简体   繁体   English

在C#中将字符串作为批处理文件运行

[英]Running string as a Batch file in c#

I'm writing an application that creates a batch file and then run : 我正在编写一个创建批处理文件然后运行的应用程序:

I know I can create a Batch file and run it no problem with that . 我知道我可以创建一个批处理文件并运行该文件。

What I want to do is : once I have created the string that makes the file , Is there is any way to execute the string as a Batch file ? 我想做的是:一旦创建了构成文件的字符串,是否有任何方法可以将字符串作为批处理文件执行? something like 就像是

string BatchFile = "echo \"bla bla\" \n iperf -c 123  ... ... .. "
Diagnostics.Process.Start(BatchFile);

您可以使用/ c作为可执行文件来运行CMD.EXE ,并将其余部分作为参数:

Process.Start("cmd.exe", "/c echo \"bla bla\" \n iperf -c 123  ... ... .. ");

for me, I am using this code: 对我来说,我正在使用以下代码:

Process process;
        private void button1_Click(object sender, EventArgs e)
        {
            process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.FileName = "cmd";
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardInput = true;
            process.Start();
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            worker.RunWorkerAsync();

            process.StandardInput.WriteLine("cd d:/tempo" );
            process.StandardInput.WriteLine("dir");



        }
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            string line;
            while (!process.StandardOutput.EndOfStream)
            {
                line = process.StandardOutput.ReadLine();
                if (!string.IsNullOrEmpty(line))
                {
                    SetText(line);
                }
            }
        }

        delegate void SetTextCallback(string text);
        private void SetText(string text)
        {
            if (this.textBox1.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.textBox1.Text += text + Environment.NewLine;
            }
        }
        private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            process.StandardInput.WriteLine("exit");
            process.Close();

        }

You may create your Batch "file" as a long string with lines terminated in \\n , exactly as you shown in your example, and then execute that string (I called it a "NotBatch-text") executing cmd.exe and redirecting such string into its Stdin standard handle. 您可以将您的批处理“文件”创建为长字符串,并以\\n终止行,这与您在示例中显示的完全相同,然后执行cmd.exe并执行该字符串重定向(我称其为“ NotBatch-text”),并重定向串入其Stdin标准手柄。 This way, your "NotBatch-text" may use a large number of Batch features, like expansion of %variables%, IF and FOR commands nested at any level, and many more. 这样,您的“ NotBatch文本”可能会使用大量的批处理功能,例如%variables%的扩展,嵌套在任何级别的IFFOR命令等等。 You may also use delayed !variable! 您还可以使用延迟!变量! expansion if you execute cmd.exe with /V:ON switch. 如果使用/V:ON开关执行cmd.exe,则扩展。 Really, the only things that don't works in the NotBatch-text are: parameters and SHIFT command, and GOTO / CALL :label commands; 实际上,在NotBatch文本中唯一不起作用的是:参数和SHIFT命令以及GOTO / CALL :label命令; further details at this post . 这个职位的进一步细节。

If you want to execute a more advanced "NotBatch-text" string, you may even simulate GOTO and CALL :label commands with the aid of a third party program, as described at this post . 如果要执行更高级的“NotBatch文本”的字符串,你甚至可以模拟 GOTOCALL :label与第三方程序的帮助下命令,如在描述这个职位

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

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