从powershell读取密钥

本文关键字:密钥 读取 powershell | 更新日期: 2023-09-27 18:15:40

我创建了一个c#应用程序,它为powershell添加了新的cmdlet。现在我打开powershell,在执行cmdlet期间,如果用户键入"ctrl+c",我想停止执行并终止。

如何在c#应用程序中从powershell读取key ?我要找的是console。readkey ()

从powershell读取密钥

如果你想从c#应用程序中检测Ctrl-C,你可以使用控制台。CancelKeyPress事件

下面是一个例子:

static void Main(string[] args)
{
    Console.CancelKeyPress += (sender, e) =>
        {
            Environment.Exit(0);
        };
    while (true)
    {
        // Do Stuff.
    }
}

然而,我可能不理解你的问题,作为一个正常的c#应用程序已经死与Ctrl-C默认

您可以使用Console.CancelKyePress事件