Results 1 to 22 of 22

Thread: Save, Get Settings from ini file [RESOLVED by Edneeis]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Save, Get Settings from ini file [RESOLVED by Edneeis]

    I feel a bit embarrased asking too many questions.

    This attached VB6 module;
    I used it to Save and Get setting from a INI file.

    However, in .NET I found it impossible to use. Upgrading didn't work, and I am not good in API to do a rewrite of the module to work with .NET.

    Can you please check the VB6 module and share with me a location where to obtain a similar module which will work with .NET?

    Thank you very much,
    McoreD.
    Attached Files Attached Files
    Last edited by ~*McoreD*~; Jan 21st, 2004 at 10:43 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Don't use INI . Instead , use XML file Configuration file . It's very easy with less code and overall has more advantages than INI files. Here's an example : http://www.thescarms.com/dotNet/AppSettings.asp

    and Don't feel that way , we're here just help each other .

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Thanks a TON!

    Excellent! Pirate.

    Thanks so much for introducing me to XML.
    I will try this for sure.

  4. #4
    Addicted Member
    Join Date
    Nov 2003
    Posts
    137
    I took a look at that link and it didnt help 1 bit. I think a ini file is way easier even tho i dont know how to create one. Ini Files are easier to read and just easier to deal with. So can someone point me to a INI file tutorial. Or a better XML tutorial Thanks
    Live to love, Not to hate

  5. #5
    New Member
    Join Date
    Jan 2004
    Location
    Italia
    Posts
    10
    I agree with Pirate.

    I got all I needed to come to grips with it in the VS .Net help files. I personally use an .xml file as opposed to the app.config. Just a matter of preference.
    DaDragon

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by AtomSoft
    I took a look at that link and it didnt help 1 bit. I think a ini file is way easier even tho i dont know how to create one. Ini Files are easier to read and just easier to deal with. So can someone point me to a INI file tutorial. Or a better XML tutorial Thanks
    Well , how about http://www.google.com ?

  7. #7
    Member
    Join Date
    Nov 2003
    Location
    Amsterdam, The Netherlands
    Posts
    53
    try this

  8. #8
    Addicted Member
    Join Date
    Nov 2003
    Posts
    137
    for as google. That is my home page. I am a google freak. Google rocks but its tirering searching and searching and etc.. Ok if i ask for hel pwhy do you point me to another place to ask for help. Why not give me a straight link ? do you use google ? if so why not search for me and send me the right link or give me the code yourself?

    I am not trying to get you mad but i get tired of replys that dont help. If you are going to reply it should be usefull. Thanks for your time.
    Live to love, Not to hate

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You don't really need to know much about xml to use an app.config file. You can use one of a couple different methods included in the .NET framework to read them (in the Configuration namespace) and typically you can write them in any text editor or the IDE. Here is a tutorial on the app.config file: http://www.edneeis.com/example.aspx?ID=13 As for writing to config files at runtime there are a number of good classes floating about the net to handle this.

    Or the ultimate source for xml is: http://www.w3.org/TR/xpath
    Don't disregard it because its from a standards site it has lots of useful information and good xpath examples. XPath is sort of query language for xml.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Thumbs up

    Much appreciated your sample Code Edneeis.
    I tested it and it works perfectly for both saving and retrieving values.

    I was able to retrive the value from a Config file using Pirate's link to TheScarms Visual Basic.NET Code Library.

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim aConfig As Configuration.ConfigurationSettings
    3.         Dim CDSize As String = aConfig.AppSettings("CDSize")
    4.         Dim NumberOfCDs As String = aConfig.AppSettings("NumberOfCDs")
    5.         TextBox1.Text = CDSize
    6.         TextBox2.Text = NumberOfCDs
    7.     End Sub

    However I wasn't smart enough to save new values back to the Config file.

    I tried:

    VB Code:
    1. Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    2.         Dim aConfig As Configuration.ConfigurationSettings
    3.         Dim CDSize As String = aConfig.AppSettings("CDSize")
    4.         Dim NumberOfCDs As String = aConfig.AppSettings("NumberOfCDs")
    5.         CDSize = TextBox1.Text
    6.         NumberOfCDs = TextBox2.Text
    7.     End Sub

    Although Edneeis works nicely for me, I would be still interested to know how to save new values to the config according to Pirate's Link.

    Thank you guys,
    You all ROCK.

    McoreD

  11. #11
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Pirates link shows how to read the values using the objects supplied with the .NET framework and unforuntately the framework doesn't provide a write method just reading ones.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Thumbs up

    Thank you very much for your complete Code Edneeis.

    I am using it now and it works Great!

    Yours,
    McoreD

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - Me.Width

    Hi Edneeis,

    Everything else works fine

    I am doing something wrong here:

    The following is in my app.Config
    PHP Code:
    <add key="WindowLeft" value="((System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2)" />
    <
    add key="WindowTop" value="((System.Windows.Forms.Screen.PrimaryScreen.Bounds.width - Me.height) / 2)" /> 
    When the Form loads, I expect the Form to do this:

    VB Code:
    1. Me.Left = AppSettingsManager.LoadAndGetValue("WindowLeft")
    2.         Me.Top = AppSettingsManager.LoadAndGetValue("WindowTop")

    But I am not getting the expected result and I get the following error instead:

    Additional information: Cast from string "((System.Windows.Forms.Screen.Pr" to type 'Integer' is not valid.
    Hope you released what I was going to do.
    Is it possible to pass this kind of information from the XML file to the Form at Form_Load?

    Thank you,
    McoreD

    P.S: It works perfectly if I replace the XML code with (using numbers):
    For example:

    PHP Code:
    <add key="WindowLeft" value="500" />
    <
    add key="WindowTop" value="500" /> 

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It's as you said you must put a number in the xml. You can't put a variable or method name in xml and expect it to process it as code. Once its in the xml its just a string. Although if it is also going to be that formula "((System.Windows.Forms.Screen.PrimaryScreen.Bounds.width - Me.height) / 2)" then why does it even need to be dynamic or stored in the xml?

  15. #15
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by AtomSoft
    for as google. That is my home page. I am a google freak. Google rocks but its tirering searching and searching and etc.. Ok if i ask for hel pwhy do you point me to another place to ask for help. Why not give me a straight link ?
    I gave a direct link and you said it didn't help , though it's there to answer this question specifically . I think you're afraid to learn something new like Config files . But you can't say it didn't help .

    do you use google ? if so why not search for me and send me the right link or give me the code yourself?
    Yes, I use Google all the time and huh , you want direct link !!fine if I find one , but if I couldn't then it's your problem to find one and the help we can offer is just some tips like 'google it' .


    I am not trying to get you mad but i get tired of replys that dont help. If you are going to reply it should be usefull. Thanks for your time.
    We're talking about XML Files and you just jumped in saying this . Ofcourse this will not make any sense if you're talking about ini files and we're not .

    ~*McoreD*~ , I know your problem has been solved but to to extend your knowledge , why don't you have a look at dynamic properties ? It's another way to set controls's configuration without having to read/write settings .

  16. #16
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Originally posted by Pirate
    ~*McoreD*~ , I know your problem has been solved but to to extend your knowledge , why don't you have a look at dynamic properties ? It's another way to set controls's configuration without having to read/write settings .
    Thats a good call! I second that. If the properties you are storing in a config file are parts of any control or things you can set at designtime then check at the top of the Property dialog at the Dynamic Properties section which just handles reading/writing from a config file for you.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Apr 2003
    Location
    Australia
    Posts
    252

    Thumbs up

    Originally posted by Edneeis
    Once its in the xml its just a string. Although if it is also going to be that formula "((System.Windows.Forms.Screen.PrimaryScreen.Bounds.width - Me.height) / 2)" then why does it even need to be dynamic or stored in the xml?
    Okay I will explain my need a bit more.

    In VB6, I used module (attached in my first post) to do the following:

    VB Code:
    1. Me.Left = sGetINI(App.Path & "\" & App.EXEName & "-Defaults.ini", "Window", "Form_Left", (Screen.Width - Me.Width) / 2)
    2. Me.Top = sGetINI(App.Path & "\" & App.EXEName & "-Defaults.ini", "Window", "Form_Top", (Screen.Width - Me.Height) / 2)

    As you can see, if there was no current setting for WindowLeft and WindowTop, then the form would start in the center of the screen. I didn't need to specify any value since the code was within the form.
    I would like to do that in XML too. (without spefifying values). But now I seem it is impossible, since once it is in XML it is just a string. (I was just hoping that XML would pass that string to the Form while the Form loads. )

    Thanks Pirate and Edneeis for the information about Dynamic Properties. I will have a look at them too.

    ------------------------------------------------------------
    I was using this workaround.

    VB Code:
    1. If Me.Left = -100 And Me.Left = -100 Then
    2. Me.Left = (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - Me.Width) / 2
    3.  Me.Top = (System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - Me.Height) / 2
    4. End If

    Cheers,
    McoreD

  18. #18
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188
    I am having issues here.
    Here is my XLM File
    Code:
    <configuration>
      <appSettings>
        <add key="mydbconn" value="host=10.0.0.x, etc;"/>
      </appSettings>
    </configura
    My program code is
    Code:
    dim aconfig as configuration.configurationsettings
    dim mydbconn as string
    mydbconn=aconfig.appsettings("mydbconn")
    oconnection.connectionstring=mydbconn
    Now it tells me that the value is null. So, I go to look for the config file and it is missing. So, I recreate it. Execute...and again it is gone from my BIN folder.

    Any ideas?

  19. #19
    Lively Member TLord's Avatar
    Join Date
    Jun 2004
    Posts
    95
    That is because Bin is an Output folder, Visual Studio .NET copies your outputted files to there, so let's say your Application Configuration File's name is "app.config", it'll overrite it if exists previously the Bin dorectory.

    To solve this problem, simpy edit the file from within Visual Studio .Net's IDE, that way it'll copy the correct data (I faced sucha situation but solved it right ahead).
    Do you think my life is easy?
    Do you think it's good to win?
    do you think it's nice to kill?
    Do you think learning is a must?
    Do you think computers are nothing?
    Do you think this post is stupid?
    Do ypu think we're really humen?

    DO YOU THINK IT'S GOOD TO THINK AT ALL? ? ? ! ! !

  20. #20
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188
    I ended up going into the Project Properties, Common Properties, Build and had to click on Supported Runtimes / CHANGE button and it created the config file for me...
    I am sure there was another way - but even creating the app.config file from the project did not seem to work...

    Thanks for the info... I found a lot of great things in the Properties in .NET.

    Any info on project versions? (not Framework versions)

  21. #21
    Lively Member TLord's Avatar
    Join Date
    Jun 2004
    Posts
    95
    Originally posted by craigreilly
    but even creating the app.config file from the project did not seem to work...
    Did you create it and fill it up with your settigns or just added it to the project?


    About the Project version, as I understtod you want that number appears when you clikc properties->Version Tab->Product Version? ! ?
    This simply can be changed by going the Assembly Information File ("AssemblyInfo.vb"), there in the bottom of the file you will find an attribute like this:
    Code:
    <Assembly: AssemblyVersion("1.0.*")>
    Change the first two numbers as your taste.
    Do you think my life is easy?
    Do you think it's good to win?
    do you think it's nice to kill?
    Do you think learning is a must?
    Do you think computers are nothing?
    Do you think this post is stupid?
    Do ypu think we're really humen?

    DO YOU THINK IT'S GOOD TO THINK AT ALL? ? ? ! ! !

  22. #22
    Addicted Member craigreilly's Avatar
    Join Date
    Jul 2004
    Location
    Scottsdale, AZ
    Posts
    188
    I did get the config working... and am now able to load the info I need.

    I work on my project from home sometimes and needed to be able to run the code and access the remote mySQL Database. This is much easier - just changing the config file to reflect the correct IP Address)


    As far as the version goes - I played with that. When the config file version requirement is higher than the code version it tells me the .NET Framework is out of date. What I am looking for is to tell me the Program is out of date and needs to be updated. Sometimes I can not update a workstation because someone is on vacation and they are very particular about people going in their office.

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