![](/img/trans.png)
[英]c# Programm using Process.Start(ProcessStartInfo info) does not work
[英]Why does Process.Start(ProcessStartInfo) fail?
我們開發了一個新的WPF應用程序,我很難從外部C#腳本啟動它。
使用ProcessStartInfo
對象調用Process.Start(ProcessStartInfo)
方法時,該對象使用WorkingDirectory
和FileName
成功進行初始化,但init的FileName
屬性僅啟動失敗。
調用任何其他應用程序時不是這種情況。
我的問題 - 啟動過程的不同方法是否有不同的邏輯?
有關詳細信息,請參閱代碼
public void LaunchApp(){
/********************************/
/* This code PASSES */
/********************************/
var pStartInfoCalc1 = new ProcessStartInfo
{
FileName = @"C:\Windows\system32\calc.exe",
};
Process.Start(pStartInfoCalc1);
/*****************************/
/* !!!This code FAILS !!! */
/*****************************/
var pStartInfo1 = new ProcessStartInfo
{
FileName = @"C:\Program Files\MyAppFolder\MyApp.exe",
};
Process.Start(pStartInfo1);
/********************************/
/* This code PASSES */
/********************************/
var pStartInfo2 = new ProcessStartInfo
{
WorkingDirectory = @"C:\Program Files\MyAppFolder",
FileName = @"MyApp.exe",
};
Process.Start(pStartInfo2);
/********************************/
/* This code PASSES */
/********************************/
var pStartInfoCalc2 = new ProcessStartInfo
{
WorkingDirectory = @"C:\Windows\system32\",
FileName = @"calc.exe",
};
Process.Start(pStartInfoCalc2); }`
這是崩潰的圖像:
以下是崩潰屏幕截圖中的問題簽名:
Problem signature:
Problem Event Name: CLR20r3
Problem Signature 01: MyApp.exe
Problem Signature 02: 1.0.0.0
Problem Signature 03: 51ef9fd8
Problem Signature 04: mscorlib
Problem Signature 05: 4.0.30319.18052
Problem Signature 06: 5173bf28
Problem Signature 07: 266d
Problem Signature 08: a4
Problem Signature 09: System.Windows.Markup.XamlParse
OS Version: 6.1.7601.2.1.0.256.4
Locale ID: 1033
Additional Information 1: 1989
Additional Information 2: 1989c043e2e04efdbf18835c58bb867b
Additional Information 3: 37d3
Additional Information 4: 37d31c18f56cf3083b1c45ca83bbb78e
Read our privacy statement online:
http://go.microsoft.com/fwlink/?linkid=104288&clcid=0x0409
If the online privacy statement is not available, please read our privacy statement offline:
C:\Windows\system32\en-US\erofflps.txt
如果未指定工作目錄,則新進程將繼承進程的工作目錄。 也就是說,新進程將繼承調用Process.Start()
的進程的工作目錄。
這是兩次嘗試啟動MyApp
的唯一區別。 其中一個繼承了工作目錄,其中一個指定了它。 顯然, MyApp
不喜歡在初始工作目錄是父進程的目錄的情況下運行。
為什么會這樣,我不能肯定地說。 看起來MyApp
正在嘗試在啟動時進行一些XML解析。 因此,XML解析可能會讀取一個假定在工作目錄中的文件。 但實際上該文件與可執行文件位於同一目錄中。
如果是這種情況,那么您需要修改MyApp
來解決問題。 您需要根據可執行文件的目錄構建絕對路徑,而不是使用此XML文件的相對路徑。
MyApp
啟動代碼可以是該目錄,如下所示:
string ExeDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.
GetExecutingAssembly().Location));
然后,您將使用Path.Combine
來形成XML文件的完整路徑。
如果您不提供工作目錄,它將使用您當前應用程序的目錄。
calc
應用程序沒有任何外部文件依賴性,因此它們並不關心它們的啟動位置。 他們不需要從工作目錄中讀取任何文件。
您的MyApp.exe
很可能需要來自其自己的工作目錄的數據,可能是配置文件。 此測試通過,因為它知道查看C:\\Program Files\\MyAppFolder
/********************************/
/* This code PASSES */
/********************************/
var pStartInfo2 = new ProcessStartInfo
{
WorkingDirectory = @"C:\Program Files\MyAppFolder",
FileName = @"MyApp.exe",
};
Process.Start(pStartInfo2);
當您沒有指定工作目錄時,您的應用程序崩潰了,因為它無法加載所需的資源,因為它試圖在您啟動應用程序的目錄中找到它。
如果您知道的話,最好在啟動應用程序時提供工作目錄。
如果您可以更新MyApp.exe
它可以使用System.Reflection.Assembly.GetExecutingAssembly().Location
來確定它自己的位置,然后您可以讀取相對於此的文件路徑,從而無需設置工作目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.