简体   繁体   中英

C# code not starting .exe getting error cannot find file name

I really have no idea why this code isnt working. Everytime i get the error Cannot start process because a file name has not been provided. Even though i provided the path in which the EXE is located and verified it.

using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;

namespace ProcessExitSample
{
    class testsandboxprogram
    {
        static void Main(string[] args)
        {
            Contract.Requires(args != null);
            try
            {
                var firstProc = new Process();
                Process.Start(@"PATH TO EXE I WANT TO LAUNCH");
                firstProc.EnableRaisingEvents = true;

                firstProc.Start();

                firstProc.WaitForExit();

                //so upon exit should run the second program here
                Console.WriteLine("First process exited: " + firstProc.ExitCode);

                var secondProc = new Process();
                Process.Start(@"PATH TO PROGRAM I WANT TO LAUNCH");
                secondProc.Start();

            }
            catch (Exception ex)
            {
                Console.WriteLine("Something went wrong sorry :(: " + ex.Message);
                return;
            }
        }
    }
}
String myexepath = @"C:\Program Files (x86)\Steam\steamapps\common\BattleBlock Theater\BattleBlockTheater.exe"

由于此路径包含,在双引号之间用空格将其括起来:

Process.Start("\""+myexepath+"\"");
            var firstProc = new Process();
            // Process.Start(@"PATH TO EXE I WANT TO LAUNCH");
            firstProc.EnableRaisingEvents = true;
            firstProc.Start();

You do not provide a path for your process to start. I commented out the irrelevant code, since is not related to the firstProc variable.

You probably want:

 firstProc.StartInfo.FileName = @"\Path\To\Exe";

The most obvious is try to run your PATH TO EXE I WANT TO LAUNCH in a command line environment and see if you get a self explanatory error.

If your path contains spaces you will see that your are trying to execute something problematic with spaces and you can then use the answer Graffito gave.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM