『练手』通过注册表 获取 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) >= && productName.IndexOf("Visual Studio", StringComparison.InvariantCultureIgnoreCase) >= )
{
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 < ) { continue; } tempPath = tempPath.Substring(, 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) >= && productName.IndexOf("Microsoft SQL Server", StringComparison.InvariantCultureIgnoreCase) >= )
{
sqlPath = fileVersion.FileName;
if (File.Exists(sqlPath))
{
if (sqlKey != null) { sqlKey.SetValue("MSSQLPATH", sqlPath); sqlKey.Flush(); sqlKey.Close(); }
return sqlPath;
}
}
}
}
} return string.Empty;
}
运行结果:

『练手』通过注册表 获取 VS 和 SQLServer 文件路径的更多相关文章
- 『练手』005 Laura.SqlForever历史遗留 的 架构思想缺陷
005 Laura.SqlForever历史遗留 的 架构思想缺陷 我们 比较一下 Laura.WinFramework 和 Laura.XtraFramework 的差异: Laura.WinFra ...
- 『练手』003 Laura.SqlForever如何扩展 兼容更多数据库引擎
003 Laura.SqlForever如何扩展 兼容更多数据库引擎 数据库引擎插件 在 界面上的体现 导航窗体 的 工具栏 中的 引擎下拉列表 导航窗体 的 树形控件 中的 引擎主节 ...
- 『练手』手写一个独立Json算法 JsonHelper
背景: > 一直使用 Newtonsoft.Json.dll 也算挺稳定的. > 但这个框架也挺闹心的: > 1.影响编译失败:https://www.cnblogs.com/zih ...
- 『练手』001 Laura.SqlForever架构基础(Laura.XtraFramework 的变迁)
001 Laura.SqlForever架构的基础(Laura.XtraFramework 的变迁之路) Laura.XtraFramework 到底是 做什么的? Laura.XtraFramewo ...
- 『练手』004 Laura.SqlForever如何扩展 导航栏 工具栏 右键菜单 插件
004 Laura.SqlForever如何扩展 导航栏 工具栏 右键菜单 插件 导航栏 插件扩展 比如下图的 窗口 > 关闭所有文档 这个导航栏: 在 任何程序集,任何命名空间,任 ...
- C# 系统应用之通过注册表获取USB使用记录(一)
该文章是“个人电脑历史记录清除软件”项目的系统应用系列文章.前面已经讲述了如何清除IE浏览器的历史记录.获取Windows最近访问文件记录.清除回收站等功能.现在我需要完成的是删除USB设备上的U盘. ...
- 练手爬虫用urllib模块获取
练手爬虫用urllib模块获取 有个人看一段python2的代码有很多错误 import re import urllib def getHtml(url): page = urllib.urlope ...
- delphi 获取 TreeView选中的文件路径
//获取 TreeView选中的文件路径 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, G ...
- C# 根据注册表获取当前用户的常用目录整理
1.使用C#获取当前程序或解决方案的路径 2.使用C#获取当前登录用户的相关目录 3.也可以获取当前系统通用目录 4.获取Windows系统的目录,从注册表中获取. 一.当前用户的目录,HKEY_Cu ...
随机推荐
- 拥抱.NET Core系列:MemoryCache 缓存选项
在上一篇 "拥抱.NET Core系列:MemoryCache 缓存过期" 中我们详细的了解了缓存过期相关的内容,今天我们来介绍一下 MSCache 中的 Options,由此来介 ...
- Java 第二章 变量、数据类型和运算符
第二章 变量.数据类型和运算符 什么是变量: 变量代表一块内存区域,变量类型不一样,这一块内存的大小也不一样. #在编程语言里面,你可以通过定义变量,向内存里添加数据或者修改内存已有的数据. ...
- 前端测试框架对比(js单元测试框架对比)
前端测试框架对比(js单元测试框架对比) 本文主要目的在于横评业界主流的几款前端框架,顺带说下相关的一些内容. 测试分类 通常应用会有 单元测试(Unit tests) 和 功能测试(Function ...
- 【POJ1151】Atlantis(线段树,扫描线)
[POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...
- 【BZOJ3530】数数(AC自动机,动态规划)
[BZOJ3530]数数(AC自动机,动态规划) 题面 BZOJ 题解 很套路的\(AC\)自动机+\(DP\) 首先,如果长度小于\(N\) 就不存在任何限制 直接大力\(DP\) 然后强制限制不能 ...
- [HNOI2014]米特运输
显然知道一个节点就可以推出整棵树 然而直接乘会爆longlong 所以考虑取log 最后排序算众数即可 # include <stdio.h> # include <stdlib.h ...
- Marriage Match IV HDU - 3416
题意 给你n个点,m条边,要求每条边只能走一次的S到T的最短路径的个数 题解 在我又WA又TLE还RE时,yyb大佬告诉我说要跑两遍SPFA,还说我写的一遍SPFA是错的,然而 啪啪打脸... 而且他 ...
- Java 计算年龄
public static String getAgeTxt(String birthTime,String beginTime,int level){ if(StringUtils.isBlank( ...
- 【noip模拟】最小点覆盖
Time Limit: 1000ms Memory Limit: 128MB Description 最小点覆盖是指在二分图中,用最小的点集覆盖所有的边.当然,一个二分图的最小点覆盖可能有很 ...
- PHP 获取访问来源
原文:http://www.upwqy.com/details/16.html $_SERVER['HTTP_REFERER'] 通过这个全局变量可以获取访问的链接是来源于哪里 比如说从博客园 htt ...