Solved

Registry Editing C#

Posted on 2004-09-01
9
1,147 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
White board coding practice 3 88
cmd.exe will not close when running .bat file to perform FTP upload 18 103
C Programming - If Statement 8 78
Query Syntax 17 36
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
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…

803 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