如何将WPF .NET Core EXE添加为Visual Studio 2022的项目参考,以用作多重启动项目?

发布于 2025-02-03 03:20:57 字数 390 浏览 4 评论 0原文

我有一个.net core带有一个Console App项目的解决方案。我想将另一个现有的WPF项目添加到解决方案中,作为项目参考,但没有其源代码。我想在wpfapp \ bin \ debug \ net6.0-windows文件夹中仅包含存在于wpfapp \ bin \ bin \ bin \ debug \ debug \ debug> folder中的exe或依赖的dll。之后,我想将项目设置为多重启动项目,并希望我的WPF EXE在我的台应用程序应用程序构建并启动时开始,并希望我的WPF EXE在控制台时关闭应用结束。

I have a .net core solution with one console app project. I want to add another existing WPF project to solution as project reference but without its source code. I want to include only exe or dependent dll that exists in WpfApp\bin\Debug\net6.0-windows folder. After that I want to set my project as multiple startup project and want my WPF exe to start when my console application build and start and want my WPF exe to close when my console application ends.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

错々过的事 2025-02-10 03:20:57

根据您的描述,我进行了一些与此问题有关的代码测试。
您可以参考以下代码来解决问题:

class Program
{
    static bool exitSystem = false;

    #region Trap application termination
    [DllImport("Kernel32")]
    private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);

    private delegate bool EventHandler(CtrlType sig);
    static EventHandler _handler;

    enum CtrlType
    {
        CTRL_C_EVENT = 0,
        CTRL_BREAK_EVENT = 1,
        CTRL_CLOSE_EVENT = 2,
        CTRL_LOGOFF_EVENT = 5,
        CTRL_SHUTDOWN_EVENT = 6
    }

    private static bool Handler(CtrlType sig)
    {
        Console.WriteLine("Exiting system due to external CTRL-C, or process kill, or shutdown");
        Close();
        Console.WriteLine("WPF.exe is closed");
        Thread.Sleep(5000); 
        Console.WriteLine("Cleanup complete");
        exitSystem = true;
        Environment.Exit(-1);
        return true;
    }
    #endregion
    static void Main(string[] args)
    {
        _handler += new EventHandler(Handler);
        SetConsoleCtrlHandler(_handler, true);
        Start();
        Console.WriteLine("WPF.exe is opened");
        while(!exitSystem)
        {
            Thread.Sleep(500);
        }
    }
    [Conditional("DEBUG")]
    private static void Start()
    {
        Process.Start(@"Your file path");
    }
    [Conditional("DEBUG")]
    private static void Close()
    {
        foreach (var process in Process.GetProcesses().Where(pr => pr.ProcessName == " name of the process without a suffix"))
        {
            process.Kill();
        }
    }

}

您需要更改代码中的文件路径和ProcessName。

这是我的测试结果:

”在此处输入图像描述”

According to your description, i make some code test related to this issue.
You can refer to the following code to solve the problem:

class Program
{
    static bool exitSystem = false;

    #region Trap application termination
    [DllImport("Kernel32")]
    private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);

    private delegate bool EventHandler(CtrlType sig);
    static EventHandler _handler;

    enum CtrlType
    {
        CTRL_C_EVENT = 0,
        CTRL_BREAK_EVENT = 1,
        CTRL_CLOSE_EVENT = 2,
        CTRL_LOGOFF_EVENT = 5,
        CTRL_SHUTDOWN_EVENT = 6
    }

    private static bool Handler(CtrlType sig)
    {
        Console.WriteLine("Exiting system due to external CTRL-C, or process kill, or shutdown");
        Close();
        Console.WriteLine("WPF.exe is closed");
        Thread.Sleep(5000); 
        Console.WriteLine("Cleanup complete");
        exitSystem = true;
        Environment.Exit(-1);
        return true;
    }
    #endregion
    static void Main(string[] args)
    {
        _handler += new EventHandler(Handler);
        SetConsoleCtrlHandler(_handler, true);
        Start();
        Console.WriteLine("WPF.exe is opened");
        while(!exitSystem)
        {
            Thread.Sleep(500);
        }
    }
    [Conditional("DEBUG")]
    private static void Start()
    {
        Process.Start(@"Your file path");
    }
    [Conditional("DEBUG")]
    private static void Close()
    {
        foreach (var process in Process.GetProcesses().Where(pr => pr.ProcessName == " name of the process without a suffix"))
        {
            process.Kill();
        }
    }

}

You need to change the file path and the ProcessName in the code.

Here is my test result:

enter image description here

我要还你自由 2025-02-10 03:20:57

简短的答案是你不能。 .exe不是C#项目。如果您确实想在不包含该解决方案的情况下运行WPF应用程序,请添加一些运行.EXE的代码。就像以下

[Conditional("DEBUG")]
private static void Junk()
{
    Process.Start("[Filepath]");//exchange [Filepath] with the filepath to the .exe
}

调用上述启动方法一样

The short answer is you can't. An .exe is not a C# project. If you really want to run the WPF app without including it in the solution then add some code that runs the .exe. like the following

[Conditional("DEBUG")]
private static void Junk()
{
    Process.Start("[Filepath]");//exchange [Filepath] with the filepath to the .exe
}

Calling the above method on startup would start the WPF .exe (only when the build mode is debug)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文