『练手』通过注册表 获取 VS 和 SQLServer 文件路径

获取任意 VS 和 SQLServer 的 磁盘安装目录。

背景需求:如果磁盘电脑安装了 VS 或者 SQLServer 则 认定这台计算机 的使用者 是一名 软件研发人员,则让程序 以最高权限运行。

 

代码如下:(基于注册表读取、exe版权信息校验)

        static void Main(string[] args)
        {
            string vsPath = FindVisualStudioPath();
            Console.WriteLine(vsPath);

            string sqlPath = FindSQLServerPath();
            Console.WriteLine(sqlPath);

            Console.ReadKey();
        }


        private static string FindVisualStudioPath()
        {
            RegistryKey studioKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\VISUALSTUDIO");
            string studioPath = studioKey == null ? string.Empty : (studioKey.GetValue("VSPATH") ?? string.Empty).ToString();
            if (File.Exists(studioPath))
            {
                if (studioKey != null) studioKey.Close();
                return studioPath;
            }

            RegistryKey devenvKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\MICROSOFT\WINDOWS\CURRENTVERSION\APP PATHS\DEVENV.EXE");
            string devenvPath = devenvKey == null ? string.Empty : (devenvKey.GetValue(string.Empty) ?? string.Empty).ToString();
            if (devenvKey != null) devenvKey.Close();
            if (File.Exists(devenvPath))
            {
                FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(devenvPath);
                string companyName = fileVersion.CompanyName ?? string.Empty;
                string productName = fileVersion.ProductName ?? string.Empty;
                if (companyName.IndexOf("Microsoft", StringComparison.InvariantCultureIgnoreCase) >= 0 && productName.IndexOf("Visual Studio", StringComparison.InvariantCultureIgnoreCase) >= 0)
                {
                    studioPath = fileVersion.FileName;
                    if (studioKey != null && File.Exists(studioPath))
                    {
                        studioKey.SetValue("VSPATH", studioPath);
                        studioKey.Flush();
                        studioKey.Close();
                        return studioPath;
                    }
                }
            }

            return string.Empty;
        }
        private static string FindSQLServerPath()
        {
            RegistryKey sqlKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\MICROSOFT\MICROSOFT SQL SERVER");
            string sqlPath = sqlKey == null ? string.Empty : (sqlKey.GetValue("MSSQLPATH") ?? string.Empty).ToString();
            if (File.Exists(sqlPath))
            {
                if (sqlKey != null) sqlKey.Close();
                return sqlPath;
            }

            RegistryKey svcRootKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CURRENTCONTROLSET\SERVICES");
            if (svcRootKey != null)
            {
                string[] svcArray = svcRootKey.GetSubKeyNames();
                foreach (string svc in svcArray)
                {
                    RegistryKey svcKey = svcRootKey.OpenSubKey(svc);
                    if (svcKey == null) { continue; }
                    string tempPath = (svcKey.GetValue("ImagePath") ?? string.Empty).ToString();
                    svcKey.Close();
                    if (string.IsNullOrEmpty(tempPath)) { continue; }

                    int index = tempPath.IndexOf("sqlservr.exe", StringComparison.InvariantCultureIgnoreCase);
                    if (index < 0) { continue; }

                    tempPath = tempPath.Substring(0, index + "sqlservr.exe".Length).Trim().Trim('\'', '"').Trim();
                    if (File.Exists(tempPath))
                    {
                        FileVersionInfo fileVersion = FileVersionInfo.GetVersionInfo(tempPath);
                        string companyName = fileVersion.CompanyName ?? string.Empty;
                        string productName = fileVersion.ProductName ?? string.Empty;
                        if (companyName.IndexOf("Microsoft", StringComparison.InvariantCultureIgnoreCase) >= 0 && productName.IndexOf("Microsoft SQL Server", StringComparison.InvariantCultureIgnoreCase) >= 0)
                        {
                            sqlPath = fileVersion.FileName;
                            if (File.Exists(sqlPath))
                            {
                                if (sqlKey != null) { sqlKey.SetValue("MSSQLPATH", sqlPath); sqlKey.Flush(); sqlKey.Close(); }
                                return sqlPath;
                            }
                        }
                    }
                }
            }

            return string.Empty;
        }
View Code

 

运行结果:

 

posted on 2017-12-03 20:21  InkFx  阅读(765)  评论(0编辑  收藏  举报