gpt4 book ai didi

c# - 为什么 Process.Start(ProcessStartInfo) 会失败?

转载 作者:行者123 更新时间:2023-11-30 13:31:53 24 4
gpt4 key购买 nike

我们开发了一个新的 WPF 应用程序,但我很难从外部 C# 脚本启动它。

使用 ProcessStartInfo 对象调用 Process.Start(ProcessStartInfo) 方法时,该对象由 WorkingDirectoryFileName 初始化> 成功,初始化 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); }`

这是崩溃时的图像: enter image description here

以下是崩溃屏幕截图中的问题签名:

 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 文件的完整路径。

关于c# - 为什么 Process.Start(ProcessStartInfo) 会失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17857225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com