C# 尝试部署对注册表进行更改的小应用程序

C# 尝试部署对注册表进行更改的小应用程序,c#,visual-studio-2010,registry,C#,Visual Studio 2010,Registry,我有以下程序,当我构建它时,发布配置。它以管理员身份在我的开发人员机器上运行。然而,当我在非管理员的终端用户上运行它时,它崩溃了,并说明了一些关于安全性的问题。我是否需要在属性或代码中执行任何操作,以便它可以伪装成管理员 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; namespace LotusTrustedSites {

我有以下程序,当我构建它时,发布配置。它以管理员身份在我的开发人员机器上运行。然而,当我在非管理员的终端用户上运行它时,它崩溃了,并说明了一些关于安全性的问题。我是否需要在属性或代码中执行任何操作,以便它可以伪装成管理员

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;        
        }    
    }
}

文章可能有帮助。

文章可能有帮助。

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


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

写入注册表是一项特权操作-您需要具有适当的权限


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

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

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

这只是在命令中添加了runas,明确要求提供用户名/密码。如果提供的凭据是针对非特权帐户的,则操作仍将失败。从组策略中禁用运行方式这只是将运行方式添加到命令中,从而明确要求提供用户名/密码。如果提供的凭据是针对非特权帐户的,则操作仍将失败。从组策略中禁用运行方式如果您不介意我问,您到底想做什么?Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains似乎不是软件应该创建任何内容的地方。如果一个操作在非管理员设置下不起作用,你首先应该问自己,你是否真的应该做你正在做的事情,或者你是否应该用其他方式来做,比如用.config文件中的应用程序设置。如果你不介意我问,你到底想做什么?Software\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains似乎不是软件应该创建任何内容的地方。如果一个操作在非管理员设置下不起作用,你首先应该问自己,你是否真的应该做你正在做的事情,或者你是否应该用其他方式来做,比如用.config文件中的应用程序设置。我在一家大企业工作,有时我们需要通过服务器安装应用程序。然而,有时它不安装,所以我们需要手动安装它,然后配置它。我宁愿安装一个对注册表进行必要更改的小应用程序,只需单击一次即可完成。这可能是我使用过的每台企业级计算机的运行速度比类似的未配置计算机慢10倍的部分原因。在这种情况下,这没什么大不了的,但这是一个坏习惯,它会导致使用windows installer机制可以解决的问题。@Cocoa-这不是一个错误的答案。这是非常正确的。我在一家大型企业工作,有时我们需要通过服务器安装应用程序。然而,有时它不安装,所以我们需要手动安装它,然后配置它。我宁愿安装一个对注册表进行必要更改的小应用程序,只需单击一次即可完成。这可能是我使用过的每台企业级计算机的运行速度比类似的未配置计算机慢10倍的部分原因。在这种情况下,这没什么大不了的,但这是一个坏习惯,它会导致使用windows installer机制可以解决的问题。@Cocoa-这不是一个错误的答案。这是一个非常正确的答案。