2010-04-27 58 views
0

我在我的程序的第一个安装日期在注册表中存储一个值。 当我尝试在Vista或Win 7上以USER(非admmin)的形式读取此值时,出现错误,说我没有足够的权限来读取注册表?从注册表中读取Vista/win上的用户7

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

+2

什么蜂房/关键是你的努力读书 ?诸如文件之类的注册表键具有权限(右键单击权限以查看它们),并且以管理员身份运行的安装程序没有与以用户身份运行的程序相同的权限。 – 2010-04-27 16:10:54

+0

您在注册表中的哪个位置存储该值?并且是您的安装程序/您的应用程序32位/ 64位?您的安装程序或应用程序是否以兼容模式运行? – 2010-04-27 16:11:51

回答

1

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

+0

你说得对。用户可以确实读取注册表值。见下面的测试代码。谢谢! – 2010-04-27 18:43:49

0

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

+2

我认为问题在于他读书时不写字。 – Kelsey 2010-04-27 16:07:59

+4

这不取决于注册表的哪一部分正在写入?当然,普通用户可以读取/写入HKEY_CURRENT_USER配置单元? – 2010-04-27 16:11:02

0

您可以通过运行安装程序或从非管理员用户以admin身份打开注册表。

0
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text; 
using System.Windows.Forms; 
using Microsoft.Win32; 

namespace RegistryTest1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      WriteKey(); 
     } 


     private void button2_Click(object sender, EventArgs e) 
     { 
      ReadKey(); 
     } 

     private string keyPath = "SOFTWARE\\My Registry Key"; 
     private string keyName = "TestKeyName"; 
     private string keyValue = "TestValue"; 
     private void WriteKey() 
     { 
      Registry.LocalMachine.CreateSubKey(keyPath); 
      RegistryKey myKey = Registry.LocalMachine.OpenSubKey(keyPath, true); 
      myKey.SetValue(keyName, keyValue, RegistryValueKind.String); 
     } 

     private void ReadKey() 
     { 
      RegistryKey myKey = Registry.LocalMachine.OpenSubKey(keyPath, checkBox1.Checked); 
      string myValue = (string)myKey.GetValue(keyName); 
      textBox1.Text = myValue; 
     } 

     private void button3_Click(object sender, EventArgs e) 
     { 
      textBox1.Text = ""; 
     } 
    } 
} 

设计师代码: ..........................

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