如何在c#中执行命令 [英] how to execute command in c#

查看:86
本文介绍了如何在c#中执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个字符串

I have two strings

string OrginalCreateTransformMsi = "D:\\Desktop\\Jonty-transform creator\\MSITransformTool.exe";
string strMSIName = "D:\\Desktop\\SetUps\\Orca.Msi";



我想通过命令提示符运行这个


I want to run this through command prompt

private void ExecuteCommandLine(string strCMDText)
{
    //System.Diagnostics.Process.Start("CMD.exe", strCmdText);
    string tempGETCMD = null;
    Process CMDprocess = new Process();
    ProcessStartInfo StartInfo = new ProcessStartInfo();
    StartInfo.FileName = "cmd"; //starts cmd window
    //StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    StartInfo.CreateNoWindow = true;
    StartInfo.RedirectStandardInput = true;
    StartInfo.RedirectStandardOutput = true;
    StartInfo.UseShellExecute = false; //required to redirect
    CMDprocess.StartInfo = StartInfo;
    CMDprocess.Start();
    StreamReader SR = CMDprocess.StandardOutput;
    StreamWriter SW = CMDprocess.StandardInput;
    SW.WriteLine("@echo on");
    SW.WriteLine(strCMDText); //the command you wish to run.....
    //SW.WriteLine("cd C:\\Program Files");
    //insert your other commands here
    SW.WriteLine("exit"); //exits command prompt window
    tempGETCMD = SR.ReadToEnd(); //returns results of the command window
    SW.Close();
    SR.Close();
}

ExecuteCommandLine(OrginalCreateTransformMsi +" " + strMSIName);



i需要在第一个字符串之间添加一个额外的字符串OrginalCreateTransformMsi,

我们还有其他任何方法吗?



请协助


i need to add an extra " between first string OrginalCreateTransformMsi ,
do do we have any other methods ?

Please assist

推荐答案

只需调用 Process.Start() [ ^ ]:

You can execute the command simply by calling Process.Start()[^]:
Process.Start(OrginalCreateTransformMsi, strMSIName);


这篇关于如何在c#中执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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