Solved

Registry Editing C#

Posted on 2004-09-01
9
1,144 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 125 total points
ID: 11960193
0
 
LVL 85

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
object oriented javascript web form 8 104
wordmultiple challenge 12 118
Device same like our heart 12 75
Java Loop 6 48
RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
This is about my first experience with programming Arduino.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

911 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now