Registry Questions

tbonejo

Member
Joined
Apr 22, 2005
Messages
9
Programming Experience
3-5
Ive looked over the docs and other threads but cant seem to make heads or tails as how to create registry items in the current user acct under software. Im just looking for a quick and easy solution to loads some paths and other info from. All the xml stuff looks too huge to write simple 5 lines of info so I figure the registry is the way to go. Am I going in the right direction for my needs. Also I have tried some code posted here but they all error saying I have no namespace and what not available. Any thoughts greatly appreciated.


Thanks.
 
If you get namespace error... probably you miss putting this:
VB.NET:
[color=Blue]Imports [/color]Microsoft.Win32

And I attached here a class I found somewhere on the net to read/write to the registry. It worked for me... and you can find easy instruction inside. Just remove the .txt extension.
 

Attachments

  • CRegistry.vb.txt
    8 KB · Views: 38
I would suggest using XML or some other file based storage for app settings/info. Using the registry is acceptable but there is the chance that a user may not have the necessary permissions to read from or write to the registry.
Sure the code seems long to write XML depending on how you go about it, but there are reusable classes already existing that you can add to your project.
.NET Framework V2 has built in classes used for persisting app settings and they utilize XML.
 
.NET app's are supposed to avoid the registry if possible. Take a look at this article for the recommended method of persisting settings data. Alternatively you could serialise an object to an XML file, which takes four lines of code to save and four lines of code to load, or just use a text file.
 
How do you serialize an object like that to write to xml.

Just for clarity-I am looking to save the info out of a few textboxes that the user puts in and that will be able to load back up when the app loads again.

XML definitely sounds great and all and I have used it elsewhere but it seems so complex with .net thats it appears to me to be to much of a pain for such a small amount of info. If someone could steer me in the right direction for this Id appreciate it.

I thought if the current user logged in would definitely have permissions to write to those(Current user) keys in the registry?
 
You would first have to define your own class that stored the values you want to save in public properties. You would then serialise and deserialise an instance like this:
VB.NET:
	Public Sub Serialise(ByVal obj As Class1, ByVal path As String)
		Dim stream As IO.FileStream = IO.File.Create(path)
		Dim serialiser As New Xml.Serialization.XmlSerializer(GetType(Class1))

		serialiser.Serialize(stream, obj)
		stream.Close()
	End Sub

	Public Function Deserialise(ByVal path As String) As Class1
		Dim stream As IO.FileStream = IO.File.OpenRead(path)
		Dim serialiser As New Xml.Serialization.XmlSerializer(GetType(Class1))

		Try
			Return DirectCast(serialiser.Deserialize(stream), Class1)
		Finally
			stream.Close()
		End Try
	End Function
 
jmcilhinney said:
.NET app's are supposed to avoid the registry if possible. Take a look at this article for the recommended method of persisting settings data .....

Very good point John
palec.gif
 
Well if I only need to store the path to which a config file is to be loaded from, what would the best and most efficient way be?

Seems like the xml is hardly a convenient way to do so, just to store one line.

Id like to make it so the user can set the default file to use so every time they load the program it know what the user wants to use.
 
The easiest way to store a single value is in the config file. The config file is ALWAYS located in the same folder as the executable and is ALWAYS named the same as the executable, including the ".exe" extension, with an extra ".config" extension. .NET provides functionality to retrieve data from the config file without specifying the location, as it ALWAYS knows where it will be and what it will be called. The article I previously linked to details how to retrieve data from and save data to the config file, which is why I provided that link in the first place.
 
Back
Top