tags:

views:

179

answers:

4

I'm storing a value in the registry with the first install date of my program. When I try to read this value as a USER (non-admmin) on Vista or Win 7, i get an error saying I don't have enough rights to read the registry ??

How can I read registry as a user, or how should I save my install date?

A: 

You can't. You must be an admin to edit the registry. So either require the user to install it as an admin, or just store the install date in some file.

Joel
I think the problem is when he is reading not writing.
Kelsey
Doesn't it depend which part of the registry is being written to? Surely regular users can read/write to the HKEY_CURRENT_USER hive?
Daniel Renshaw
A: 

you can do that by running the installation or open the register as admin from non admin user.

Eyla
+1  A: 

Something wrong with your installer, I'd say. Restricted rights should not block users from reading keys in the HKLM\Software hive. Do make sure you didn't ask for write permissions, pass False as the 2nd argument to RegistryKey.OpenSubKey().

Hans Passant
You are right. Users CAN indeed read registry values. See test code below. Thanks!
Run CMD
A: 
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 = "";
        }
    }
}

Designer code : ..........................

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