Results 1 to 12 of 12

Thread: [RESOLVED] [2005] My.Settings Not So Friendly!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Resolved [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.

  2. #2
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    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.
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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.

  4. #4
    Hyperactive Member josep's Avatar
    Join Date
    Sep 2006
    Location
    Barcelona
    Posts
    409

    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?
    Useful links:DB connection strings ADO.NET VB.NET Tutorials

    • Don't forget to close the thread if solved
    • If this post helps you rate it

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim config As Configuration.Configuration = Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None)
    2.  
    3. config.AppSettings.Settings("setting name here").Value = "some value"
    4. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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!

  7. #7
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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:
    1. Dim config As Configuration.Configuration = Configuration.ConfigurationManager.OpenExeConfiguration(Configuration.ConfigurationUserLevel.None)
    2.         config.AppSettings.Settings.Add("setting name here", "some value")
    3.         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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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:
    1. Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
    2.         'MessageBox.Show(cls_XMLReadWrite.readFromXML("c:\settings.xml", "name"))
    3.         Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    4.         config.AppSettings.Settings.Add("strStarter", "frmMain")
    5.         config.Save()
    6.  
    7.     End Sub

    Then i have this in my Application Events
    VB Code:
    1. Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    2.             If My.Settings.strStarter = "frmWiz" Then
    3.                 Using _frmWiz As New frmWiz
    4.                     If _frmWiz.ShowDialog() = DialogResult.Cancel Then
    5.                         e.Cancel = True
    6.                     End If
    7.                 End Using
    8.             ElseIf My.Settings.strStarter = "frmMain" Then
    9.                 'Loads the splash form first before the Login form.
    10.                 Using _frmSplash As New frmSplash
    11.                     'Open the splash Screen
    12.                     _frmSplash.ShowDialog()
    13.                     _frmSplash.Dispose()
    14.                 End Using
    15.  
    16.                 'Loads the Login form first After the Splash form.
    17.                 Using _frmLogin As New frmLogin
    18.                     'Open the Login Form
    19.                     If _frmLogin.ShowDialog() = DialogResult.Cancel Then
    20.                         e.Cancel = True
    21.                     End If
    22.                 End Using
    23.             End If
    24.         End Sub

  9. #9
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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).

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    Re: [2005] My.Settings Not So Friendly!

    Correct!. Thanks. so i used
    config.AppSettings.Settings.Item("strStarter").Value = "frmMain"
    To access it.

  11. #11
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [RESOLVED] [2005] My.Settings Not So Friendly!

    Sounds like you have things sorted out now

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    May 2006
    Posts
    426

    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:
    1. Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    2.             If ConfigurationManager.AppSettings("strStarter") = "frmMain" Then
    3.                 'Loads the splash form first before the Login form.
    4.                 Using _frmSplash As New frmSplash
    5.                     'Open the splash Screen
    6.                     _frmSplash.ShowDialog()
    7.                     _frmSplash.Dispose()
    8.                 End Using
    9.  
    10.                 'Loads the Login form first After the Splash form.
    11.                 Using _frmLogin As New frmLogin
    12.                     'Open the Login Form
    13.                     If _frmLogin.ShowDialog() = DialogResult.Cancel Then
    14.                         e.Cancel = True
    15.                     End If
    16.                 End Using
    17.             Else
    18.                 Using _frmWiz As New frmWiz
    19.                     If _frmWiz.ShowDialog() = DialogResult.Cancel Then
    20.                         e.Cancel = True
    21.                     End If
    22.                 End Using
    23.             End If
    24.         End Sub

    On the Finish Button i used the code below as JMC had suggested.
    VB Code:
    1. Private Sub btnFinish_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFinish.Click
    2.         Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    3.         config.AppSettings.Settings.Item("strStarter").Value = "frmMain"
    4.         config.Save()
    5.     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
  •  



Click Here to Expand Forum to Full Width