472,331 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

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 16462
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: MLH | last post by:
GHudson has working procedures posted at http://www.access-programmers.co.uk/forums/showthread.php?t=66320 They work. Relationships, however, and...
3
by: Marco Roberto | last post by:
Hi, does anybody what is the best format to save particular system settings (ex: Default path to save files, etc) in .Net (XML, ini files, or...
7
by: timb | last post by:
Hi, is it possible to access the properties of a class or structure in a for each statement? I want to iterate through the stirng properties in...
4
by: Kourosh | last post by:
I have a log file that is viewed on the local machine. There are some settings on the html page that allow the user to specify how the file is...
4
by: Bill Nguyen | last post by:
How can I detect timezone and whether daylight saving is applied on a client PC/server using VB.NET? Thanks Bill
2
by: Tal Shachar | last post by:
Hi, I have a windows application that needs to save some user preferences to an xml and to load it on the next application run. Saving the...
0
by: =?Utf-8?B?VHJhY2tz?= | last post by:
Does the toolstripcontainer etc work at all? I want to save the settings for the toolstrip positions in my app so they start up with the saved...
3
by: RobEveryThingIsPossible | last post by:
IN VISTA MS Access 2000 Printer Settings Not Saving With Vista Bus. The printer settings (Label or Env.) keep on change back to another setting...
27
by: RobG | last post by:
I was investigating a function to determine whether daylight saving was being observed on a particular date (given the platform's regional...
2
by: AGP | last post by:
VB.NET 2005 i was reading some of the documentation on saving settings and i think i have a good grasp on things with the My.Settings classes. I...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.