是否可以在控制台应用程序中为.NET Core主机进程(dotnet.exe)设置标题?

时间:2018-08-20 16:05:36

标签: .net-core console-application

我正在尝试通过命令参数– --title="Host1"进行设置,但是它不起作用。如您所见,ASP.NET Core应用程序是可能的:

enter image description here

还有其他选择吗?我的计算机上有很多控制台进程,想要为其分配友好名称。

1 个答案:

答案 0 :(得分:0)

在您的项目中添加此类

如果已调试

delegate IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
public class DebuggerForm
{
    const UInt32 WS_EX_NOACTIVATE = 0x08000000;
    const UInt32 WS_MINIMIZE = 0x20000000;
    const int SW_SHOWMINNOACTIVE = 7;
    const UInt32 WM_DESTROY = 2;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    struct WNDCLASSEX
    {
        [MarshalAs(UnmanagedType.U4)]
        public int cbSize;
        [MarshalAs(UnmanagedType.U4)]
        public int style;
        public IntPtr lpfnWndProc;
        public int cbClsExtra;
        public int cbWndExtra;
        public IntPtr hInstance;
        public IntPtr hIcon;
        public IntPtr hCursor;
        public IntPtr hbrBackground;
        public string lpszMenuName;
        public string lpszClassName;
        public IntPtr hIconSm;
    }

    private WndProc delegWndProc = myWndProc;

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern bool DestroyWindow(IntPtr hWnd);

    [DllImport("user32.dll", SetLastError = true, EntryPoint = "CreateWindowEx")]
    public static extern IntPtr CreateWindowEx(UInt32 dwExStyle, UInt16 lpClassName, string lpWindowName, UInt32 dwStyle, int x, int y, int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance, IntPtr lpParam);

    [DllImport("user32.dll", SetLastError = true, EntryPoint = "RegisterClassEx")]
    static extern System.UInt16 RegisterClassEx([In] ref WNDCLASSEX lpWndClass);

    [DllImport("kernel32.dll")]
    static extern uint GetLastError();

    [DllImport("user32.dll")]
    static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, IntPtr wParam, IntPtr lParam);

    internal bool Create(string name)
    {
        WNDCLASSEX windowClass = new WNDCLASSEX();
        windowClass.cbSize = Marshal.SizeOf(typeof(WNDCLASSEX));
        windowClass.style = 0;
        windowClass.hbrBackground = IntPtr.Zero;
        windowClass.cbClsExtra = 0;
        windowClass.cbWndExtra = 0;
        windowClass.hInstance = Marshal.GetHINSTANCE(this.GetType().Module);
        windowClass.hIcon = IntPtr.Zero;
        windowClass.hCursor = IntPtr.Zero;
        windowClass.lpszMenuName = null;
        windowClass.lpszClassName = "DebuggerForm";
        windowClass.lpfnWndProc = Marshal.GetFunctionPointerForDelegate(delegWndProc);
        windowClass.hIconSm = IntPtr.Zero;
        ushort regResult = RegisterClassEx(ref windowClass);

        if (regResult == 0)
        {
            uint error = GetLastError();
            return false;
        }
        string wndClass = windowClass.lpszClassName;

        IntPtr hWnd = CreateWindowEx(WS_EX_NOACTIVATE, regResult, name, WS_MINIMIZE, 0, 0, 200, 200, IntPtr.Zero, IntPtr.Zero, windowClass.hInstance, IntPtr.Zero);

        if (hWnd == ((IntPtr)0))
        {
            uint error = GetLastError();
            return false;
        }
        ShowWindow(hWnd, SW_SHOWMINNOACTIVE);

        return true;
    }

    private static IntPtr myWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam)
    {
        switch (msg)
        {
            case WM_DESTROY:
                DestroyWindow(hWnd);
                break;

            default:
                break;
        }
        return DefWindowProc(hWnd, msg, wParam, lParam);
    }
}

endif

然后在构造函数的Startup.cs中添加 新的DebuggerForm()。Create(“ WebApp”);

这将把进程标题显示为WebApp。