Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Registry Editing C#

Posted on 2004-09-01
9
Medium Priority
?
1,160 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
  • 4
  • 2
9 Comments
 
LVL 22

Accepted Solution

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

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

916 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