Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#控制台中不存在应用程序,我想重新启动控制台_C# - Fatal编程技术网

C#控制台中不存在应用程序,我想重新启动控制台

C#控制台中不存在应用程序,我想重新启动控制台,c#,C#,我制作了一个刽子手游戏,我希望在你赢/输后能够重新启动它,而不是每次都要手动重新启动程序 我在互联网上看到了可以用来重新启动控制台的“应用程序”。然而,它说这个应用程序并不存在 Close(); System.Diagnostics.Process.Start(Application.ExecutablePath); 请帮帮我 你可以在下面找到它: 您需要包括System.Windows.Forms程序集,并使用添加 using System.Windows.Forms 但对于我更喜欢使

我制作了一个刽子手游戏,我希望在你赢/输后能够重新启动它,而不是每次都要手动重新启动程序

我在互联网上看到了可以用来重新启动控制台的“应用程序”。然而,它说这个应用程序并不存在

 Close();
 System.Diagnostics.Process.Start(Application.ExecutablePath);
请帮帮我

你可以在下面找到它:

您需要包括
System.Windows.Forms
程序集,并使用添加

using System.Windows.Forms
但对于我更喜欢使用的控制台应用程序(在控制台程序集中时):

正如Andre在评论中所说,您最好使用
Assembly.GetEntryAssembly()
,因为它获得了第一个被调用的程序集(当然是控制台应用程序中的可执行程序)


当然,就像前面已经删除的答案一样,首先重新打开,然后关闭应用程序。

我认为你真的不需要重新启动控制台应用程序来重新启动游戏。下面是一个可能的游戏重启场景,可以使用:

class Program
{
    static void Main(string[] args)
    {
        Game game = new Game();

        Menu menu = new Menu(game);

        game.Finished += menu.AskAndRun;

        menu.Welcome();
    }
}

class Menu
{
    public readonly IRunnable runnable;

    public Menu(IRunnable runnable)
    {
        this.runnable = runnable;
    }

    public void Welcome()
    {
        Console.WriteLine("Welcome, Player!");

        AskAndRun();
    }

    public void AskAndRun()
    {
        Console.WriteLine("\nTo play a new game, press [ENTER]");

        Console.ReadLine();

        Console.Clear();

        runnable.Run();
    }
}

interface IRunnable
{
    void Run();
}

class Game : IRunnable
{
    public event Action Finished;

    public void Run()
    {
        Console.WriteLine("Game started here...");

        Console.WriteLine("Oops! Game finished already");

        if (Finished != null)
        {
            Finished();
        }
    }
}
如您所见,
游戏
类与现在由
菜单
类处理的重启逻辑分离。现在,引入诸如退出和计算已启动游戏数量之类的新功能是很简单的


请注意,
菜单
不依赖于
游戏
。这取决于
IRunnable
,这将帮助您更改
游戏
,测试
菜单
,并毫无痛苦地引入新功能。

您不能添加一些代码来重置应用程序,而不是实际重新启动它吗?我想您听说过循环之类的事情吗?您需要重新启动程序的原因是否合理?请参阅Patrick关于如何获取执行/进入程序集的回答,但正如JLe所说,这并不是解决实际问题的好办法。也就是说,你不应该每次都重新启动游戏。考虑一种将状态重置为已知状态或将状态封装在单独的对象ETCI中的方法。绝对不要仅仅为了获得程序集路径而在winforms上添加依赖项。另请看Assembly.GetEntryAssembly()好的,我尝试使用System.Windows.Forms添加,但出现错误“类型或命名空间名称“Windows”在命名空间“System”中不存在”(是否缺少程序集引用?”@FatPoo2:还有程序集?是否添加了它?在哪里添加程序集?添加引用框在哪里?
class Program
{
    static void Main(string[] args)
    {
        Game game = new Game();

        Menu menu = new Menu(game);

        game.Finished += menu.AskAndRun;

        menu.Welcome();
    }
}

class Menu
{
    public readonly IRunnable runnable;

    public Menu(IRunnable runnable)
    {
        this.runnable = runnable;
    }

    public void Welcome()
    {
        Console.WriteLine("Welcome, Player!");

        AskAndRun();
    }

    public void AskAndRun()
    {
        Console.WriteLine("\nTo play a new game, press [ENTER]");

        Console.ReadLine();

        Console.Clear();

        runnable.Run();
    }
}

interface IRunnable
{
    void Run();
}

class Game : IRunnable
{
    public event Action Finished;

    public void Run()
    {
        Console.WriteLine("Game started here...");

        Console.WriteLine("Oops! Game finished already");

        if (Finished != null)
        {
            Finished();
        }
    }
}