如何使用C#检查PowerShell是否已安装

4

我希望编写一个类似于C#语言的方法:

public bool PowershellExists()
{
    // Returns true if PowerShell exists on the machine, else false.
}

请返回翻译后的文本:http://blogs.msdn.com/b/powershell/archive/2009/06/25/detection-logic-poweshell-installation.aspx + Registry.GetValue - Mitch
1
是的,我明白了,但我的意思是针对一个C#方法。 - Mohammad Nadeem
3
检查注册表是一个C#方法。StackOverflow不是将文档转化为代码的工具。如果你不理解注册表或如何访问它,应该进行相关研究。 - Mitch
那么这不是关于它不使用吗? - Ghasem
1个回答

9

使用MSDN博客文章Detection logic for PowerShell installation,我编写了以下方法:

public bool PowershellExists()
{
    string regval = Microsoft.Win32.Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1", "Install", null).ToString();
    if (regval.Equals("1"))
        return true;
    else
        return false;
}

它能否告诉版本号?或者我们能否修改它以某种方式获取版本信息? - N.K
@N.K 如果您跟随链接,就会发现另一个键可以让您知道版本:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine - 在较新的版本中,您可以查看 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\3\PowerShellEngine。 - Matthieu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接