【发布时间】:2016-07-07 18:56:51
【问题描述】:
我们开发了一个新的 WPF 应用程序,但我在从外部 C# 脚本启动它时遇到了困难。
在使用ProcessStartInfo 对象调用Process.Start(ProcessStartInfo) 方法时,该对象使用WorkingDirectory 和FileName 成功初始化,但初始化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
【问题讨论】:
-
我认为 Program Files 文件夹默认受操作系统保护。
-
一个合理的第一个猜测是:有问题的应用程序希望在其工作目录中找到一个特定文件——如果没有,它会因为错误处理不当而崩溃。
-
UseShellExecute的值是多少UseShellExecute为false时,不使用WorkingDirectory属性查找可执行文件 -
您需要修复代码中的两个错误。错误 #1 使用的文件名只是名称,如 baz.ext,而不是完整路径,如 c:\foo\bar\baz.ext。错误 #2 忘记为 AppDomain.CurrentDomain.UnhandledException 编写事件处理程序,这是必要的,这样您就可以找出程序崩溃的原因。
-
我说的是您的 WPF 程序,而不是您在此处发布的代码。