473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 16536
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 some minor default settings are not preserved....
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 registry)? Best Regards, Marco
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 my class or structure and then write them away to...
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 viewed. I'm just wnodering what are my options in...
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 preferences is done per user. I know that there is a way...
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 positions. Tried using: ...
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 after saving them on a report or form, when...
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 settings) and came across a suggestion at merlyn.com to...
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 understand how to bind a control to a setting and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.