繁体   English   中英

有没有办法将此Powershell脚本转换为C#

[英]Is there a way to convert this powershell script to C#

我想将此代码转换为C#。

Function CheckFileInstalled {

param (
    [string]$pathProg     = "C:\Program Files\WinRAR\WinRAR.exe",
    [string]$nameProg     = "Winrar"
)

$testFileProg = Test-Path $pathProg

$x86 = ((Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall") |
    Where-Object { $_.GetValue( "DisplayName" ) -like "*$nameProg*" } ).Length -gt 0;

$x64 = ((Get-ChildItem "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall") |
    Where-Object { $_.GetValue( "DisplayName" ) -like "*$nameProg*" } ).Length -gt 0;


return ( $testFileProg -and ($x86 -or $x64) )

}

if( CheckFileInstalled ) {
    Write-Host "Program installed." 
}
else {
    Write-Host "Failed to install."
}

试试这个,但是您可能需要管理员权限才能注册

public bool CheckFileInstalled(string pathProg, string nameProg)
{
    bool pathExist = Directory.Exists(pathProg);
    bool x86 = false;
    bool x64 = false;

    RegistryKey x86Key = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Uninstall", true);
    if(x86Key.GetValueNames().Contains(nameProg)) x86 = true;

    RegistryKey x64Key = Registry.LocalMachine.OpenSubKey(@"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall", true);
    if(x64Key.GetValueNames().Contains(nameProg)) x64 = true;

    return (pathExist && (x86 || x64));
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM