关于c#从注册表获取软件安装路径的问题

beandcurd2010 2011-12-29 10:23:37
在网上看了很多方法,但是碰到的问题是这样的:
1、该软件在注册表中的HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall下
没有InstallLocation项。
2、该软件在注册表中的HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall下
的UninstallString项也没有包含该软件的安装目录。
请问这种情况如何获得软件的安装位置?
...全文
274 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
PaulyJiang 2011-12-29
  • 打赏
  • 举报
回复
最差的办法是 如果你知道 那么收索 注册表
csdn_aspnet 2011-12-29
  • 打赏
  • 举报
回复
很多软件安装位置不一样,但是他们基本上会在注册表的同一个位置写下自己的名字和程序路径,这个位置就是:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App 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_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App 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(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App 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;
}
}
}
beandcurd2010 2011-12-29
  • 打赏
  • 举报
回复
没有人愿意帮忙吗?
beandcurd2010 2011-12-29
  • 打赏
  • 举报
回复
现在的问题是我要找的程序是别人做的,在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths里没有该程序的信息,顺便问下,是不是所有的程序都会在注册表有安装路径这些信息。如果没有该如何获取?

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧