469,327 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Saving Settings in VB.NET

Hello,

I am trying to find a VB.NET method that will allow me to save and read
an application's settings in a separate file located on any drive,
any folder. I have found a couple of methods for doing this, but they
are (in my opinion) more complicated than I require.

I found a method that uses IsolatedStorageFile and
IsolatedStorageFileStream, which works correctly. That is, it stores
the settings in XML format to a file located here: "C:\Documents and
Settings\userid\Local Settings\Application Data\IsolatedStorage".
While this method works, the Isolated Storage file does not allow you
to specify a file location other than this folder. As I mentioned, I
need to be able to specify a file location elsewhere (even on another
drive).

There HAS to be a method to save simple settings & retrieve them
without loading the entire file, then searching through all of it for
my one setting. When I say simple settings, I mean "File1=text.txt,
Color=234, FileDL = True".

I have tried several methods and they are all too complicated to
mention here. I would GREATLY appreciate any suggestions of how to do
this simply.
Thank you.

Reference material:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Nov 21 '05 #1
7 16316
Hi Kirk,

The reason why the IsolatedStorageFile is placed in that specific location
is because any user, even one with a least privledged account, has access to
that folder. If you read\write to another location, you have to make sure
the user has the proper security permissions. Another easy place to put
application settings is by creating an App.config file in your project. When
you compile the project, the config file is named after your application
name (appname.exe.config). However this file is always located in the same
place as the executable and there are only methods for easy reading of the
settings. To create the app.config file: Select Project -> Add New Item ->
Application Configuration File. You'll then want to add your key/value pairs
to the file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<add key="MyKey1" value="My value 1" />
<add key="AnotherKey2" value="Another value 2" />
</configuration>

From your code you can read the settings like so:

Dim mySetting As String =
Configuration.ConfigurationSettings.AppSettings("M yKey1")

However, if you want to write to the file, you'll have to do that manually.
Take a look here for info on how to do that:
http://ryanfarley.com/blog/archive/2...te-nav-9056600

HTH,
-B

"Kirk" <lo****@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hello,

I am trying to find a VB.NET method that will allow me to save and read
an application's settings in a separate file located on any drive,
any folder. I have found a couple of methods for doing this, but they
are (in my opinion) more complicated than I require.

I found a method that uses IsolatedStorageFile and
IsolatedStorageFileStream, which works correctly. That is, it stores
the settings in XML format to a file located here: "C:\Documents and
Settings\userid\Local Settings\Application Data\IsolatedStorage".
While this method works, the Isolated Storage file does not allow you
to specify a file location other than this folder. As I mentioned, I
need to be able to specify a file location elsewhere (even on another
drive).

There HAS to be a method to save simple settings & retrieve them
without loading the entire file, then searching through all of it for
my one setting. When I say simple settings, I mean "File1=text.txt,
Color=234, FileDL = True".

I have tried several methods and they are all too complicated to
mention here. I would GREATLY appreciate any suggestions of how to do
this simply.
Thank you.

Reference material:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Nov 21 '05 #2
Set up a Class containing all your start-up information as properties and
make sure the class is serializable. You can then serialize it to a file
located anywhere you want then de-serialize from the file on start-up of your
appliciation. Note however that this requires that you know where the file
is stored as you will have to hard code the path into your appliciation.

"Kirk" wrote:
Hello,

I am trying to find a VB.NET method that will allow me to save and read
an application's settings in a separate file located on any drive,
any folder. I have found a couple of methods for doing this, but they
are (in my opinion) more complicated than I require.

I found a method that uses IsolatedStorageFile and
IsolatedStorageFileStream, which works correctly. That is, it stores
the settings in XML format to a file located here: "C:\Documents and
Settings\userid\Local Settings\Application Data\IsolatedStorage".
While this method works, the Isolated Storage file does not allow you
to specify a file location other than this folder. As I mentioned, I
need to be able to specify a file location elsewhere (even on another
drive).

There HAS to be a method to save simple settings & retrieve them
without loading the entire file, then searching through all of it for
my one setting. When I say simple settings, I mean "File1=text.txt,
Color=234, FileDL = True".

I have tried several methods and they are all too complicated to
mention here. I would GREATLY appreciate any suggestions of how to do
this simply.
Thank you.

Reference material:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Nov 21 '05 #3
Kirk,
You could also save your settings in the Registry.

Look at SaveSetting(), GetSetting() etc.
They write to CurrentUser\VB and VBA Program Settings\"Application Name"\

You can also build up a simple class to read or write to any Registry Key
(where you have permissions).

Doug

"Kirk" <lo****@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
Hello,

I am trying to find a VB.NET method that will allow me to save and read
an application's settings in a separate file located on any drive,
any folder. I have found a couple of methods for doing this, but they
are (in my opinion) more complicated than I require.

I found a method that uses IsolatedStorageFile and
IsolatedStorageFileStream, which works correctly. That is, it stores
the settings in XML format to a file located here: "C:\Documents and
Settings\userid\Local Settings\Application Data\IsolatedStorage".
While this method works, the Isolated Storage file does not allow you
to specify a file location other than this folder. As I mentioned, I
need to be able to specify a file location elsewhere (even on another
drive).

There HAS to be a method to save simple settings & retrieve them
without loading the entire file, then searching through all of it for
my one setting. When I say simple settings, I mean "File1=text.txt,
Color=234, FileDL = True".

I have tried several methods and they are all too complicated to
mention here. I would GREATLY appreciate any suggestions of how to do
this simply.
Thank you.

Reference material:
http://msdn.microsoft.com/msdnmag/is...s/default.aspx

Nov 21 '05 #4
Dennis,

Thank you for your reply - it sounds like my best option. I appreciate
the other people who replied, unfortunately, the App.Config & Registry
solutions will NOT work for me, as I need something that will read
settings NOT in a fixed location (as I mentioned in my post).

Unfortunately, I do not have a lot of experience with creating a
serializable class or serializing it to a file. I tried researching
this in the .NET help file, but could not get their example to work
properly. I know I am asking a lot, but could you please post some
sample code or a link to somewhere an example is shown? I would
GREATLY appreciate it.
Thanks in advance!

Nov 21 '05 #5
I've recently tackled this problem and the solution was a blend of XML
serialization with options to write to Isolated Storage locations or on a
machine with greater access privleges -- write to a location specified by
the user. I created a manager class to allow the user to change and update
configurations setting from main application and a serializer/deserlializer
class to handle the reading/writing to file. Now, to counter the event that
the settings are wiped, a default settings file is stored in the
application as a resource, which the application will read should the
settings file be missing/corrupted/not present. This has worked well for me
on various accounts with liberal, conservative, and limited access to file
storage.

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #6
If you use serialization, you'll be deserializing to an object which has
all the properties keys/values of your settings file. Then, it is only a
matter of accessing the property:

FYI, not to make things too complicated, I would reccomend a singleton
implementation of a manager class to handle the settings.

Dim MyOptionsManager as CConfigurationManager
MyOptionsManager = CConfigurationsManager.Instance()

Reading:
Dim SettingA = MyOptionsManager.SettingA

Writing (method 1):
MyOptionsManager.SettingA = MyValue 'MyValue being a variable = setting
value

Writing (method 2):
MyOptionsManager.UpdateSetting("SettingA",MyValue" )

This last option basically illustrates the use of a method in the
CConfigurationManager to allow the user to specifiy the setting and the
value. I reccomend the first method, but the second method certainly has
it's uses.

All in all, it IS really, really simple to maintain application settings,
and I wouldn't be concerned about reading the entire file for one setting.
It's either methods like this or writing a wrapper to handle INI file
settings -- tha's ugly.

--
Message posted via http://www.dotnetmonster.com
Nov 21 '05 #7
The below is an example of serializing and deserializing a structure to a
memorystream which you could then save to a file and read from a file. You
should also be able to do this with a Class's properties that is declared as
serializable.

'Sturcture to be Serialized
<Serializable()> Public Structure TestSerialize
Dim i As Integer
Dim a As String
Dim b As Byte()
End Structure
sub Main

'Set t as the input structure and ot as the output structure (both can be
the same if you wish)
Dim t As TestSerialize
Dim ot As TestSerialize
'Create a BinaryFormatter to serialize the structure and a memory stream
to save it to
Dim formatter As New BinaryFormatter
Dim s As New MemoryStream
'Put some values into the structure
Dim i As Integer
t.i = 125
t.a = "This is a test of Serialilzing a structure"
t.b = New Byte() {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25}
'Serialize the structure t to memorystream s
formatter.Serialize(s, t)
'Deserialize the memorystream into the new structure ot
s.Seek(0, SeekOrigin.Begin)
ot = formatter.Deserialize(s)
i = 0
End Sub

"Kirk" wrote:
Dennis,

Thank you for your reply - it sounds like my best option. I appreciate
the other people who replied, unfortunately, the App.Config & Registry
solutions will NOT work for me, as I need something that will read
settings NOT in a fixed location (as I mentioned in my post).

Unfortunately, I do not have a lot of experience with creating a
serializable class or serializing it to a file. I tried researching
this in the .NET help file, but could not get their example to work
properly. I know I am asking a lot, but could you please post some
sample code or a link to somewhere an example is shown? I would
GREATLY appreciate it.
Thanks in advance!

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Marco Roberto | last post: by
7 posts views Thread by timb | last post: by
4 posts views Thread by Kourosh | last post: by
2 posts views Thread by Tal Shachar | last post: by
reply views Thread by =?Utf-8?B?VHJhY2tz?= | last post: by
27 posts views Thread by RobG | last post: by
2 posts views Thread by AGP | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.