C# 在Vista/win 7上以用户身份从注册表读取

C# 在Vista/win 7上以用户身份从注册表读取,c#,.net,registry,C#,.net,Registry,我正在注册表中存储一个值,其中包含程序的第一个安装日期。 当我在Vista或Win 7上尝试以用户(非admmin)身份读取此值时,我收到一个错误,说我没有足够的权限读取注册表 如何以用户身份读取注册表,或者如何保存安装日期?您不能。您必须是管理员才能编辑注册表。因此,要么要求用户以管理员的身份安装,要么只是将安装日期存储在某个文件中。您可以通过运行安装来实现这一点,或者从非管理员用户处以管理员的身份打开注册表。我想说,您的安装程序有问题。受限权限不应阻止用户读取HKLM\软件配置单元中的密钥。

我正在注册表中存储一个值,其中包含程序的第一个安装日期。 当我在Vista或Win 7上尝试以用户(非admmin)身份读取此值时,我收到一个错误,说我没有足够的权限读取注册表


如何以用户身份读取注册表,或者如何保存安装日期?

您不能。您必须是管理员才能编辑注册表。因此,要么要求用户以管理员的身份安装,要么只是将安装日期存储在某个文件中。

您可以通过运行安装来实现这一点,或者从非管理员用户处以管理员的身份打开注册表。

我想说,您的安装程序有问题。受限权限不应阻止用户读取HKLM\软件配置单元中的密钥。请确保没有请求写入权限,将False作为第二个参数传递给RegistryKey.OpenSubKey()

设计器代码:

namespace RegistryTest1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(13, 13);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(641, 20);
            this.textBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 53);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Write key";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(94, 53);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "Read key";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(185, 57);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(112, 17);
            this.checkBox1.TabIndex = 3;
            this.checkBox1.Text = "writable parameter";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(13, 104);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 4;
            this.button3.Text = "reset";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(676, 264);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.Button button3;
    }
}
名称空间注册表test1
{
部分类Form1
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.textBox1=new System.Windows.Forms.TextBox();
this.button1=new System.Windows.Forms.Button();
this.button2=new System.Windows.Forms.Button();
this.checkBox1=new System.Windows.Forms.CheckBox();
this.button3=new System.Windows.Forms.Button();
这个.SuspendLayout();
// 
//文本框1
// 
this.textBox1.Location=新系统.图纸.点(13,13);
this.textBox1.Name=“textBox1”;
this.textBox1.Size=新系统.Drawing.Size(641,20);
this.textBox1.TabIndex=0;
// 
//按钮1
// 
this.button1.Location=新系统图纸点(13,53);
this.button1.Name=“button1”;
this.button1.Size=新系统图纸尺寸(75,23);
this.button1.TabIndex=1;
this.button1.Text=“写入键”;
this.button1.UseVisualStyleBackColor=true;
this.button1.Click+=新系统.EventHandler(this.button1\u Click);
// 
//按钮2
// 
this.button2.Location=新系统图纸点(94,53);
this.button2.Name=“button2”;
this.button2.Size=新系统图纸尺寸(75,23);
this.button2.TabIndex=2;
this.button2.Text=“读取键”;
this.button2.UseVisualStyleBackColor=true;
this.button2.Click+=新系统.EventHandler(this.button2\u Click);
// 
//复选框1
// 
this.checkBox1.AutoSize=true;
this.checkBox1.Location=新系统图纸点(185,57);
this.checkBox1.Name=“checkBox1”;
this.checkBox1.Size=新系统图纸尺寸(112,17);
this.checkBox1.TabIndex=3;
this.checkBox1.Text=“可写参数”;
this.checkBox1.UseVisualStyleBackColor=true;
// 
//按钮3
// 
this.button3.Location=新系统图纸点(13,104);
this.button3.Name=“button3”;
this.button3.Size=新系统图纸尺寸(75,23);
this.button3.TabIndex=4;
this.button3.Text=“重置”;
this.button3.UseVisualStyleBackColor=true;
this.button3.Click+=新系统.EventHandler(this.button3\u Click);
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(676264);
this.Controls.Add(this.button3);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name=“Form1”;
this.Text=“Form1”;
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button按钮1;
private System.Windows.Forms.Button按钮2;
private System.Windows.Forms.CheckBox复选框1;
private System.Windows.Forms.Button按钮3;
}
}

您要读取的配置单元/键是什么?注册表项(如文件)具有权限(右键单击以查看它们的权限),并且以管理员身份运行的安装程序与以用户身份运行的程序没有相同的权限。您在注册表中的何处存储该值?您的安装程序/应用程序是32位还是64位?您的安装程序或应用程序是否在兼容模式下运行?这不取决于正在写入注册表的哪个部分吗?当然,普通用户可以读/写HKEY_当前用户配置单元?你是对的。用户确实可以读取注册表值。请参阅下面的测试代码。谢谢
namespace RegistryTest1
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.textBox1 = new System.Windows.Forms.TextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.button3 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(13, 13);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(641, 20);
            this.textBox1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(13, 53);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Write key";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(94, 53);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 2;
            this.button2.Text = "Read key";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(185, 57);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(112, 17);
            this.checkBox1.TabIndex = 3;
            this.checkBox1.Text = "writable parameter";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(13, 104);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 4;
            this.button3.Text = "reset";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(676, 264);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.Button button3;
    }
}