C# 調(diào)用命令行執(zhí)行Cmd命令的操作
1、不知道為啥
process.StartInfo.Arguments = "/c" + "start D:/Tim/Bin/QQScLauncher.exe";
這個執(zhí)行命令一定要加/c ,/c ,/c,重要的事說3遍 才能正常編譯并運行
cmd /c dir:是執(zhí)行完dir命令后關(guān)閉命令窗口;
cmd /k dir:是執(zhí)行完dir命令后不關(guān)閉命令窗口。
process.StartInfo.Arguments 我猜測這個調(diào)用的是第一張圖的窗口,而不是二圖的窗口

代碼:
static void LaunchCommandLineApp()
{
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c" + "start D:/Tim/Bin/QQScLauncher.exe";
process.StartInfo.UseShellExecute = false; //是否使用操作系統(tǒng)shell啟動
process.StartInfo.CreateNoWindow = false; //是否在新窗口中啟動該進程的值 (不顯示程序窗口)
process.Start();
process.WaitForExit(); //等待程序執(zhí)行完退出進程
process.Close();
}
補充:C# 執(zhí)行指定命令和執(zhí)行cmd命令
通常需要在程序執(zhí)行過程中調(diào)用CMD命令并獲取信息,
以下方法實現(xiàn)了該功能
/// <summary>
/// 執(zhí)行內(nèi)部命令(cmd.exe 中的命令)
/// </summary>
/// <param name="cmdline">命令行</param>
/// <returns>執(zhí)行結(jié)果</returns>
public static string ExecuteInCmd(string cmdline)
{
using (var process = new Process())
{
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.AutoFlush = true;
process.StandardInput.WriteLine(cmdline + "&exit");
//獲取cmd窗口的輸出信息
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
return output;
}
}
以下方法實現(xiàn)了調(diào)用第三方實現(xiàn)的命令
/// <summary>
/// 執(zhí)行外部命令
/// </summary>
/// <param name="argument">命令參數(shù)</param>
/// <param name="application">命令程序路徑</param>
/// <returns>執(zhí)行結(jié)果</returns>
public static string ExecuteOutCmd(string argument, string applocaltion)
{
using (var process = new Process())
{
process.StartInfo.Arguments = argument;
process.StartInfo.FileName = applocaltion;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.AutoFlush = true;
process.StandardInput.WriteLine("exit");
//獲取cmd窗口的輸出信息
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.Close();
return output;
}
}
ProcessCore.ExecuteInCmd("ipconfig");
ProcessCore.ExecuteOutCmd("-I http://www.baidu.com", @"C:\curl.exe");
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
C#中使用JSON.NET實現(xiàn)JSON、XML相互轉(zhuǎn)換
這篇文章主要介紹了C#中使用JSON.NET實現(xiàn)JSON、XML相互轉(zhuǎn)換的相關(guān)代碼及示例,需要的朋友可以參考下2015-11-11
DataGridView設置單元格的提示內(nèi)容ToolTip
這篇文章介紹了DataGridView設置單元格提示內(nèi)容ToolTip的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02
C#讀取數(shù)據(jù)庫返回泛型集合詳解(DataSetToList)
本篇文章主要是對C#讀取數(shù)據(jù)庫返回泛型集合(DataSetToList)進行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01

