2015-04-01 71 views
0

我开发了一个用WPF/C#编写的自定义shell,它将explorer.exe替换为Windows 8.1系统上的shell。一切运行良好,除了我想在Run/RunOnce注册表位置运行应用程序。主要问题是,似乎没有将条目放入注册表的标准方式 - 有些条目是双引号,有些则没有;有些通过rundll32运行,并有一个入口点,后面跟着逗号分隔的参数;有些有空间分隔的论点;一些条目中有一些可执行文件。
有没有服务或可执行文件我可以调用来运行这些条目,或者我坚持试图找出一种方法来解析这个尽可能最好,并使用Process.Start()?以编程方式在Run和RunOnce注册表中运行应用程序

感谢您的帮助!

回答

0

我不得不在工作中做类似的项目。下面的代码或多或少是我写的。我不能保证它是万无一失的,但它似乎与我测试的注册管理机构一起工作。

public static class ProcessHelper 
{ 
    const string RegistrySubKeyName = 
     @"Software\Microsoft\Windows\CurrentVersion\Run"; 

    public static void LaunchStartupPrograms() 
    { 
     foreach (string commandLine in GetStartupProgramCommandLines()) 
     { 
      string fileName; 
      string arguments; 

      if (File.Exists(commandLine)) 
      { 
       fileName = commandLine; 
       arguments = String.Empty; 
      } 
      else if (commandLine.StartsWith("\"")) 
      { 
       int secondQuoteIndex = commandLine.IndexOf("\"", 1); 
       fileName = commandLine.Substring(1, secondQuoteIndex - 1); 

       if (commandLine.EndsWith("\"")) 
       { 
        arguments = String.Empty; 
       } 
       else 
       { 
        arguments = commandLine.Substring(secondQuoteIndex + 2); 
       } 
      } 
      else 
      { 
       int firstSpaceIndex = commandLine.IndexOf(' '); 
       if (firstSpaceIndex == -1) 
       { 
        fileName = commandLine; 
        arguments = String.Empty; 
       } 
       else 
       { 
        fileName = commandLine.Substring(0, firstSpaceIndex); 
        arguments = commandLine.Substring(firstSpaceIndex + 1); 
       } 
      } 

      Process.Start(fileName, arguments); 
     } 
    } 

    static IEnumerable<string> GetStartupProgramCommandLines() 
    { 
     using (RegistryKey key = 
      Registry.CurrentUser.OpenSubKey(RegistrySubKeyName)) 
     { 
      foreach (string name in key.GetValueNames()) 
      { 
       string commandLine = (string) key.GetValue(name); 
       yield return commandLine; 
      } 
     } 

     using (RegistryKey key = 
      Registry.LocalMachine.OpenSubKey(RegistrySubKeyName)) 
     { 
      foreach (string name in key.GetValueNames()) 
      { 
       string commandLine = (string) key.GetValue(name); 
       yield return commandLine; 
      } 
     } 
    } 
} 
+0

哦,我的上帝,这看起来很惊人。目前并不需要万无一失。我将通过它来看看它是如何适合我的代码库的。你摇滚! – Giallo 2015-04-01 16:01:11

相关问题