【问题标题】:How to check PostgreSql is installed or not in windows machine using C#.net?如何使用 C#.net 检查 Windows 机器中是否安装了 PostgreSql?
【发布时间】:2015-05-13 06:43:43
【问题描述】:

我有一个带有 PostgreSQL 的 WPF 应用程序,我需要检查 PostgreSQL 是否安装在本地计算机上。

const string PostgresSQLKeyName = "SOFTWARE\\PostgreSQL\\Installations\\postgresql-x64-9.4";
const string NetRegKeyValue = "DllFullPath";

private static bool GetRegistryValue<T>(RegistryHive hive, string key, string value, RegistryValueKind kind, out T data)
{
    bool success = false;
    data = default(T);

    using (RegistryKey baseKey = RegistryKey.OpenRemoteBaseKey(hive, String.Empty)) {
        if (baseKey != null) {
            using (RegistryKey registryKey = baseKey.OpenSubKey(key, RegistryKeyPermissionCheck.ReadSubTree)) {
                if (registryKey != null) {
                    try {
                        // If the key was opened, try to retrieve the value.
                        RegistryValueKind kindFound = registryKey.GetValueKind(value);
                        if (kindFound == kind) {
                            object regValue = registryKey.GetValue(value, null);
                            if (regValue != null) {
                                data = (T)Convert.ChangeType(regValue, typeof(T), CultureInfo.InvariantCulture);
                                success = true;
                            }
                        }
                    }
                    catch (IOException ex) {
                        //Logger.Write(ex, "GetRegistryValue-Detection.cs", "GetRegistryValue");
                        success = false;
                    }
                }
            }
        }
    }
    return success;
}

但即使我安装了它,我总是得到错误的结果(= 未安装)。我在这里错过了什么?

【问题讨论】:

  • 这个方法怎么称呼?哪个蜂巢?
  • 如果安装了其他版本的PostgreSQL怎么办?
  • @Vivek,postgresql-x64-9.4,已经安装在我的机器上
  • 试试this的方式,不错

标签: c# wpf postgresql installation


【解决方案1】:

使用 C# 检查注册表项

var postgres = Registry.CurrentUser.OpenSubKey(@"Software\pgadmin");
if(postgres != null)
{
    //postgreSQL is installed on your machine
}

【讨论】:

    猜你喜欢
    • 2011-06-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-17
    相关资源
    最近更新 更多