2011-05-18 26 views
1

我有以下程序,当我建立它(释放配置)。它以管理员身份在我的开发人员机器上运行。但是,当我在最终用户(非管理员)上运行它时,它崩溃并说出有关安全性的内容。有什么我需要的属性(或者代码),这样做就可以伪装成一个管理员试图部署一个小的应用程序,使更改注册表

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Win32; 

namespace LotusTrustedSites 
{  
    class ReportDownloader  
    {   
     [STAThread]   
     static void Main(string[] args)   
     {    
      const string domainsKeyLocation = @"Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains";    
      const string domain = @"newsite.com";    
      const int trustedSiteZone = 0x2;    
      var subdomains = new Dictionary<string, string> 
       {          
        {"www", "https"},          
        {"www", "http"},          
        {"blog", "https"},          
        {"blog", "http"}         
       };    
      RegistryKey currentUserKey = Registry.CurrentUser;    
      currentUserKey.GetOrCreateSubKey(domainsKeyLocation, domain, false);    
      foreach (var subdomain in subdomains)    
      {     
       CreateSubdomainKeyAndValue(currentUserKey, domainsKeyLocation, domain, subdomain, trustedSiteZone);    
      }   //automation code   
     }   

     private static void CreateSubdomainKeyAndValue(RegistryKey currentUserKey, string domainsKeyLocation, string domain, KeyValuePair<string, string> subdomain, int zone)   
     {    
      RegistryKey subdomainRegistryKey = currentUserKey.GetOrCreateSubKey(string.Format(@"{0}\{1}", domainsKeyLocation, domain), subdomain.Key, true);    
      object objSubDomainValue = subdomainRegistryKey.GetValue(subdomain.Value);    
      if (objSubDomainValue == null || Convert.ToInt32(objSubDomainValue) != zone)    
      {     
       subdomainRegistryKey.SetValue(subdomain.Value, zone, RegistryValueKind.DWord);    
      }   
     }  
    }  
    public static class RegistryKeyExtensionMethods  
    {   
     public static RegistryKey GetOrCreateSubKey(this RegistryKey registryKey, string parentKeyLocation, string key, bool writable)   
     {    
      string keyLocation = string.Format(@"{0}\{1}", parentKeyLocation, key);   
      RegistryKey foundRegistryKey = registryKey.OpenSubKey(keyLocation, writable);    
      return foundRegistryKey ?? registryKey.CreateSubKey(parentKeyLocation, key);   
     }   
     public static RegistryKey CreateSubKey(this RegistryKey registryKey, string parentKeyLocation, string key)   
     {    
      RegistryKey parentKey = registryKey.OpenSubKey(parentKeyLocation, true); //must be writable == true    
      if (parentKey == null) 
      { 
       throw new NullReferenceException(string.Format("Missing parent key: {0}", parentKeyLocation)); 
      }    
      RegistryKey createdKey = parentKey.CreateSubKey(key);    
      if (createdKey == null) 
      { 
       throw new Exception(string.Format("Key not created: {0}", key)); 
      }    
      return createdKey;   
     }  
    } 
} 
+1

如果你不介意我的问题,你究竟想要做什么? 'Software \ Microsoft \ Windows \ CurrentVersion \ Internet Settings \ ZoneMap \ Domains'似乎不是您的软件应该创建任何东西的地方。首先,您应该问自己,如果某项操作在非管理设置下无效,那么您是应该真的在做自己正在做的事情,还是应该以其他方式执行此操作,例如应用程序设置为一个.config文件。 – 2011-05-18 20:21:08

回答

3

This文章可能会有帮助。

+0

这只是将'runas'添加到命令中,使其明确要求提供用户名/密码。如果提供的凭证用于非特权帐户,则操作仍会失败。 – Oded 2011-05-18 19:36:20

+0

运行方式从组策略中禁用 – 2011-05-18 19:59:05

2

写入注册表是一项特权操作 - 您需要拥有正确的权限。

应用程序正在运行的帐户需要具有适当的权限。

-1

创建注册表设置programmaticaly是有点做错了。它的问题是Windows安装程序子系统无法跟踪这些条目,并修复它们,或根据需要将其删除。更好的选择是使用应用程序设置文件,或者提供一个MSI安装程序为您创建这些条目。

+0

我在大型企业工作,有时需要通过服务器安装应用程序。但有时它不会安装,因此我们需要手动安装并配置它。我宁愿要一个小型应用程序,它可以对注册表进行必要的更改,并在1次点击后完成。 – 2011-05-19 13:45:50

+0

@Cocoa - 这可能是我为什么每台企业机器的速度比同类机器的速度慢10倍的原因之一。在这种情况下,这并不是什么大不了的事情,但这是一个坏习惯,会导致可以通过使用Windows安装程序机制进行迁移的问题。 – 2011-05-19 14:03:55

+0

@Cocoa - 并没有错误的答案。这是一个非常正确的。 – 2011-05-19 14:05:40

相关问题