Tirion

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
      一直以来很习惯用Win+R打开"Run..."对话框,然后输入命令来打开要使用的程序,如winword,如wmplayer....
然而很多程序安装好之后并不能在Run...中简单输入程序名来打开,于是原来一直手动编辑注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
将新安装的项添加进去。近日重装了系统,原来加好的列表全没了-_-||,一个一个的重新手动加又嫌麻烦,所以干脆写个小工具,能够把选定的可执行文件快速加入到app Paths列表中。
     要做的很简单。首先修改注册表,在HKEY_CLASSES_ROOT\exefile\shell下加一个新项"Add2Cmd",默认值为“加入到命令列表...",在其下再创建一个名为"command"的项,其默认值为"C:\Document\Visual Studio Projects\Add2Cmd\Add2Cmd\bin\Debug\Add2Cmd.exe" "%1",其中C:\Document\Visual Studio Projects\Add2Cmd\Add2Cmd\bin\Debug\Add2Cmd.exe是我写的程序的地址。%1作为参数即为被选定的可执行文件的绝对路径和文件名。
    执行Add2Cmd.exe的时候将被选定的可执行文件的绝对路径和文件名记入HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths即可。
代码如下所示:
static void Main(string[] args) 
        
{
            
using (RegistryKey
                       Software 
= Registry.ClassesRoot.OpenSubKey(@"exefile\shell",true),
                       shell 
= Software.CreateSubKey("add2cmd"),
                       command 
= shell.CreateSubKey("command"))
            
{
                shell.SetValue(
"""添加到命令列表");    // 修改(默认)项的值
                command.SetValue("""\"" + Application.ExecutablePath + "\" \"%1\"");    // 修改路径
            }


            
if( args.Length > 1 )
                MessageBox.Show(
"参数过多,程序没有正常执行。","错误",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Error);
            
else if( args.Length == 1 )
            
{
                
string appPathName = args[0];
                
int index = appPathName.LastIndexOf("\\");
                
string appName = appPathName.Substring(index+1);
                
string appPath = appPathName.Substring(0,index);

                
using (RegistryKey
                           Software 
= Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths",true),
                           newCmd 
= Software.CreateSubKey(appName))
                
{
                    newCmd.SetValue(
"Path", appPath);    // 修改路径
                    newCmd.SetValue("", appPathName);    // 修改(默认)项的值
                }


                MessageBox.Show(
"成功加入到命令列表,可以在Start->Run中输入"+appName+"运行该程序了。","成功",System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information);
            }

        }
posted on 2004-11-07 12:56  Findekano  阅读(1288)  评论(1编辑  收藏  举报