运行程序时使用Process.开始时,它无法找到它的资源文件

本文关键字:源文件 资源 程序 开始时 Process 运行 | 更新日期: 2023-09-27 18:01:44

我有这样的代码:

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文件,但我怎么能做到这一点?

运行程序时使用Process.开始时,它无法找到它的资源文件

我在互联网上搜索了您的问题的解决方案,并找到了这个网站: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'Desktop'3dcwrelease'3dcw.exe";
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = exepath;
psi.WorkingDirectory = Path.GetDirectoryName(exepath);
Process.Start(psi);

问题很可能是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);
}