Solved

Registry Editing C#

Posted on 2004-09-01
9
1,158 Views
Last Modified: 2013-11-13
I want to edit the registry through a C# program . I want somebody to tell the class used for that . I don't want to use the registry editor for the deployment /setup tool . Also , I don't want to use a pre written .reg file
0
Comment
Question by:drtoto82
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
9 Comments
 
LVL 22

Accepted Solution

by:
cookre earned 125 total points
ID: 11960193
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
ID: 11960199
The Registry Class is in the Microsoft.Win32 namspace.

You use it by retrieving a RegistryKey instance representing one of the base level keys such as LocalMachine or CurrentUser.  It is through this RegistryKey instance that you can navigate keys and values and make any necessary changes.

Below is an example in VB.Net that shows how to get a listing of installed software on a system by enumerating the keys in the "LocalMachine\Software\Microsoft\Windows\CurrentVersion\Uninstall" key.

Regards,

Idle_Mind

Imports Microsoft.Win32

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.ListBox1.Location = New System.Drawing.Point(8, 8)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(288, 160)
        Me.ListBox1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(304, 182)
        Me.Controls.Add(Me.ListBox1)
        Me.Name = "Form1"
        Me.Text = "Add/Remove Program Listing"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim rk As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Uninstall")
            Dim sk As String
            Dim appKey As RegistryKey
            Dim displayNameValue As Object
            Dim displayName As String
            ListBox1.Sorted = True
            ListBox1.BeginUpdate()
            If Not (rk Is Nothing) Then
                For Each sk In rk.GetSubKeyNames
                    appKey = rk.OpenSubKey(sk)
                    displayNameValue = appKey.GetValue("DisplayName")
                    If Not (displayNameValue Is Nothing) Then
                        ListBox1.Items.Add(CType(displayNameValue, String))
                    End If
                Next
            End If
            ListBox1.EndUpdate()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Information, "Error Accessing Registry")
        End Try
    End Sub

End Class
0
 
LVL 22

Expert Comment

by:cookre
ID: 11960205
For example:

// Add service description if service is there
reg=Registry.LocalMachine.OpenSubKey("System\\CurrentControlSet\\Services\\DskMonSvc",true);
if (reg!=null)
   {
   try {
        reg.SetValue("Description","Disk monitor");
        reg.Close();
        }
   catch (Exception e)
           {
           LogMsg("svcd: <<<"+e.ToString()+">>>");
           return;
           }
   }
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:cookre
ID: 11960206
Gotta love cross posts...
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11960219
C# looks so odd to me...

=)
Idle_Mind
0
 
LVL 22

Expert Comment

by:cookre
ID: 11960457
If I know my audience has the .NET framework and I'm not doing a lot of bit fiddling, it's my language of choice.  Development goes sooo much faster by virtue of both the language and the IDE.  Shoot, the ToString() by itself is almost worth the price of admission.

It does, however, have its dark side.  It has greatly simplified many tasks that used to be only in the pervue of more, uh, seasoned programmers, such as memory management, services, and threading.  Alas, that very simplification has lured in many who have no idea of what they're doing and the impact of their actions on, for example, a heavily loaded box.

I shouldn't complain, however.  I've garnered a whole squat pot full of points here just from Q's about failing services in c#.
0
 
LVL 3

Expert Comment

by:IceColdas
ID: 11961560
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Progress
Starting up a Project

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question