Process.Start 找不到现有的文件。

5
我有一个可执行文件的路径(C:\Test\n4.TestConsole.exe)。
File.Exists(path) 返回 true
File.OpenRead(path) 没有问题地获取了它的流。
Process.Start(path) 抛出了一个 System.ComponentModel.Win32Exception,消息如下:

系统找不到指定的文件。

我做错了什么?

Windows 8 Professional x64 - .NET Framework 4.5


编辑: 这是代码。

public partial class Form1 : Form
{
    public string Path { get; set; }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // I put a breakpoint here and verify the Path's value is
        // C:\Test\n4.TestConsole.exe.

        // File.Exists returns true.
        MessageBox.Show(File.Exists(Path));

        // File.OpenRead doesn't throw an exception.
        using (var stream = File.OpenRead(Path)) { }

        // This throws the exception.
        Process.Start(Path);
    }
}

5
可能缺失的文件是必要的依赖项,例如 DLL。TestConsole.exe 是否可以从命令提示符中运行? - Rich Tebb
1
展示一些代码。那会有所帮助! - Derek
1
好的,它不起作用,所以代码可能有问题。我不能假设你是正确的,可能只是一些非常简单的问题。 - Derek
1
还有其他问题。也许在执行Process.Start(path)时,path不是你想象的那样。像Derek已经要求的那样,向我们展示完整的代码,也许我们可以找到问题所在。你是否尝试过调试并确保path符合你的预期? - Shadow The Spring Wizard
1
你能否在与WinForms应用程序当前目录相同的目录中从命令提示符运行?您是否尝试过运行cmd /k C:\ Test \ n4.TestConsole.exe而不是直接运行? - Rich Tebb
显示剩余15条评论
2个回答

2

可能是缺少DLL或其他依赖项。您可以比较使用Process.Start(exe_path)直接运行和使用Process.Start("cmd", "/k " + exe_path)运行时的PATH环境变量。


1

试试这个:

private void button1_Click(object sender, EventArgs e)
{
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.WorkingDirectory = @"C:\Test";
    psi.FileName = "n4.TestConsole.exe";
    Process.Start(psi);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接