Reputation: 248
Does anyone know why the following will Open the Kool.exe however the Kool.exe cannot load all of its files unless i place the current projects debug/project.exe into the same folder as the Kool.exe it is trying to open?
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);
}
}
Other: I am running the application as an administrator.
Upvotes: 0
Views: 1246
Reputation: 1501926
Firstly, I doubt that this has anything to do with the file open dialog.
I strongly suspect that the problem is that Kool.exe assumes that the files it needs are in the current working directory, instead of trying to find them relative to the executable file itself.
Ideally, you should fix Kool.exe to be more resilient - but if that's not possible, just set the new process's working directory when you start it.
string file = openF1.FileName;
string directory = new FileInfo(file).Directory;
Process.Start(new ProcessStartInfo {
FileName = file,
WorkingDirectory = directory
});
Upvotes: 5