简体   繁体   中英

How can I add wpf .net core exe as project reference to visual studio 2022 for using as multiple startup project?

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.

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:

在此处输入图像描述

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)

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