C#中呼叫外部exe的使用、呼叫powershell、退出程序、委託和事件
針對幾種情況,簡單總結學習一下
竟然找到一個超級簡單的方法呼叫exe檔案
程式碼就一句,並且呼叫的效果特別好。加上後面的.WaitForExit()就是為了等呼叫的程式執行完後再執行下面的程式碼,沒有後面的.WaitForExit() 無需等待程式完成後執行程式碼。
System.Diagnostics.Process.Start(Application.StartupPath + “你的應用程式.exe”);
System.Diagnostics.Process.Start(".\\CameraTool\\CameraPreview.exe").WaitForExit();
目前有一個工具需要cmd視窗開啟工具介面,工具中無法關閉自身,必須關掉cmd窗口才可以。在C#中呼叫工具,cmd視窗未出現。
使用bat,批處理命令,可以同時開啟cmd視窗和工具介面,C#再呼叫此bat.
呼叫的exe實時顯示在視窗的,可以採用bat的方式來實現。
System.Diagnostics.Process類:用來啟動和停止程序的。
1、
Process pr = new Process();//宣告一個程序類物件 process.StartInfo.FileName = "C:\\Keil_v5\\UV4\\UV4.exe"; process.Start();
還可以簡單點:Process的靜態方法Start();
Process.Start(String fileName);(+4過載) //filiName 是你要執行的程式名,是物理路徑
Process.Start(String fileName,string arguments)//filiName 是你要執行的程式名,是物理路徑;arguments啟動改程式時傳遞的命令列引數
各個引數的定義
Process類的StartInfo屬性包含了一些程序啟動資訊,其中比較重要的幾個
FileName 可執行程式檔名
Arguments 程式引數,已字串形式輸入
CreateNoWindow 是否不需要建立視窗
UseShellExecute 是否需要系統shell呼叫程式
System.Diagnostics.Process p= new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; //是否使用作業系統shell啟動 p.StartInfo.RedirectStandardInput = true;//接受來自呼叫程式的輸入資訊 p.StartInfo.RedirectStandardOutput = true;//由呼叫程式獲取輸出資訊 p.StartInfo.RedirectStandardError = true;//重定向標準錯誤輸出 p.StartInfo.CreateNoWindow = true;//不顯示程式視窗 p.Start();//啟動程式
2、呼叫powershell
Process p = new Process();
p.StartInfo.FileName = "PowerShell.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.Verb = "runas";
p.StartInfo.CreateNoWindow = false;
p.Start();
p.StandardInput.WriteLine("set-ExecutionPolicy RemoteSigned");
p.StandardInput.WriteLine("y");
p.StandardInput.WriteLine("cd SVC_Tool_1.0.0.0_Master_Test");
p.StandardInput.WriteLine(".\\Add-AppDevPackage.ps1");
p.StandardInput.AutoFlush = true;
p.WaitForExit();
p.Close();
3、退出程序
若想關閉主程式的同時也關閉呼叫的程式,需要在退出程式的按鈕事件中新增如下程式碼:
Process[] pProcess;
pProcess = Process.GetProcesses();
for (int i = 1; i <= pProcess.Length - 1; i++)
{
if (pProcess[i].ProcessName == "應用程式名字") //工作管理員應用程式的名
{
pProcess[i].Kill();
break;
}
}
4、事件和委託
using System;
using System.Collections.Generic;
using System.Text;
namespace Delegate {
// 熱水器
public class Heater {
private int temperature;
public delegate void BoilHandler(int param); //宣告委託
public event BoilHandler BoilEvent; //宣告事件
// 燒水
public void BoilWater() {
for (int i = 0; i <= 100; i++) {
temperature = i;
if (temperature > 95) {
if (BoilEvent != null) { //如果有物件註冊
BoilEvent(temperature); //呼叫所有註冊物件的方法
}
}
}
}
}
// 警報器
public class Alarm {
public void MakeAlert(int param) {
Console.WriteLine("Alarm:嘀嘀嘀,水已經 {0} 度了:", param);
}
}
// 顯示器
public class Display {
public static void ShowMsg(int param) { //靜態方法
Console.WriteLine("Display:水快燒開了,當前溫度:{0}度。", param);
}
}
class Program {
static void Main() {
Heater heater = new Heater();
Alarm alarm = new Alarm();
heater.BoilEvent += alarm.MakeAlert; //註冊方法
heater.BoilEvent += (new Alarm()).MakeAlert; //給匿名物件註冊方法
heater.BoilEvent += Display.ShowMsg; //註冊靜態方法
heater.BoilWater(); //燒水,會自動呼叫註冊過物件的方法
}
}
}
輸出為:
Alarm:嘀嘀嘀,水已經 96 度了:
Alarm:嘀嘀嘀,水已經 96 度了:
Display:水快燒開了,當前溫度:96度。
總結:簡單總結一下就是,宣告一個委託和事件,委託和呼叫的方法返回值和引數相同。在需要呼叫事件的方法中,呼叫事件。在程式執行中,別忘記註冊事件。
再舉個使用過的例子:
宣告委託和事件
/// <summary>
/// 事件,資料到達,處理此事件以接受輸入資料
/// </summary>
public event EventHandler<byte[]> GetFeatureDataReceived;
protected virtual void OnGetFeatureDataReceived(byte[] e)
{
if (GetFeatureDataReceived!=null)
{
GetFeatureDataReceived(this,e);
}
}
註冊事件(主程式執行入口程式碼)
public FormMain()
{
InitializeComponent();
//getfeature 得到的資料進行處理
myHid.GetFeatureDataReceived += new EventHandler<byte[] >(myHID_received_packet_management);
}
呼叫事件
public HID_RETURN Get_Feature(report r)
{
if (deviceOpened)
{
try
{
byte[] buffer = new byte[featureReportLength];
buffer[0] = r.reportID;
int maxBufferLength = 0;
if (r.reportBuff.Length < featureReportLength - 1)
{
maxBufferLength = r.reportBuff.Length;
}
else
{
maxBufferLength = featureReportLength - 1;
}
for (int i = 1; i <= maxBufferLength; i++)
{
buffer[i] = r.reportBuff[i - 1];
}
if (HidD_GetFeature(device, buffer, featureReportLength))
{
OnGetFeatureDataReceived(buffer);
return HID_RETURN.SUCCESS;
}
else
{
return HID_RETURN.SET_FEATURE_FAILED;
}
}
catch
{
EventArgs ex = new EventArgs();
OnDeviceRemoved(ex);
return HID_RETURN.SET_FEATURE_FAILED;
}
}
return HID_RETURN.SET_FEATURE_FAILED;
}
定義使用委託的方法(按照正常定義方法的方式)
//Dealing with the received data packet
private void myHID_received_packet_management(object sender, byte[] e)
{
received_packet = e;
}
...
相關推薦
C#中呼叫外部exe的使用、呼叫powershell、退出程序、委託和事件
針對幾種情況,簡單總結學習一下竟然找到一個超級簡單的方法呼叫exe檔案程式碼就一句,並且呼叫的效果特別好。加上後面的.WaitForExit()就是為了等呼叫的程式執行完後再執行下面的程式碼,沒有後面的.WaitForExit() 無需等待程式完成後執行程式碼。System.
C#使用WinAPI中 WinExec 呼叫外部exe程式
1、新增引用 using System.Runtime.InteropServices;//呼叫外部的DLL 2、定義win32API //呼叫exe [DllImport("kernel32.dll")] public static extern int WinExec(string
C# 呼叫外部exe程式
有時候dll不能引用,那就只能另外做一個exe程式,然後通過呼叫這個程式就可以解決問題,但往往需要在本地生成一箇中間資料。雖然有name一點麻煩,但也挺好用。 這裡就是一個呼叫外部程式的方法。 /// <summary> /// 通過程序呼叫外部程式 /// <
C++小知識(八)——C/C++執行外部程式(呼叫外部exe程式)
轉載自:https://blog.csdn.net/dongpanshan/article/details/7835722,其中我只用到了ShellExecute函式,根據下面的示例以及介紹的其中的引數進行了調整,實現了對外部exe的呼叫 C中的函式: 1.exec()
C#程式呼叫外部exe程式(轉)
連結:https://www.cnblogs.com/BookCode/p/5329890.html 在編寫程式時經常會使用到呼叫可執行程式的情況,本文將簡單介紹C#呼叫exe的方法。在C#中,通過Process類來進行程序操作。 Process類在System.Diagnostics包中。
C#使用Process類呼叫外部exe程式
在C#中,通過Process 類來進行程序操作。 Process類在System.Diagnostics包中。 【System.Diagnostics.Process.Start("calc.exe");】執行計算器 示例一 using System.Diagno
C#如何呼叫外部exe程式
http://blog.csdn.net/cyamazing/article/details/52849106 1.在主介面新增呼叫外部exe程式的Button,雙擊此Button進入程式碼編輯頁面。 在程式的開始using System.Diagnostics; 然後在初始化函式的上方定義private
C++以阻塞的方式呼叫外部exe程式,等待其執行結束
SHELLEXECUTEINFO ShExecInfo = {0}; ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
C#如何呼叫外部exe程式 (轉)
1.在主介面新增呼叫外部exe程式的Button,雙擊此Button進入程式碼編輯頁面。 在程式的開始using System.Diagnostics; 然後在初始化函式的上方定義private static System.Diagnostics.Process p; 2.
Unity呼叫外部EXE和啟動瀏覽器(手機端也可以啟動IE核心)
1、呼叫外部瀏覽器 using System.Diagnostics; void OnGUI() { if (GUI.Button(new Rect(50, 50, 100, 30), “Click”)) { Process.Start(“IExplore.ex
C#中實現 子窗體控制呼叫父窗體成員和控制元件
因專案需要,我要在一個子窗體form6中呼叫建立它的父窗體form4的一個combobox的方法和屬性。 網上搜集的,沒有統一答案,試驗了下。 可行方法如下: 在form6的class類定義中加入一個私有成員變數,用來記錄父窗體; private form M; 在f
C++中,巢狀類呼叫外部類函式例子
class nestedClass // 內部類 { public: nestedClass() { testNestedClass(); } voi
C++中多繼承建構函式呼叫順序
class B1 {public: B1(int i) {cout<<"consB1"<<i<<endl;} };//定義基類B1 class B2 {public: B2(int j) {cout<<"consB2"<<
C++ 中拷貝建構函式被呼叫情況
1、當用類的一個物件初始化該類的另一個物件時.例如: Int main(){ Point A; Point B(A);// } 2 如果函式的形參是類的物件,呼叫函式時,進行形參和實參結合時. void fun(Point P){ } int
java呼叫外部exe程式並傳參
其實很簡單, 首先編寫一個命令command, 如g++編譯.c程式的命令為: g++ -o test.exe test.c Runtime.getRuntime()得到當前執行是環境, 然後呼叫它的exec(cmd)方法, 該方法返回一個程序process 檢視p
c# wpf啟動外部exe並嵌入到啟動主視窗中執行
本文主要介紹通過c#來啟動外部exe(第三方軟體)並嵌入到主視窗執行,在自己的應用程式中執行 一、啟動外部exe相對簡單 IntPtr hcalc; //用以儲存目標視窗控制代碼 ProcessStartInfo ps = new ProcessStartInfo();
Matlab程式呼叫外部exe(可執行檔案)方法及引數傳遞
如何進行matlab和c程式碼聯合除錯, 請見下帖: 一般情況,exe檔案都是一些獨立執行的程式,不需要涉及其他輸入輸出。但是有時我們的確需要!下面我們就對這個問題分析下!其實,將引數傳遞給一個由matlab建立的exe,與將引數傳遞給其它任何平臺的應用程式一樣下面
[.Net碼農].net呼叫外部exe檔案
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text
asp.net呼叫外部exe程式
想想asp.net學習的時間最長了,竟然沒寫一篇日誌,,今日找份原來的筆記補上。 static void Main() { //宣告一個程式資訊類 Sys
c++中使用外部庫exe,.dll與.lib檔案的生成與使用的詳解
兩種庫: • 包含了函式所在的DLL檔案和檔案中函式位置的資訊(入口),程式碼由執行時載入在程序空間中的DLL提供,稱為動態連結庫dynamic link library。 • 包含函式程式碼本身,在編譯時直接將程式碼加入程式當中,稱為靜態連結庫static li