简体   繁体   English

C# 代码未启动 .exe 出现错误找不到文件名

[英]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.即使我提供了 EXE 所在的路径并对其进行了验证。

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.我注释掉了不相关的代码,因为它与firstProc变量无关。

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.最明显的是尝试在命令行环境中运行你的PATH TO EXE I WANT TO LAUNCH ,看看你是否得到一个不言自明的错误。

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.如果您的路径包含空格,您将看到您正在尝试执行一些有问题的空格,然后您可以使用 Graffito 给出的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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