【问题标题】:Process.Start can't find an existing fileProcess.Start 找不到现有文件
【发布时间】:2012-10-31 22:19:55
【问题描述】:

  • 我有一个可执行文件的路径 (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);
    }
}

【问题讨论】:

  • 可能是缺少的文件是必需的依赖项,例如一个DLL。 TestConsole.exe 是否从命令提示符运行?
  • 显示一些代码。这会有所帮助!
  • 好吧,它不起作用,所以代码中可能有问题。我不能只是假设你做对了,它可能非常简单。
  • 还有别的问题。也许path 不是您在执行Process.Start(path) 时所想的那样。向我们展示 Derek 已经问过的完整代码,也许我们可以找到问题所在。您是否也尝试过调试并确保 path 是您所期望的?
  • 您能否在命令提示符下从与 WinForms 应用程序的当前目录相同的目录运行?您是否尝试过运行 cmd /k C:\Test\n4.TestConsole.exe 而不是直接运行?

标签: c# .net file process io


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-09
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多