0

有谁知道为什么以下将打开 Kool.exe 但是 Kool.exe 无法加载其所有文件,除非我将当前项目 debug/project.exe 放入与它试图打开的 Kool.exe 相同的文件夹中?

private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog openF1 = new OpenFileDialog();

    openF1.InitialDirectory = @"C:\";
    openF1.Title = "Browse for Kool.exe...";
    openF1.CheckFileExists = true;
    openF1.CheckPathExists = true;
    openF1.DefaultExt = "exe";
    openF1.FileName = "Kool";
    openF1.Filter = "Kool (*.exe)|*.exe|All Files(*.*)|*.*";
    openF1.FilterIndex = 2;
    openF1.RestoreDirectory = true;
    openF1.ReadOnlyChecked = true;
    openF1.ShowReadOnly = true;

    if (openF1.ShowDialog() == DialogResult.OK)
    {
        Process[] pname = Process.GetProcessesByName(openF1.FileName);
        if (pname.Length == 0)
        {
            Process.Start(openF1.FileName);
            this.Close();
        }
        else
        {
            MessageBox.Show("Kool is already running.", "Patch: Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
        }
    }
    else
    {
        MessageBox.Show("Cannot find Kool install", "Patch: Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
    }
}

其他:我以管理员身份运行该应用程序。

4

1 回答 1

5

首先,我怀疑这与文件打开对话框有什么关系。

我强烈怀疑问题在于 Kool.exe 假定它需要的文件位于当前工作目录中,而不是尝试相对于可执行文件本身查找它们。

理想情况下,您应该修复 Kool.exe 使其更具弹性 - 但如果这不可能,只需在启动时设置新进程的工作目录。

string file = openF1.FileName;
string directory = new FileInfo(file).Directory;
Process.Start(new ProcessStartInfo {
    FileName = file,
    WorkingDirectory = directory
});
于 2013-05-23T01:29:23.150 回答