从Registry中读取可执行路径 [英] Reading Executible path from Registry

查看:77
本文介绍了从Registry中读取可执行路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码项目上已有很多帖子,但我无法让这个程序读取安装了Adobe Acrobat的注册表中的路径。



这里是代码:



  private    Form1_Load( object  sender,EventArgs e)
{

RegistryKey adobe = Registry.LocalMachine.OpenSubKey( Software)。OpenSubKey( Adob​​e);
RegistryKey acroRead = adobe.OpenSubKey( Acrobat Reader);
string [] acroReadVersions;
string vnumber = ;
if (adobe!= null
{
acroReadVersions = acroRead.GetSubKeyNames();
foreach string versionNumber in acroReadVersions)
{
switch (versionNumber)
{
case 9.0 // 做某事;
vnumber = versionNumber;
break ;
case 10.0 // 执行某些操作;
vnumber = versionNumber;
break ;

默认
break ;
}

}

}

RegistryKey acroVersion = acroRead.OpenSubKey(vnumber);
RegistryKey acroPath = acroVersion.OpenSubKey( InstallPath);
acroPath = acroPath;
// 以下返回null。应该在我的系统上返回C:\Program Files(x86)\ Adob​​e \Reader 10.0 \ Reader:
string path =( string )acroPath.GetValue( 默认);
path = path;





这段代码出了什么问题?

解决方案

(默认)值是特殊的。要检索它,请指定空字符串()或null:

  string  path =(  string )acroPath.GetValue( null ); 





这应该可以解决你所问的问题。



其他一些提示:



1)您不需要依次打开每个单独的子键 - 在这种情况下您可以指定完整路径或其任何部分,因此只需一次调用就可以执行此操作:

 RegistryKey acroRead = Registry.LocalMachine.OpenSubKey( @  Software \ Adob​​e \Acrobat Reader); 





2)以上内容可帮助您避免一些错误 - 例如,当你应该对acroRead进行测试时,你正在检查adobe是否为null:

  if (acroRead !=  null 
{
acroReadVersions = acroRead.GetSubKeyNames();





3)RegistryKey是一次性的,所以理想情况下你应该在使用块中包装它。打开更少的键意味着你不需要经常这样做(现在,做OpenSubKey()。OpenSubKey()你不能为每一个指定一个使用声明!)



4)不确定你的额外任务是什么(路径=路径;等等,这些都没有做任何事情)。



5 )在测试中我注意到我有11.0版的Adobe Reader,所以你可能想要添加它。



6)我认为Adobe的安装会早些删除版本注册表项?如果不能假设您可能想要检查最高值而不是第一个遇到的值。



冒昧地重新编写代码然后,分开从第6项 - 开始,允许在32位窗口上工作(以x86或AnyCPU为目标) 64位窗口(定位x86,x64或AnyCPU)

 RegistryKey acroRead = Registry.LocalMachine.OpenSubKey( @  Software \ Adob​​e \Acrobat Reader); 
if null == acroRead)
{
acroRead = Registry.LocalMachine.OpenSubKey( @ SOFTWARE \Wow6432Node\Adobe\Acrobat Reader);
}
if (acroRead!= null
{
使用(acroRead)
{
string vnumber = ;
string [] acroReadVersions = acroRead.GetSubKeyNames();
foreach string versionNumber in acroReadVersions)
{
switch (versionNumber)
{
case 9.0 // 做某事;
vnumber = versionNumber;
break ;
case 10.0 // 执行某些操作;
vnumber = versionNumber;
break ;
case 11.0 // 执行某些操作;
vnumber = versionNumber;
break ;
默认
break ;
}
}
if (!string.IsNullOrEmpty(vnumber))
{
使用(RegistryKey acroVersion = acroRead.OpenSubKey(vnumber))
{
using (RegistryKey acroPath = acroVersion.OpenSubKey( InstallPath))
{
string path =( string )acroPath.GetValue( null );
// 使用path执行某些操作,或将其返回,或者在using块之外声明它: )
}
}
}
其他
{
// 错误 - 找不到版本号
}
}
}
else
{
// 错误 - 未找到adobe键
}







问候,

Ian。


如果您要查找的应用程序是64位系统上的32位应用程序,则注册表路径为HKEY_LOCAL_MACHINE \ SOFTWARE \Wow6432Node \Adobe \ ...


我尝试使用路径HKEY_LOCAL_MACHINE \ SOFTWARE \Wow6432Node \\Adobe\Acrobat读卡器

则抛出异常的对象引用不设置到AMN的对象的实例。

There are a number of posts already on Code Project but I just can''t get this program to read the path from the Registry where Adobe Acrobat is installed.

Here''s the code:

       private void Form1_Load(object sender, EventArgs e)
        {

           RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Adobe");
            RegistryKey acroRead = adobe.OpenSubKey("Acrobat Reader");
            string[] acroReadVersions;
            string vnumber = "";
            if (adobe != null)
            {
                acroReadVersions = acroRead.GetSubKeyNames();
                foreach (string versionNumber in acroReadVersions)
                {
                    switch (versionNumber)
                    {
                        case "9.0":  //do something;
                            vnumber = versionNumber;
                            break;
                        case "10.0":  //do something;
                            vnumber = versionNumber;
                            break;

                        default:
                            break;
                    }

                }

            }

            RegistryKey acroVersion = acroRead.OpenSubKey(vnumber);
            RegistryKey acroPath = acroVersion.OpenSubKey("InstallPath");
            acroPath = acroPath;
// following returns null. Should return C:\Program Files (x86)\Adobe\Reader 10.0  \Reader on my system
            string path = (string)acroPath.GetValue("Default"); 
            path = path;      



What''s wrong with this code?

解决方案

The (Default) value is special. To retrieve it, specify either an empty string ("") or null:

string path = (string)acroPath.GetValue(null);



That should solve the problem you''ve asked about.

A few other tips:

1) You don''t need to open each individual subkey in turn - you can specify the complete path, or any part thereof in this case, thus with one call you can do this:

RegistryKey acroRead = Registry.LocalMachine.OpenSubKey(@"Software\Adobe\Acrobat Reader");



2) The above might help you to avoid some errors - for example you are checking that "adobe" is not null, when you should be testing against acroRead:

if (acroRead != null)
{
    acroReadVersions = acroRead.GetSubKeyNames();



3) RegistryKey is disposable, so ideally you should wrap the use of it in a "using" block. Opening fewer keys will mean you need to do this less often (and presently, doing OpenSubKey().OpenSubKey() you can''t specify a using statement for each one!)

4) Not sure what your extra assignments are for at the end (path = path; etc. These don''t do anything).

5) In testing I noted that I have version 11.0 of Adobe Reader, so you might want to add that in.

6) I assume Adobe''s installation removes earlier version registry keys? If that cannot be assumed you may want to check for the highest value rather than the first one encountered.

Taking the liberty of re-working your code slightly then, apart from item 6 - and allowing for this to work on 32-bit windows (targeting x86 or AnyCPU) or 64-bit windows (targeting x86, x64 or AnyCPU):

RegistryKey acroRead = Registry.LocalMachine.OpenSubKey(@"Software\Adobe\Acrobat Reader");
if (null == acroRead)
{
    acroRead = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\Adobe\Acrobat Reader");
}
if (acroRead != null)
{
    using (acroRead)
    {
        string vnumber = "";
        string[] acroReadVersions = acroRead.GetSubKeyNames();
        foreach (string versionNumber in acroReadVersions)
        {
            switch (versionNumber)
            {
                case "9.0":  //do something;
                    vnumber = versionNumber;
                    break;
                case "10.0":  //do something;
                    vnumber = versionNumber;
                    break;
                case "11.0":  //do something;
                    vnumber = versionNumber;
                    break;
                default:
                    break;
            }
        }
        if (!string.IsNullOrEmpty(vnumber))
        {
            using (RegistryKey acroVersion = acroRead.OpenSubKey(vnumber))
            {
                using (RegistryKey acroPath = acroVersion.OpenSubKey("InstallPath"))
                {
                    string path = (string)acroPath.GetValue(null);
                    // Do something with path, or return it, or else declare it outside the "using" blocks :)
                }
            }
        }
        else
        {
            // Error - version number not found
        }
    }
}
else
{
    // Error - adobe key not found
}




Regards,
Ian.


If the application you''re looking for is a 32-bit app on a 64-bit system, the registry path will be HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Adobe\...


I tried using the path HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Adobe\Acrobat Reader
an exception is thrown "Object reference not set to amn instance of an object. "


这篇关于从Registry中读取可执行路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆