我在我的程序的第一個安裝日期在註冊表中存儲一個值。 當我嘗試在Vista或Win 7上以USER(非admmin)的形式讀取此值時,出現錯誤,說我沒有足夠的權限來讀取註冊表?從註冊表中讀取Vista/win上的用戶7
如何以用戶身份讀取註冊表,或者如何保存安裝日期?
我在我的程序的第一個安裝日期在註冊表中存儲一個值。 當我嘗試在Vista或Win 7上以USER(非admmin)的形式讀取此值時,出現錯誤,說我沒有足夠的權限來讀取註冊表?從註冊表中讀取Vista/win上的用戶7
如何以用戶身份讀取註冊表,或者如何保存安裝日期?
你的安裝程序有問題,我會說。受限制的權利不應阻止用戶閱讀HKLM \ Software配置單元中的密鑰。請確保您沒有要求寫入權限,將False作爲RegistryKey.OpenSubKey()的第二個參數傳遞。
你說得對。用戶可以確實讀取註冊表值。見下面的測試代碼。謝謝! – 2010-04-27 18:43:49
你不能。您必須是管理員才能編輯註冊表。因此,無論是要求用戶以管理員身份安裝它,還是將安裝日期存儲在某個文件中。
我認爲問題在於他讀書時不寫字。 – Kelsey 2010-04-27 16:07:59
這不取決於註冊表的哪一部分正在寫入?當然,普通用戶可以讀取/寫入HKEY_CURRENT_USER配置單元? – 2010-04-27 16:11:02
您可以通過運行安裝程序或從非管理員用戶以admin身份打開註冊表。
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;
}
}
什麼蜂房/關鍵是你的努力讀書 ?諸如文件之類的註冊表鍵具有權限(右鍵單擊權限以查看它們),並且以管理員身份運行的安裝程序沒有與以用戶身份運行的程序相同的權限。 – 2010-04-27 16:10:54
您在註冊表中的哪個位置存儲該值?並且是您的安裝程序/您的應用程序32位/ 64位?您的安裝程序或應用程序是否以兼容模式運行? – 2010-04-27 16:11:51