473,734 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 IsolatedStorage File and
IsolatedStorage FileStream, which works correctly. That is, it stores
the settings in XML format to a file located here: "C:\Documen ts and
Settings\userid \Local Settings\Applic ation Data\IsolatedSt orage".
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 16566
Hi Kirk,

The reason why the IsolatedStorage File 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.co nfig). 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" ?>
<configuratio n>
<add key="MyKey1" value="My value 1" />
<add key="AnotherKey 2" value="Another value 2" />
</configuration>

From your code you can read the settings like so:

Dim mySetting As String =
Configuration.C onfigurationSet tings.AppSettin gs("MyKey1")

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******** *************@z 14g2000cwz.goog legroups.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 IsolatedStorage File and
IsolatedStorage FileStream, which works correctly. That is, it stores
the settings in XML format to a file located here: "C:\Documen ts and
Settings\userid \Local Settings\Applic ation Data\IsolatedSt orage".
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 IsolatedStorage File and
IsolatedStorage FileStream, which works correctly. That is, it stores
the settings in XML format to a file located here: "C:\Documen ts and
Settings\userid \Local Settings\Applic ation Data\IsolatedSt orage".
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\"Appli cation 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******** *************@z 14g2000cwz.goog legroups.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 IsolatedStorage File and
IsolatedStorage FileStream, which works correctly. That is, it stores
the settings in XML format to a file located here: "C:\Documen ts and
Settings\userid \Local Settings\Applic ation Data\IsolatedSt orage".
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 MyOptionsManage r as CConfigurationM anager
MyOptionsManage r = CConfigurations Manager.Instanc e()

Reading:
Dim SettingA = MyOptionsManage r.SettingA

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

Writing (method 2):
MyOptionsManage r.UpdateSetting ("SettingA",MyV alue")

This last option basically illustrates the use of a method in the
CConfigurationM anager 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.Seria lize(s, t)
'Deserialize the memorystream into the new structure ot
s.Seek(0, SeekOrigin.Begi n)
ot = formatter.Deser ialize(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
1906
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. Reestablishing some default settings and settings is a small price to pay for a set of text files that may save your database from total loss #AND# seem to remove the database bloat as well. I was wondering if someone knew how to preserve the...
3
1097
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
1514
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 the config file. thanks for your help in advance --
4
2376
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 saving these settings (so they could be reloaded next time the user loads the page). Writing to registry, a file, or a cookie? what's the right approach?
4
12471
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
2936
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 of saving it to some path under "Documents and Settings/UserName/...." , but I don't know the correct approach (e.g. path naming convention, how to preserve this per user, etc.) I can use all the assistance I can get (code snippets and some...
0
1570
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: ToolStripManager.SaveSettings(Me) in form.closing with ToolStripManager.LoadSettings(Me) in form load
3
2206
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 viewing them on other Vista computers. I have tried many things see Example: Open my report in Design mode. Select Page Setup. On the Page tab, alongside Use Specific Printer, press the Printer button. Having selected your printer, press Properties....
27
6887
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 test the time zone offset on a variety of dates to see if it changes. Based on that, I developed the following checkDST() function which, as far as I can tell, should be sufficient. It checks either the date passed to it or the current date with...
2
1152
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 also how to save settings manually. The only thing I havent beeen able to find is how to do a massive amount of controls in one swoop. Say on my app i have 45 checkboxes and i want to save their settings (checked, enabled, etc) how would i do that...
0
8951
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9317
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8192
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6040
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4818
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3266
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.