Solved

Registry Editing C#

Posted on 2004-09-01
9
1,140 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
Comment Utility
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 22

Expert Comment

by:cookre
Comment Utility
Gotta love cross posts...
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
C# looks so odd to me...

=)
Idle_Mind
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
Comment Utility
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
mergeTwo  challenge 13 71
Magic Software info 18 101
firstChar challenge 13 83
Adding Namespace to the second node of the xml 16 30
A short article about a problem I had getting the GPS LocationListener working.
This is an explanation of a simple data model to help parse a JSON feed
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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…

743 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

17 Experts available now in Live!

Get 1:1 Help Now