在应用启动C#时最小化控制台

时间:2017-06-21 11:40:53

标签: c# .net console-application

我有一个控制台应用程序应用程序,我希望在运行应用程序时最小化(不会永久隐藏)控制台,是否可能? 另外,我每隔10分钟使用一个计时器来运行任务,是否可以在每次应用程序运行时最小化控制台? 谢谢!

1 个答案:

答案 0 :(得分:3)

以下代码应该可以解决问题。使用Win32方法最小化控制台窗口。我正在使用Console.ReadLine()来防止窗口立即关闭。

internal class Program
{
    [DllImport("User32.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool ShowWindow([In] IntPtr hWnd, [In] int nCmdShow);

    private static void Main(string[] args)
    {
        IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;

        ShowWindow(handle, 6);

        Console.ReadLine();
    }
}
相关问题