You could also use Isolated Storage (System.IO.IsolatedStorage) - its a
private virtual store (physically held within a user's "Documents And
Settings" area). The trouble with the registry is that the user/app need
permissions to access. Also, .NET apps running in a secure sandbox, for
example executing off a web site, have limited rights.
However, Isolated Storage is available to most .net apps and its seen as a
safe place to put stuff since apps and uses can't interfere with each other.
using a config class to store properties etc., this is serialised into a
file and stored. To get it back, use the Deserialise method and voila - the
class is back.
See the .net tool StoreAdm to manpilate the contents of users
IsolatedStorage.
Hope this helps
Shaun
[System.Serializable]
public class config
{
public string DatabaseName;
public string ServerName;
}
public void Save()
{
// create a handle onto the store for this user/assembly
System.IO.IsolatedStorage.IsolatedStorageFile
file=System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(System.IO.Isolat
edStorage.IsolatedStorageScope.Domain |
System.IO.IsolatedStorage.IsolatedStorageScope.User |
System.IO.IsolatedStorage.IsolatedStorageScope.Assembly,null,null);
// binary formatter will be responsible for converting my class to a binary
representation that can be put in a file
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter=new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
System.IO.IsolatedStorage.IsolatedStorageFile
file=System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(System.IO.Isolat
edStorage.IsolatedStorageScope.Domain |
System.IO.IsolatedStorage.IsolatedStorageScope.User |
System.IO.IsolatedStorage.IsolatedStorageScope.Assembly,null,null);
// open (create) a file in the store
System.IO.IsolatedStorage.IsolatedStorageFileStream stream=new
System.IO.IsolatedStorage.IsolatedStorageFileStream("MyFile.Dat",System.IO.F
ileMode.Create,System.IO.FileAccess.Write,System.IO.FileShare.None,file);
// convert class into the file stream
formatter.Serialize(stream,this);
stream.Close();
file.Close();
}
Paul Glavich said:
I would generally use a config file unless you have some sensitive
information to store (but the configuration application block released by
microsoft can help here) as its much easier and has broader support. The
registry is a good central place to hold things but yu have to make sure
your process/user has enough privileges to read the specified registry key.
Also, when hosting components in COM+, you either need to have the config
file in \windows\system32 (which I dont like) or have the settings in the
registry (which is what I typically do when hosting components in COM+).
- Paul Glavich
registry