Results 1 to 11 of 11

Thread: [RESOLVED] Save and load variable values from/to file

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Resolved [RESOLVED] Save and load variable values from/to file

    Hello.
    I've been literally googling for an hour now trying to find how to save variable settings to a file. Now i turn to you guys on this forum for help.
    I'm writing an application and I'm using alot of changeable variables. I want to be able to save these to a file so they can be loaded at application startup. I don't feel like using the regestry since I hate to write unimportant things to the registry.
    I remembered that I found a good example with simple code of how to do this with xml, but I can't seem to find the example now.

    Could someone please explain to me how to do this in an easy and smart way.

    Thank you

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

    Re: Save and load variable values from/to file

    You certainly shouldn't be using the Registry because the .NET Framework introduced application config files specifically so you wouldn't. VB 2008 introduced My.Settings to make it even easier. Open the project properties and select the Settings page. Now click the link at the top and read on for the solution to your problem.
    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

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Save and load variable values from/to file

    nvm.
    Last edited by n00b scripter; May 12th, 2009 at 11:58 AM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Save and load variable values from/to file

    Ok so I got it working alright.
    BUT, something is messed up. The values does change and is saved if I restart the application.
    The <applicationName>.exe.config stays the same. It doesn't change. Where the hell are the settings stored? :-o What's happened? :-o
    And yes I have built the application so I don't run it in debugger.

    I'm using this code to modify:
    Code:
            With My.Settings
                .Test = textbox1.text
            End With

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Save and load variable values from/to file

    Quote Originally Posted by jmcilhinney View Post
    vb 2008 introduced my.settings to make it even easier.
    vb 2005..?

  6. #6
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Save and load variable values from/to file

    @noob scripter

    I think it's just My.Settings.Test = TextBox1.Text. Not sure what your with statement is for.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Save and load variable values from/to file

    Well I believe that 'with' is a way to cut down on the codewriting. I guess there something more it but for example:
    If you have long variablenames and would end up with:
    VB Code:
    1. UserHandler.GetUser.First.User.FirstName="Stefan"
    2. UserHandler.GetUser.First.User.LastName="Karlsson"
    3. UserHandler.GetUser.First.User.Age="39"
    4. UserHandler.GetUser.First.User.Sex="Male"
    5. UserHandler.GetUser.First.User.Occupation="Programmer"
    6. UserHandler.GetUser.First.User.UserID="0"
    then I would use WITH to make it more readable:
    VB Code:
    1. With UserHandler.GetUser.First.User
    2.     .FirstName="Stefan"
    3.     .LastName="Karlsson"
    4.     .Age="39"
    5.     .Sex="Male"
    6.     .Occupation="Programmer"
    7.     .UserID="0"
    8. end with

    And in this application I do have a lot of lines

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

    Re: Save and load variable values from/to file

    Quote Originally Posted by n00b scripter View Post
    Ok so I got it working alright.
    BUT, something is messed up. The values does change and is saved if I restart the application.
    The <applicationName>.exe.config stays the same. It doesn't change. Where the hell are the settings stored? :-o What's happened? :-o
    And yes I have built the application so I don't run it in debugger.

    I'm using this code to modify:
    Code:
            With My.Settings
                .Test = textbox1.text
            End With
    The application config file contains the initial values for all your settings. Any settings you create with User scope will then be copied into a user config file stored under each user's Documents and Settings folder. When you edit settings at run time, that's where the changes are made. This also means that there is a different set of settings values for each Windows user who uses your application.
    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Save and load variable values from/to file

    Quote Originally Posted by jmcilhinney View Post
    The application config file contains the initial values for all your settings. Any settings you create with User scope will then be copied into a user config file stored under each user's Documents and Settings folder. When you edit settings at run time, that's where the changes are made. This also means that there is a different set of settings values for each Windows user who uses your application.
    Aah, I see. Okey I found where it's stored. I'm using the VS on my school computer and the values are store in a folder named as the companys name which is my schools. This folder is automatically created and named the same on every computer. Is there anyway to change this in the application? I don't really feel that it would be good if a publish it on the internet and the folder is called as that companys name on some unknown users computer.

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

    Re: Save and load variable values from/to file

    If you want a different company name then change the company name. Click the assembly Information button on the Application tab of the project properties.
    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

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2005
    Location
    London
    Posts
    164

    Re: Save and load variable values from/to file

    Quote Originally Posted by jmcilhinney View Post
    If you want a different company name then change the company name. Click the assembly Information button on the Application tab of the project properties.
    Ok thank you I think I got it all working now. Thx for all the great help

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