使用C#使用命令提示符(CMD)执行EXE文件 [英] Executing an EXE file with command Prompt (CMD) using C#

查看:91
本文介绍了使用C#使用命令提示符(CMD)执行EXE文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在
中有一个EXE "C:\\ Program Files \\ Veritas \\ NetBackup \\ Bin \\ admincmd> bpdbjobs.exe"
它写一些文本输出...

现在,我需要获取输出文本并将其写入文本文件.为此,我正在使用此命令
"C:\\ Program Files \\ Veritas \\ NetBackup \\ Bin \\ admincmd> bpdbjobs.exe>> e:\\ ra.txt"


在命令提示符下运行良好.我正在使用此代码执行命令提示符,但无法正常工作....

Hello every one ,

I am having an EXE in
"C:\\Program Files\\Veritas\\NetBackup\\Bin\\admincmd>bpdbjobs.exe"
it writes some text output ...

Now i need to take that output text and write it into a text file . For that i am using this command
"C:\\Program Files\\Veritas\\NetBackup\\Bin\\admincmd>bpdbjobs.exe>> e:\\ra.txt"


It is working well in command prompt. I am using this code to execute the command prompt but it is not working ....

string strCmdText="C:\\Program Files\\Veritas\\NetBackup\\Bin\\admincmd>bpdbjobs.exe>> e:\\ra.txt";
 System.Diagnostics.ProcessStartInfo procStartInfo =
                    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + strCmdText);
                 procStartInfo.RedirectStandardOutput = true;
                 procStartInfo.UseShellExecute = false;
                 procStartInfo.CreateNoWindow = true;
                 System.Diagnostics.Process proc = new System.Diagnostics.Process();
                 proc.StartInfo = procStartInfo;
                 proc.Start();
                 string result = proc.StandardOutput.ReadToEnd();
                 MessageBox.Show(result);



上面显示的代码是在e驱动器中创建文件,但文本文件为空.
我正在C驱动器中运行我的windos应用程序,甚至在消息框中也为空.如果我写 strCmdText ="Md arun",则在我的应用程序的bin \ debug文件夹中创建文件夹 但是我想执行C驱动器中的命令,即使我复制了此项目要c驱动器无法正常工作...

所以任何人都可以建议一些链接或代码或提示.


预先感谢



the above code shown is creating a file in e drive but the text file is empty.
and i am running my windos application in C drive and even in message box is also empty . If I write strCmdText="Md arun" the folder in creating in bin\debug folder of my application But i want to execute command which is in C drive even if i copy this project to c drive it is not working...

so can anybody suggest some link or code or a hint .


Advance thanks

推荐答案

您为什么使用cmd执行该操作?为什么不直接执行它,在代码已经执行的同时捕获输出,并在需要时自行保存该文本?当然这是更简单的方法吗?您所需要做的就是在ProcessStartInfo中将可执行文件的路径而不是"cmd"放入其中,并删除DOS重定向.
Why are you using cmd to execute that? Why not execute it directly, capture the output as your code is doing already, and save that text yourself if you need to? Surely that is the easier way? All you need to them is put the path to your executable in the ProcessStartInfo instead of "cmd" and drop the DOS redirect.


为什么要使用cmd?

只需使用以下代码.希望它能正常工作.
why to use cmd ?

just use the following code. hope it will work fine.
string strCmdText="C:\\Program Files\\Veritas\\NetBackup\\Bin\\admincmd>bpdbjobs.exe>> e:\\ra.txt";
 System.Diagnostics.ProcessStartInfo procStartInfo =
                    new System.Diagnostics.ProcessStartInfo(strCmdText);
>


这篇关于使用C#使用命令提示符(CMD)执行EXE文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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