当运行一个程序使用Process.Start时,它找不到它的资源文件

When running a program using Process.Start, it can't find it's resource files

我有这个代码:

private void button1_Click(object sender, EventArgs e)
{
    Process p = new Process();
    p.StartInfo.FileName = "C:/Users/Valy/Desktop/3dcwrelease/3dcw.exe";
    p.Start();
}

3dcw.exe 是一款用于 OpenGL 图形的应用程序。

问题是当我点击按钮时,可执行文件运行,但无法访问其纹理文件。

有人有解决办法吗?我想像在后台加载位图文件,然后 运行 exe 文件,但我该怎么做?

问题很可能是 3dcw.exe 正在从其当前工作目录中查找文件。当您 运行 一个应用程序使用 Process.Start 时,该应用程序的当前工作目录将默认为 %SYSTEMROOT%\system32。该程序可能需要一个不同的目录,很可能是可执行文件所在的目录。

您可以使用以下代码设置进程的工作目录:

private void button1_Click(object sender, EventArgs e)
{
    string path = "C:/Users/Valy/Desktop/3dcwrelease/3dcw.exe";

    var processStartInfo = new ProcessStartInfo();

    processStartInfo.FileName = path;
    processStartInfo.WorkingDirectory = Path.GetDirectoryName(path);

    Process.Start(processStartInfo);
}

我在 Internet 上搜索了您遇到的问题的解决方案并找到了这个站点:http://www.widecodes.com/0HzqUVPWUX/i-am-lauching-an-opengl-program-exe-file-from-visual-basic-but-the-texture-is-missing-what-is-wrong.html

在 C# 代码中它看起来像这样:

string exepath = @"C:\Users\Valy\Desktopdcwreleasedcw.exe";
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = exepath;
psi.WorkingDirectory = Path.GetDirectoryName(exepath);
Process.Start(psi);