[英]How can i run an .exe file with Assembly class
我想用Assembly類運行我的exe文件。 這是我的密碼
Assembly as = Assembly.LoadFile("path");
as.EntryPoint.Invoke(null,null);
錯誤:
參數計數不匹配。
如果是本機exe,請使用Process.Start ,如果是托管(即使用C#創建),則需要加載程序集,然后通過反射調用Main
。
本機exe:
Process.Start("IExplore.exe");
看起來您已經管理了程序集,並且您已經知道Main
入口點(通過Assembly.EntryPoint屬性)。 您需要確保它不為null
(不太可能),並傳遞正確的參數。
Main
簽名是static void Main(string[] args)
或static int Main(string[] args)
或static void Main()
因此您需要在Invoke
傳遞null
並提供正確數量的參數。 如果Main
不帶參數-將new object[0]
用作第二個參數,否則構造帶有參數的字符串數組,然后將其包裝在new object[]{args}
。
Main
接受參數但不對其進行任何處理的情況的樣本:
Assembly as = Assembly.LoadFile(@"c:\temp\my.exe");
as.EntryPoint.Invoke(null, new object[]{new string[0]});
如果要將可執行的.NET程序集加載到自己的進程中,請使用AppDomain.ExecuteAssembly
:
int exitCode = AppDomain.Current.ExecuteAssembly("path");
如果您具有程序集名稱而不是路徑,那么還將存在ExecuteAssemblyByName
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.