سؤال

I am having troubles executing a .exe from a path. I have searched google and checked the recommended ones on this topic, however they will not work...

I am trying to run a .exe from a certain path, but it just says it can't find the file?

I am trying this:

const string ex1 = "C:\\";
const string ex2 = "C:\\Desktop\\3D Survival\\3\\Test\\";

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.FileName = "3D Survival.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2;

try
{

    using (Process exeProcess = Process.Start(startInfo))
    {
        exeProcess.WaitForExit();
    }
}
catch
{
    // Log error.
}

And that gives me the file not found too, I have also tried Process.Start("3D Survival.exe"); It still says file not found.. Yes the file does exist.

Does anyone have a fix?

هل كانت مفيدة؟

المحلول

Your code assumes that "3D Survival.exe" exists in the current working directory. You need to provide an absolute path, or a path relative to the working directory.

If you want it to be relative to your executing program, use this:

Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), pathToExe);

To get the current working directory just use:

Directory.GetCurrentDirectory();

Your working directory could be different than you think it is, and this will tell you for sure..

To do an absolute path, just put the whole path into Process.Start, or into the StartInfo.FileName

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top