C#获取常见软件的安装路径(调注册表)
作者:网络转载 发布时间:[ 2013/9/18 13:27:32 ] 推荐标签:
很多软件安装位置不一样,但是他们基本上会在注册表的同一个位置写下自己的名字和程序路径,这个位置是:
HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Paths
所以只要去这个地方找软件路径可以了。
你可以添加你知道的正确的文件名,是它在注册表中的名字。比如office word在注册表中叫winword
终拿到的是那个exe的完全路径,如果要文件夹的话用Path.GetDirectoryName()行了
public enum Softwares
{
//The names are the same with the registry names.
//You can add any software exists in the "regedit" path:
//HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Paths
EXCEL, //Office Excel
WINWORD, //Office Word
MSACCESS, //Office Access
POWERPNT, //Office PowerPoint
OUTLOOK, //Office Outlook
INFOPATH, //Office InfoPath
MSPUB, //Office Publisher
VISIO, //Office Visio
IEXPLORE, //IE
ITUNES //Apple ITunes
//.........
}
public class SoftwareOperator
{
//When you do not want to use string name, then use the Enum instead
public static bool TryGetSoftwarePath(Softwares softName, out string path)
{
return TryGetSoftwarePath(softName.ToString(), out path);
}
public static bool TryGetSoftwarePath(string softName, out string path)
{
string strPathResult = string.Empty;
string strKeyName = ""; //"(Default)" key, which contains the intalled path
object objResult = null;
Microsoft.Win32.RegistryValueKind regValueKind;
Microsoft.Win32.RegistryKey regKey = null;
Microsoft.Win32.RegistryKey regSubKey = null;
try
{
//Read the key
regKey = Microsoft.Win32.Registry.LocalMachine;
regSubKey = regKey.OpenSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionApp Paths" + softName.ToString() + ".exe", false);
//Read the path
objResult = regSubKey.GetValue(strKeyName);
regValueKind = regSubKey.GetValueKind(strKeyName);
//Set the path
if (regValueKind == Microsoft.Win32.RegistryValueKind.String)
{
strPathResult = objResult.ToString();
}
}
catch (System.Security.SecurityException ex)
{
throw new System.Security.SecurityException("You have no right to read the registry!", ex);
}
catch (Exception ex)
{
throw new Exception("Reading registry error!", ex);
}
finally
{
if (regKey != null)
{
regKey.Close();
regKey = null;
}
if (regSubKey != null)
{
regSubKey.Close();
regSubKey = null;
}
}
if (strPathResult != string.Empty)
{
//Found
path = strPathResult;
return true;
}
else
{
//Not found
path = null;
return false;
}
}
}
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11