文章编号: 842795 - 最后修改: 2006年11月3日 - 修订: 3.3

尝试创建自定义事件日志时,将会收到“Requested registry access is not allowed(不允许所请求的注册表访问权)”错误消息


重要说明:本文包含有关如何修改注册表的信息。修改注册表之前,一定要先进行备份,并且一定要知道在发生问题时如何还原注册表。有关如何备份、还原和修改注册表的更多信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
256986? (http://support.microsoft.com/kb/256986/ ) Microsoft Windows 注册表说明

本页

展开全部 | 关闭全部

症状

如果以普通用户身份登录计算机,并尝试使用 Microsoft Visual Studio .NET 创建自定义事件日志以注册事件,可能会收到以下错误消息:
An unhandled exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
Additional information:Requested registry access is not allowed.

原因

出现此问题的原因在于登录所用的用户帐户没有足够的权限。

首次调用 EventLog.CreateEventSource() 方法创建自定义事件日志时,将会在下面的注册表子项下创建自定义事件日志条目:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
要创建此子项条目,必须拥有写权限。但是,普通用户帐户没有写权限。因此,会收到“症状”部分中提到的错误消息。

替代方法

警告:如果使用注册表编辑器或其他方法错误地修改了注册表,则可能导致严重问题。这些问题可能需要重新安装操作系统才能解决。Microsoft 不能保证可以解决这些问题。修改注册表需要您自担风险。
要解决此问题,请使用下列方法之一:
  • 授予创建自定义事件日志的权限
  • 以管理员身份安装自定义事件日志

授予创建自定义事件日志的权限

  1. 以管理员身份登录计算机。
  2. 依次单击“开始”和“运行”,在“打开”框中键入 regedit,然后单击“确定”。这将打开“注册表编辑器”窗口。
  3. 找到以下注册表子项:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Eventlog
  4. 右键单击“Eventlog”,然后单击“权限”。这将显示“Eventlog 的权限”对话框。
  5. 单击“高级”。此时显示“Eventlog 的高级安全设置”对话框。
  6. 在“名称”列中,双击“Users”组。这将显示“Eventlog 的权限项目”对话框。
  7. 依次选中“设置数值”和“创建子项”复选框,然后单击“确定”。
  8. 退出注册表编辑器,然后从管理员帐户中注销。
  9. 以普通用户身份登录计算机。
  10. 尝试使用 Visual Studio .NET 创建自定义事件日志,然后尝试使用基于 Visual Studio .NET 构建的应用程序写入此事件日志。

以管理员身份安装自定义事件日志

  1. 以管理员身份登录计算机。
  2. 使用 Visual Studio .NET 中的 CreateEventSource 方法创建自定义事件日志和事件日志源。
  3. 从管理员帐户注销。
  4. 以普通用户身份登录计算机。
  5. 通过运行应用程序将条目写入自定义事件日志。

更多信息

重现问题的步骤

  1. 创建普通用户帐户:
    1. 以管理员身份登录计算机。
    2. 右键单击“我的电脑”,然后单击“管理”。这将显示“计算机管理”窗口。
    3. 在“系统工具”下,展开“本地用户和组”。
    4. 右键单击“用户”,然后单击“新用户”。这将显示“新用户”对话框。
    5. 在“用户名”框中键入新帐户的名称,然后分别在“密码”和“确认密码”框中键入一个密码。
    6. 清除“用户下次登录时必须更改密码”复选框,选中“密码永不过期”复选框,然后单击“创建”。
    7. 单击“关闭”,关闭“新用户”对话框。
  2. 将用户添加到“Debugger User”组:
    1. 在“本地用户和组”下,单击“用户”。
    2. 在“计算机管理”窗口右侧窗格的“名称”列中,右键单击新帐户的名称,然后单击“属性”。这将显示新帐户的属性对话框。
    3. 单击“隶属于”选项卡,然后单击“添加”。这将显示“选择组”对话框。
    4. 单击“高级”。这将显示“选择组”对话框。
    5. 单击“立即查找”。可用的组将显示在“选择组”对话框中。
    6. 在“名称 (RDN)”列中,单击“Debugger Users”,然后单击“确定”。
    7. 在“选定组”对话框中,单击“确定”。
    8. 在新帐户的属性对话框中,单击“确定”。
    9. 从管理员帐户注销。
  3. 创建 Windows 应用程序:
    1. 使用在“步骤 1 - 创建普通用户帐户”中创建的普通用户帐户登录计算机。
    2. 启动 Visual Studio .NET。
    3. 在“文件”菜单上,指向“新建”,然后单击“项目”。此时显示“新建项目”对话框。
    4. 在“项目类型”下,单击“Visual C# 项目”。
    5. 在“模板”下,单击“Windows 应用程序”。
    6. 在“名称”框中,键入项目的名称,然后单击“确定”。默认情况下,将会显示 Form1 窗体。
    7. 右键单击 Form1 窗体,然后单击“查看代码”。这将显示 Form1.cs 文件。
    8. 将现有代码替换为以下代码:
      using System;
      using System.Drawing;
      using System.Collections;
      using System.ComponentModel;
      using System.Windows.Forms;
      using System.Data;
      using System.Diagnostics;
      
      namespace WindowsApplication1
      {
      	/// <summary>
      	/// Summary description for Form1.
      	/// </summary>
      	public class Form1 : System.Windows.Forms.Form
      	{
      		private System.Diagnostics.EventLog eventLog1;
      		private System.Windows.Forms.Button button1;
      		/// <summary>
      		/// Required designer variable.
      		/// </summary>
      		private System.ComponentModel.Container components = null;
      
      	
      
      
      		public Form1()
      		{
      			//
      			// Required for Windows Form Designer support
      			//
      			InitializeComponent();
      
      			if(!EventLog.SourceExists("Source1"))
      			{
      				EventLog.CreateEventSource("Source1","MyLog1");
      			}
      			eventLog1.Source = "Source1";
      			eventLog1.Log = "MyLog1";
      			//
      			// TODO: Add any constructor code after InitializeComponent call
      			//
      		}
      		
      		
      		
      		
      		/// <summary>
      		/// Clean up any resources being used.
      		/// </summary>
      		protected override void Dispose( bool disposing )
      		{
      			if( disposing )
      			{
      				if (components != null) 
      				{
      					components.Dispose();
      				}
      			}
      			base.Dispose( disposing );
      		}
      
      		#region Windows Form Designer generated code
      		/// <summary>
      		/// Required method for Designer support - do not modify
      		/// the contents of this method with the code editor.
      		/// </summary>
      		private void InitializeComponent()
      		{
      			this.eventLog1 = new System.Diagnostics.EventLog();
      			this.button1 = new System.Windows.Forms.Button();
      			((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
      			this.SuspendLayout();
      			// 
      			// eventLog1
      			// 
      			this.eventLog1.SynchronizingObject = this;
      			// 
      			// button1
      			// 
      			this.button1.Location = new System.Drawing.Point(104, 104);
      			this.button1.Name = "button1";
      			this.button1.TabIndex = 0;
      			this.button1.Text = "button1";
      			this.button1.Click += new System.EventHandler(this.button1_Click);
      			// 
      			// Form1
      			// 
      			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      			this.ClientSize = new System.Drawing.Size(292, 273);
      			this.Controls.Add(this.button1);
      			this.Name = "Form1";
      			this.Text = "Form1";
      			((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
      			this.ResumeLayout(false);
      
      		}
      		#endregion
      
      		/// <summary>
      		/// The main entry point for the application.
      		/// </summary>
      		[STAThread]
      		static void Main() 
      		{
      			Application.Run(new Form1());
      		}
      		
      		private void button1_Click(object sender, System.EventArgs e)
      		{
      			eventLog1.WriteEntry("The Button1 Is Clicked");
      		}
      	}
      }
      
    9. 在“文件”菜单上,单击“保存 Form1.cs”保存该文件。
  4. 运行该项目:
    1. 在“生成”菜单上,单击“生成解决方案”。
    2. 运行该项目。此时,可能会收到“症状”部分中提到的错误消息。
注意:将在操作系统的事件日志记录 API 级别强制事件日志注册表子项的写入权限安全。默认情况下,自定义事件日志的权限与应用程序事件日志的权限相同。

参考

有关更多信息,请访问下面的 Microsoft Developer Network (MSDN) 网站:
创建和删除自定义事件日志
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingremovingcustomlogseventsources.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingremovingcustomlogseventsources.asp)

将条目写入事件日志
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskWritingEntriesToEventLogs.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskWritingEntriesToEventLogs.asp)

将应用程序添加为事件日志条目的源
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddingyourapplicationassourceofeventlogentries.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddingyourapplicationassourceofeventlogentries.asp)

事件日志记录安全
http://windowssdk.msdn.microsoft.com/en-us/library/ms684080.aspx (http://windowssdk.msdn.microsoft.com/en-us/library/ms684080.aspx)
有关更多信息,请单击下面的文章编号,以查看 Microsoft 知识库中相应的文章:
329291? (http://support.microsoft.com/kb/329291/ ) PRB:ASP.NET 应用程序试图在 EventLog 中写入新的 EventSource 时出现“Requested Registry Access Is Not Allowed”(不允许所请求的注册表访问)错误消息

这篇文章中的信息适用于:
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
关键字:?
kbtshoot kberrmsg kbregistry kbpermissions kbeventlog kbaccounts kbprb KB842795
Microsoft和/或其各供应商对于为任何目的而在本服务器上发布的文件及有关图形所含信息的适用性,不作任何声明。 所有该等文件及有关图形均"依样"提供,而不带任何性质的保证。Microsoft和/或其各供应商特此声明,对所有与该等信息有关的保证和条件不负任何责任,该等保证和条件包括关于适销性、符合特定用途、所有权和非侵权的所有默示保证和条件。在任何情况下,在由于使用或运行本服务器上的信息所引起的或与该等使用或运行有关的诉讼中,Microsoft和/或其各供应商就因丧失使用、数据或利润所导致的任何特别的、间接的、衍生性的损害或任何因使用而丧失所导致的之损害、数据或利润不负任何责任。
 

文章翻译

 

Related Support Centers