在C#中运行批处理文件代码 [英] run the batch file code in C#

查看:72
本文介绍了在C#中运行批处理文件代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要从C#运行批处理文件的内容。怎么做?



批处理文件的内容:



echo关闭

\\ server-name \directory\PsExec.exe -u domain\user-id -p ****** -d -e cmd.exe / c \\ \\\server-name \directory\file.exe




我要求在系统中运行带有管理员ID的软件用户没有管理员访问权限。



由于我是初学者,请发布包含所有命名空间详细信息的完整代码。



注意:我不想要一个批处理文件,我想用C#编写所有内容。

解决方案

你可以使用其中一个方法 System.Diagnostics.Process.Start ,例如:



系统.Diagnostics.Process.Start(myBatchFileName); 



 System.Diagnostics.Process.Start(myBatchFileName,myBatchParameters); 





所有参数都是字符串。第一个参数是批处理文件名(不要使用CMD.EXE或其他东西,仅使用批处理文件名),可选的第二个参数是用一个字符串写的批处理文件参数列表,用黑色空格分隔。



您还可以为这些方法返回的子进程分配对该进程的引用。您可以将此变量用于不同目的。特别是,您可以将父进程的调用线程置于等待状态,直到批处理文件使用 Process.Wait 完成执行。



最后,您可以隐藏控制台,将批处理文件的所有输出重定向到流并读取此流的所有文本输出。您需要至少重定向到流: StandardOutput StandardError



请参阅:

http:/ /msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx [ ^ ]。



您可以在这里找到重定向示例: http://msdn.microsoft.com/en-us/library/system.diagnostics。 process.standardoutput.aspx [ ^ ]。



-SA


使用以下代码,它将编写并运行.bat文件。

 System.IO.StreamWriter S W =  new  System.IO.StreamWriter(  test。蝙蝠); 
SW.WriteLine( @ \\server-name\directory\PsExec.exe - u domain\user-id -p ****** -d -e cmd.exe / c \\ server-name \directory\file.exe);
SW.Flush();
SW.Close();
SW.Dispose();
SW = null ;
System.Diagnostics.Process.Start( test.bat);





在字符串前添加@符号以防止由未转义的反斜杠字符引起的错误。[/ edit]


如SA所述,使用 System.Diagnostics.Process namesapce来运行批处理文件。

您需要为Process.Start方法提供文件路径

 Process.Start( //   BatchFileFullPath);  


Hi

I need to run the content of the batch file from C#. How to do that?

Content of the batch file:

echo off
\\server-name\directory\PsExec.exe -u domain\user-id -p ****** -d -e cmd.exe /c \\server-name\directory\file.exe


I require this to run a software with admin id in system where user does'nt have admin access.

Please post the complete code with all the namespace details since I'm a beginner.

Note: i dont want to have a batch file, i want to write everything in C#.

解决方案

You can use one of the methods System.Diagnostics.Process.Start, for example:

System.Diagnostics.Process.Start(myBatchFileName);

or

System.Diagnostics.Process.Start(myBatchFileName, myBatchParameters);



All parameters are strings. First parameter is the batch file name (don't use CMD.EXE or something, use the batch file name only) and the optional second parameter is the list of batch file arguments written in one string, separated with black spaces.

You can also assign the reference to the child process you start as it is returned by these methods. You can use this variable for different purposes. In particular, you can put your calling thread of your parent process to a wait state until the batch file completes its execution using Process.Wait.

Finally, you can hide the console, redirect all the output of your batch file to a stream and read all text output from this stream. You will need to redirect at least to streams: StandardOutput and StandardError.

Please see:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^].

You can find re-direction example here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput.aspx[^].

—SA


Use following code, which will write and run .bat file.

System.IO.StreamWriter SW = new System.IO.StreamWriter("test.bat");
SW.WriteLine(@"\\server-name\directory\PsExec.exe -u domain\user-id -p ****** -d -e cmd.exe /c \\server-name\directory\file.exe");
SW.Flush();
SW.Close();
SW.Dispose();
SW = null;
System.Diagnostics.Process.Start("test.bat");



[edit]Added @ sign in front of string to prevent errors caused by unescaped backslash characters.[/edit]


As SA told, use System.Diagnostics.Process namesapce to runout your batch file.
you need to provide filepath to Process.Start method

Process.Start(//BatchFileFullPath);


这篇关于在C#中运行批处理文件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆