|
-
Oct 17th, 2006, 05:50 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] My.Settings Not So Friendly!
My App has a wizard, this Wizard starts up and creates tables. When this wizard Finishes its work, i want it to store a value so that i use it to call a splash screen to startup the next time. i was trying to use My.Settings but, It is seeming so hard for me to store my settings for ALL Users! I wonder why My.Settings was created for storage of settings to the current user!.
The only workaround i am thinking of now, is to store my settings using the old methods...may be in another hidden place!
But if there is any Idea or workaround...let me know.
Last edited by maps; Oct 17th, 2006 at 06:16 AM.
-
Oct 17th, 2006, 06:03 AM
#2
Hyperactive Member
Re: [2005] My.Settings Not So Friendly!
Hi,
I think you checked
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VSADD.v10.en/dnvs05/html/vbmysettings.htm
Application-scoped settings are read-only and are shared between all users of that application. These settings are stored in the app.config file in the <applicationSettings> section.
User-scope settings are specific for each user. They can be read and set safely by the application code at run time. These settings are stored in a user.config file. To be technically accurate, there are two user.configs per user per application—one for non-roaming and one for roaming. Although the documentation available with the beta release of Visual Basic 2005 states that the user.config file will be named according to the user's name (joe.config), this is not the case. The user.config file is created in the <c:\Documents and Settings>\<username>\[Local Settings\]Application Data\<companyname>\<appdomainname>_<eid>_<hash>\<verison>.
User-scope settings are great for storing your application preferences, which are usually different for every user. Examples of user-scope settings are display settings, such as font size, window location, MRU lists, and so on.
This architecture is very flexible because it allows your application to store settings and preferences for each user even if your application is running in a partial-trust scenario.
Hi will check in dept this article, but as far as I understand that's what you need.
My.settings will create a new user.config file for each user. If you want to store all user configs onto one file, you will need to do it using the old way as you said
Last edited by josep; Oct 17th, 2006 at 06:08 AM.
-
Oct 17th, 2006, 06:15 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] My.Settings Not So Friendly!
My Objective is ti write to the file and read from it as well.
Application-scoped settings are read-only which implies i cant write or rather Edit to the file.
-
Oct 17th, 2006, 06:26 AM
#4
Hyperactive Member
Re: [2005] My.Settings Not So Friendly!
Hi,
Understood, sorry sometimes understanding english is not as easy as it seems
Yo can save it on the registry or using the old way.
Anyway your application will be installed on several machines (one per user) or just on one (for all users)?
and
Your wizard gives you settings for all the users or for each user individually?
-
Oct 17th, 2006, 06:59 AM
#5
Re: [2005] My.Settings Not So Friendly!
There is a reason that Application-scoped settings are read-only. Any Windows user could be using your application and users with limited accounts don't have write access to the Program Files folder, under which your app's config file is stored. That means that the settings mechanism cannot save Application-scoped settings for users with limited accounts. If you want to write to the main config file at run time you can do so though, through a Configuration object:
VB Code:
Dim config As Configuration.Configuration = Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None)
config.AppSettings.Settings("setting name here").Value = "some value"
config.Save()
I've never actually tried that myself so I can't guarantee that that's correct but it's pretty close at least. You should make sure that the current user is a member of the Administrators group first or else the save will fail for the aforementioned reason.
-
Oct 17th, 2006, 09:08 AM
#6
Thread Starter
Hyperactive Member
Re: [2005] My.Settings Not So Friendly!
Thanks JMC but i can't get this to work. it tells me Object reference not set to an instance of an object. where i set the value!
-
Oct 17th, 2006, 09:32 AM
#7
Re: [2005] My.Settings Not So Friendly!
maps, you need to reference system.configuration.dll.
Also the values should be added via the standard collection method of
VB Code:
Dim config As Configuration.Configuration = Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None)
config.AppSettings.Settings.Add("setting name here", "some value")
config.Save()
The null reference was coming from trying to access the "setting name here" key to set its value before it was actually created.
-
Oct 17th, 2006, 10:03 AM
#8
Thread Starter
Hyperactive Member
Re: [2005] My.Settings Not So Friendly!
Thanks...Kleinma but i am not getting it. I cant even see where the file is stored.
i have a reference to system.configuration.dll.
In my Settings i have. Name(strStarter) ,Type(string), Scope(Application),Value(frmWiz)
Then i have this code on the finish button of the Wizard.
VB Code:
Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
'MessageBox.Show(cls_XMLReadWrite.readFromXML("c:\settings.xml", "name"))
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.Settings.Add("strStarter", "frmMain")
config.Save()
End Sub
Then i have this in my Application Events
VB Code:
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
If My.Settings.strStarter = "frmWiz" Then
Using _frmWiz As New frmWiz
If _frmWiz.ShowDialog() = DialogResult.Cancel Then
e.Cancel = True
End If
End Using
ElseIf My.Settings.strStarter = "frmMain" Then
'Loads the splash form first before the Login form.
Using _frmSplash As New frmSplash
'Open the splash Screen
_frmSplash.ShowDialog()
_frmSplash.Dispose()
End Using
'Loads the Login form first After the Splash form.
Using _frmLogin As New frmLogin
'Open the Login Form
If _frmLogin.ShowDialog() = DialogResult.Cancel Then
e.Cancel = True
End If
End Using
End If
End Sub
-
Oct 17th, 2006, 10:08 AM
#9
Re: [2005] My.Settings Not So Friendly!
in your bin folder you should get a .config file after calling that code...
for example, if your app was testapp.exe and the project was located at c:\testapp and the exe compiled to c:\testapp\bin\debug then when your code runs it should create a .config file at
c:\testapp\bin\debug\testapp.vshost.exe.config
when you stop debugging this file will be deleted however....
if you run the stand alone exe (outside the ide) the file does not get deleted (and does not have the vshost in it).
-
Oct 17th, 2006, 10:22 AM
#10
Thread Starter
Hyperactive Member
Re: [2005] My.Settings Not So Friendly!
Correct!. Thanks. so i used
config.AppSettings.Settings.Item("strStarter").Value = "frmMain"
To access it.
-
Oct 17th, 2006, 10:25 AM
#11
Re: [RESOLVED] [2005] My.Settings Not So Friendly!
Sounds like you have things sorted out now
-
Oct 17th, 2006, 12:28 PM
#12
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] My.Settings Not So Friendly!
Actually, many steps to get there.
Below is what is in my ApplicationEvents. For you not to get an exception, you have to edit the app.config file to have an appSettings Node.
If you dont do this you will get an exception as i described in post #6.My.Settings does not add the node there automatically instead it adds an <applicationSettings> node.
VB Code:
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
If ConfigurationManager.AppSettings("strStarter") = "frmMain" Then
'Loads the splash form first before the Login form.
Using _frmSplash As New frmSplash
'Open the splash Screen
_frmSplash.ShowDialog()
_frmSplash.Dispose()
End Using
'Loads the Login form first After the Splash form.
Using _frmLogin As New frmLogin
'Open the Login Form
If _frmLogin.ShowDialog() = DialogResult.Cancel Then
e.Cancel = True
End If
End Using
Else
Using _frmWiz As New frmWiz
If _frmWiz.ShowDialog() = DialogResult.Cancel Then
e.Cancel = True
End If
End Using
End If
End Sub
On the Finish Button i used the code below as JMC had suggested.
VB Code:
Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.Settings.Item("strStarter").Value = "frmMain"
config.Save()
End Sub
Hope it helps someone else.
Last edited by maps; Oct 17th, 2006 at 12:49 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|