Results 1 to 34 of 34

Thread: read/write to ini file help...[SOLVED]

  1. #1

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    read/write to ini file help...[SOLVED]

    i am trying to read from an ini file (config.ini) I have searched for this and most of it is in vb6/classic... im in .net....

    this is my config.ini contents
    Code:
    [Main]
    Username=bumbum
    Password=Conaticus

    and wut i wanted to do is read from that and display the username and password in the corresponding textboxes. Problem is i dont know how to even save teh info to an ini let alone read from it.... i checked my "Microsoft Visual Studio .NET 2003 Documentation" that came with it...i checked thru my book...
    but i dont know wut to do... so as a last resort im here asking you ppl for help.... I thank you in advance for all the help you give me.
    Last edited by PlaGuE; Jun 7th, 2005 at 04:53 PM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

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

    Re: read/write to ini file help...

    INI files are not part of the .NET world. If you have the choice of file format, I strongly suggest you use serialization and XML instead. As an example, you can create a UserCredentials class that has a UserName property and a Password property. To save an object of this type to a file you simply serialize it to an XML file, which takes four lines of code. Another four lines of code deserializes the file to an object.
    VB Code:
    1. Private Sub SaveUserCredentials(ByVal user As UserCredentials)
    2.         'The object that performs the serialization.
    3.         Dim serializer As New Xml.Serialization.XmlSerializer(GetType(UserCredentials))
    4.  
    5.         'The object that creates the file.
    6.         Dim stream As New IO.FileStream(Application.StartupPath & IO.Path.DirectorySeparatorChar & "UserCredentials.xml", IO.FileMode.Create)
    7.  
    8.         'Convert the object to XML.
    9.         serializer.Serialize(stream, user)
    10.  
    11.         'Close the file.
    12.         stream.Close()
    13.     End Sub
    14.  
    15.     Private Function LoadUserCredentials() As UserCredentials
    16.         Dim userPath As String = Application.StartupPath & IO.Path.DirectorySeparatorChar & "UserCredentials.xml"
    17.  
    18.         If System.IO.File.Exists(userPath) Then
    19.             'The object that performs the serialization.
    20.             Dim serializer As New Xml.Serialization.XmlSerializer(GetType(UserCredentials))
    21.  
    22.             'The object that opens the file.
    23.             Dim stream As New IO.FileStream(userPath, IO.FileMode.Open)
    24.  
    25.             'Convert the XML to an object.
    26.             'Each function has a local variable of the same name and type that can be assigned to rather than using Return.
    27.             LoadUserCredentials = serializer.Deserialize(stream)
    28.  
    29.             'Close the file.
    30.             stream.Close()
    31.         Else
    32.             'You can create a new object with default values if no file exists.
    33.             Return New UserCredentials
    34.         End If
    35.     End Function
    If you are determined to use an INI file, you will save the file using a StreamWriter and its WriteLine method. You will then open the file with a StreamReader and its ReadLine method. It is then up to you to parse each line as it is read. See the help topics "Writing Text to a File" and "Reading Text from a File".

  3. #3

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    I think i will try both methods and then see which one works best...
    I had my heart set on INI cuz idk i see it alot and im not familiar with XML...

    Ill post back with my results
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    Quote Originally Posted by PlaGuE
    I think i will try both methods and then see which one works best...
    I had my heart set on INI cuz idk i see it alot and im not familiar with XML...

    Ill post back with my results
    Now is the time to familiarise yourself with XML as it is poised to take over the world at any moment
    Seriously though, what you are doing currently is a simple problem and will therefore be an easy introduction to XML and serialization. If you want to save 100, 1000 or 1000000 values, serialization still only requires four lines of code, whereas saving and parsing INI files would quickly get time consuming and complex. Microsoft are obviously strongly behind XML and INI files have no place in their vision of the future, or the present for that matter. XML also has many other applications so this is a good way to open yourself up to those possibilities.

  5. #5

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    i stuck the code in the form that has the textboxes that show the information....

    the only error i get is the Usercredentials part which isnt defined.

    i still am kinda new to all this...becuz of school and all i dont get much time to do this.... but with summer coming and school almost over i hope to get more into it.

    [EDIT]
    I fixed the usercredentials.... forgot to make it into a class... lol...

    but now to ask how do i have it so it shows the username in username textbox and password in password textbox..

    then i will try and do this on my own but im sure to have bad luck so ill post now and keep trying...

    how to save it...lol easy it should be... but to me its still french... but with all your help i am learning to speak french...lol
    Last edited by PlaGuE; Jun 6th, 2005 at 01:16 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    The UserCredentials class is one that you need to create yourself. Its defintion would look something like this:
    VB Code:
    1. Public Class UserCredentials
    2.  
    3.     Private m_UserName As String
    4.     Private m_Password As String
    5.  
    6.     Public Property UserName() As String
    7.         Get
    8.             Return Me.m_UserName
    9.         End Get
    10.         Set(ByVal Value As String)
    11.             Me.m_UserName = Value
    12.         End Set
    13.     End Property
    14.  
    15.     Public Property Password() As String
    16.         Get
    17.             Return Me.m_Password
    18.         End Get
    19.         Set(ByVal Value As String)
    20.             Me.m_Password = Value
    21.         End Set
    22.     End Property
    23.  
    24. End Class
    You can create classes similar to this whenever you want to persist data between sessions. I usually have a ProgramOptions class for every project I work on. Then the options for the app are saved between sessions and are tied to an app instance, unlike if the options were stored in a shared database.

  7. #7

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    wow taht was quick... even while i was editing my post
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

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

    Re: read/write to ini file help...

    Once you have a UserCredentials object it is up to you to use the UserName and Password properties to populate your TextBoxes, and then to write back any changes. If you want to get more advanced, you could make your class suitable for databinding, which I think means implementing the IList interface.

  9. #9

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    Reference to last post....(im goin to bed so i wont see it till tommorow)

    "... .... how do i have it so it shows the username in username textbox and password in password textbox..."

    "...i will try and do this on my own but im sure to have bad luck so ill post now and keep trying..."

    "how to save it...lol easy it should be... but to me its still french..."

    once my apps done im sure to include vbforums.com in my about/readme for all the help you've given me
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

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

    Re: read/write to ini file help...

    Quote Originally Posted by jmcilhinney
    INI files are not part of the .NET world. If you have the choice of file format, I strongly suggest you use serialization and XML instead. As an example, you can create a UserCredentials class that has a UserName property and a Password property. To save an object of this type to a file you simply serialize it to an XML file, which takes four lines of code. Another four lines of code deserializes the file to an object.
    VB Code:
    1. Private Sub SaveUserCredentials(ByVal user As UserCredentials)
    2.         'The object that performs the serialization.
    3.         Dim serializer As New Xml.Serialization.XmlSerializer(GetType(UserCredentials))
    4.  
    5.         'The object that creates the file.
    6.         Dim stream As New IO.FileStream(Application.StartupPath & IO.Path.DirectorySeparatorChar & "UserCredentials.xml", IO.FileMode.Create)
    7.  
    8.         'Convert the object to XML.
    9.         serializer.Serialize(stream, user)
    10.  
    11.         'Close the file.
    12.         stream.Close()
    13.     End Sub
    14.  
    15.     Private Function LoadUserCredentials() As UserCredentials
    16.         Dim userPath As String = Application.StartupPath & IO.Path.DirectorySeparatorChar & "UserCredentials.xml"
    17.  
    18.         If System.IO.File.Exists(userPath) Then
    19.             'The object that performs the serialization.
    20.             Dim serializer As New Xml.Serialization.XmlSerializer(GetType(UserCredentials))
    21.  
    22.             'The object that opens the file.
    23.             Dim stream As New IO.FileStream(userPath, IO.FileMode.Open)
    24.  
    25.             'Convert the XML to an object.
    26.             'Each function has a local variable of the same name and type that can be assigned to rather than using Return.
    27.             LoadUserCredentials = serializer.Deserialize(stream)
    28.  
    29.             'Close the file.
    30.             stream.Close()
    31.         Else
    32.             'You can create a new object with default values if no file exists.
    33.             Return New UserCredentials
    34.         End If
    35.     End Function
    If you are determined to use an INI file, you will save the file using a StreamWriter and its WriteLine method. You will then open the file with a StreamReader and its ReadLine method. It is then up to you to parse each line as it is read. See the help topics "Writing Text to a File" and "Reading Text from a File".
    VB Code:
    1. 'Load the user credentials.
    2. Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    3.  
    4. 'Populate the text boxes with the user credentials.
    5. Me.userNameText.Text = currentUser.UserName
    6. Me.passwordText.Text = currentUser.Password
    7.  
    8. 'Do whatever.
    9.  
    10. 'Get the updated user credentials.
    11. currentUser.UserName = Me.userNameText.Text
    12. currentUser.Password = Me.passwordText.Text
    13.  
    14. 'Save the updated user credentials.
    15. Me.SaveUserCredentials(currentUser)

  11. #11

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    i hada let dog outside for a min so i thought i'd come and check...

    You may call me noob right now... god knows i am...

    i know i should figure tis out on my own...and i will but just incase i cant find where... can you give me an example...


    i would think that...

    VB Code:
    1. Private Sub Apply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Apply.Click
    2.         'Save the updated user credentials.
    3.         'Load the user credentials.
    4.         Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    5.         currentUser.UserName = Me.UserName.Text
    6.         currentUser.Password = Me.password.Text
    7.  
    8.         Me.SaveUserCredentials(currentUser)
    9.     End Sub

    VB Code:
    1. Private Sub mnuBotConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Load the user credentials.
    3.         Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    4.  
    5.         'Populate the text boxes with the user credentials.
    6.         Me.UserName.Text = currentUser.UserName
    7.         Me.password.Text = currentUser.Password
    8.  
    9.         'Do whatever.
    10.     End Sub
    i get no underlined code there...
    but when i run it and i access the form it exeptions me
    Last edited by PlaGuE; Jun 6th, 2005 at 02:26 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    Firstly, in your Apply_Click procedure you don't need to load the credentials. It won't do any harm but you might as well just create a new object as you are setting the properties anyway.
    Secondly, if you are asking for help on an unhandled exception you need to give details of what the exception was and where it occurred. The first action you should take in this situation is to click the Break button on the unhandled exception dialogue and examine all your variables to make sure they are what you expect them to be. You can mouse over some variables to get their value or use the Autos, Locals and Watch windows.

  13. #13

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    Code:
     An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll
    
    Additional information: There is an error in XML document (0, 0).
    then when it brings me to the System.Xml.Serialization.XmlSerializer.Deserialize it goes straight to
    Code:
    0000020b  mov         dword ptr [ebp-8],0
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  14. #14

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    im thinking its not reading the xml...

    this is my first BIG project...
    usually its just lil things... but none with usernames and pw's and stuff...
    Last edited by PlaGuE; Jun 6th, 2005 at 02:30 PM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  15. #15

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    the error is situated in
    VB Code:
    1. Private Sub Apply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Apply.Click
    2.         'Save the updated user credentials.
    3.         'Load the user credentials.
    4.         Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    5.         currentUser.UserName = Me.UserName.Text
    6.         currentUser.Password = Me.password.Text
    7.  
    8.         Me.SaveUserCredentials(currentUser)
    9.     End Sub
    10.  
    11.  
    12. 'AND
    13.  
    14.  
    15.  
    16.     Private Sub mnuBotConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    17.         'Load the user credentials.
    18.         'Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    19.  
    20.         'Populate the text boxes with the user credentials.
    21.         'Me.UserName.Text = currentUser.UserName
    22.         'Me.password.Text = currentUser.Password
    23.  
    24.         'Do whatever.
    25.     End Sub
    i stuck the ' there to test to see if it was the problem
    Last edited by PlaGuE; Jun 6th, 2005 at 08:20 PM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    It seems as though your XML file is probably not valid XML. Unless you know exactly what the format of the XML file should be, the best way to create it in the first place is by serializing an object. Are you checking that the file exists before trying to deserialize? If you create an empty file and try to deserialize it you would most surely get an error.

  17. #17

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    Yes. it created itself.. and it is empty...

    would sumtin like
    Code:
     
    "UserCredentials.XML"
    <MyClass>
     <MyObjectProperty>
     <UserName>My String</UserName>
     </MyObjectProperty>
    </MyClass>
    be it
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    That's why, in my original code for LoadUserCredentials, I included the "If File.Exists" part. A valid XML file for your class will look something like this:
    Code:
    <?xml version="1.0"?>
    <UserCredentials xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <UserName>user</UserName>
      <Password>password</Password>
    </UserCredentials>
    I never write these XML files by hand. Whenever I use this method I let the first run of my code create the file for me by serializing an object. If you want to distribute an empty file with your app you can simply edit out the actual UserName and Password values. These values can then be entered manually for each installation or via your app.

  19. #19

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    so the IF file exists part is this:
    VB Code:
    1. 'Load UserCredentials
    2.     Private Function LoadUserCredentials() As UserCredentials
    3.         Dim userPath As String = Application.StartupPath & IO.Path.DirectorySeparatorChar & "UserCredentials.xml"
    4.  
    5.         If System.IO.File.Exists(userPath) Then
    6.             'The object that performs the serialization.
    7.             Dim serializer As New Xml.Serialization.XmlSerializer(GetType(UserCredentials))

    from
    VB Code:
    1. 'Load UserCredentials
    2.     Private Function LoadUserCredentials() As UserCredentials
    3.         Dim userPath As String = Application.StartupPath & IO.Path.DirectorySeparatorChar & "UserCredentials.xml"
    4.  
    5.         If System.IO.File.Exists(userPath) Then
    6.             'The object that performs the serialization.
    7.             Dim serializer As New Xml.Serialization.XmlSerializer(GetType(UserCredentials))
    8.  
    9.             'The object that opens the file.
    10.             Dim stream As New IO.FileStream(userPath, IO.FileMode.Open)
    11.  
    12.             'Convert the XML to an object.
    13.             'Each function has a local variable of the same name and type that can be assigned to rather than using Return.
    14.             LoadUserCredentials = serializer.Deserialize(stream)
    15.  
    16.             'Close the file.
    17.             stream.Close()
    18.         Else
    19.             'You can create a new object with default values if no file exists.
    20.             Return New UserCredentials
    21.         End If
    22.     End Function
    am i right?


    and the code added to the XML file manually is
    Code:
    <?xml version="1.0"?>
    <UserCredentials xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <UserName>hey</UserName>
      <Password>dude</Password>
    </UserCredentials>
    im sorry if im annoying you ppl... im only asking questions cuz im not sure..
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  20. #20

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    this is the form its situated in...


    VB Code:
    1. ' recently added to see if it would help
    2. Imports System Imports System.Xml
    3. Imports System.Xml.Serialization
    4. Imports System.IO
    5. Imports Microsoft.VisualBasic
    6. 'it didnt
    7.  
    8. Public Class mnuBotConfig
    9.     Inherits System.Windows.Forms.Form
    10.  
    11.  
    12.   Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    13.  
    14.     End Sub
    15.  
    16.     Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
    17.  
    18.     End Sub
    19.  
    20.     Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Starcraft_Product.CheckedChanged
    21.  
    22.     End Sub
    23.  
    24.     Private Sub RadioButton7_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WarCraftIIITFT_Product.CheckedChanged
    25.  
    26.     End Sub
    27.  
    28.     Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter
    29.  
    30.     End Sub
    31.  
    32.     Private Sub Apply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Apply.Click
    33.         'Save the updated user credentials.
    34.         'Load the user credentials.
    35.         Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    36.         currentUser.UserName = Me.UserName.Text
    37.         currentUser.Password = Me.password.Text
    38.  
    39.         Me.SaveUserCredentials(currentUser)
    40.     End Sub
    41.  
    42.     Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
    43.         Me.Close()
    44.     End Sub
    45.  
    46.     Private Sub mnuBotConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    47.         'Load the user credentials.
    48.         Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    49.  
    50.         'Populate the text boxes with the user credentials.
    51.         Me.UserName.Text = currentUser.UserName
    52.         Me.password.Text = currentUser.Password
    53.  
    54.         'Do whatever.
    55.     End Sub
    56.  
    57.     Private Sub Options_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Options.Click
    58.         Dim Options As New options
    59.  
    60.         Options.Show()
    61.         Me.Close()
    62.     End Sub
    63.  
    64.     Private Sub password_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles password.TextChanged
    65.  
    66.     End Sub
    67.     'Save UserCedentials
    68.     Private Sub SaveUserCredentials(ByVal userame As UserCredentials)
    69.         'The object that performs the serialization.
    70.         Dim serializer As New Xml.Serialization.XmlSerializer(GetType(UserCredentials))
    71.  
    72.         'The object that creates the file.
    73.         Dim stream As New IO.FileStream(Application.StartupPath & IO.Path.DirectorySeparatorChar & "UserCredentials.xml", IO.FileMode.Create)
    74.  
    75.         'Convert the object to XML.
    76.         serializer.Serialize(stream, UserName)
    77.  
    78.         'Close the file.
    79.         stream.Close()
    80.     End Sub
    81.     'Load UserCredentials
    82.     Private Function LoadUserCredentials() As UserCredentials
    83.         Dim userPath As String = Application.StartupPath & IO.Path.DirectorySeparatorChar & "UserCredentials.xml"
    84.  
    85.         If System.IO.File.Exists(userPath) Then
    86.             'The object that performs the serialization.
    87.             Dim serializer As New Xml.Serialization.XmlSerializer(GetType(UserCredentials))
    88.  
    89.             'The object that opens the file.
    90.             Dim stream As New IO.FileStream(userPath, IO.FileMode.Open)
    91.  
    92.             'Convert the XML to an object.
    93.             'Each function has a local variable of the same name and type that can be assigned to rather than using Return.
    94.             LoadUserCredentials = serializer.Deserialize(stream)
    95.  
    96.             'Close the file.
    97.             stream.Close()
    98.         Else
    99.             'You can create a new object with default values if no file exists.
    100.             Return New UserCredentials
    101.         End If
    102.     End Function
    103.     Private Sub UserName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UserName.TextChanged
    104.  
    105.     End Sub
    106. End Class
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  21. #21
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    In the LoadUserCredentials method, the code checks for the existence of the desired file. If the file exists it is deserialized into an object. If the file does not contain valid XML for the specified type an exception will be thrown. If the file does not exist, a new object is created. The reason I code it like this is so that when you run your app for the first time and no XML file exists, an empty object is created. You can then populate the properties of this empty object and serialize it to create the XML file. Then when your app is run the next time, the file will exist and it will be deserialized. If you want the file to be there the first time you run your app then you must write the XML file by hand. You must make sure that it is valid XML, though, as even a single character in the wrong place means the whole lot is no good. If you paste the code from my previous post into a file and edit the value of the UserName and Password you should find that it will work. I don't know if it's been mentioned before but my code also assumes that the XML file is in your app's program folder. In short, the answer to the question in your second last post is "Yes".

  22. #22

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    koo...
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  23. #23

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    Okay... IM SO DUMB....lol...really i am...

    ive been at this problem for hours....

    wut ive come to relize is... idk where to stick the code...

    idk where to stick it so that when i click apply it saves the code... or when i load the form it loads the data strings into the correct boxes...
    this was the right thing...to do..?:
    VB Code:
    1. Private Sub Apply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Apply.Click
    2.         'Save the updated user credentials.
    3.         'Load the user credentials.
    4.         Dim currentUser As UserCredentials = Me.SaveUserCredentials(currentUser)
    5.         currentUser.UserName = Me.UserName.Text
    6.         currentUser.Password = Me.password.Text
    7.  
    8.        
    9.     End Sub
    10.  
    11.  
    12. 'AND
    13.  
    14.  
    15.  
    16.     Private Sub mnuBotConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    17.         'Load the user credentials.
    18.          Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    19.  
    20.         'Populate the text boxes with the user credentials.
    21.         Me.UserName.Text = currentUser.UserName
    22.         Me.password.Text = currentUser.Password
    23.  
    24.         'load info into correct boxes
    25.     End Sub

    and with
    Code:
     
    <?xml version="1.0"?>
    <UserCredentials xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <UserName>boo</UserName>
      <Password>dude</Password>
    </UserCredentials>
    is that right... or should it be like
    Code:
     
    <?xml version="1.0"?>
    <UserCredentials xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <UserName>m_UserName</UserName>
      <Password>m_Password</Password>
    </UserCredentials>
    if you need anymore information in which to help me with this... just ask... im at a complete loss...
    Last edited by PlaGuE; Jun 7th, 2005 at 01:41 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  24. #24
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    The last lot of code you posted looks like it should work OK. It seems to me that the issue was that your XML was invalid. Simply paste the XML code I provided into a blank text file, edit the UserName and Password values if you want and then save it as UserCredentials.xml in your program folder.

  25. #25

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    okay well... i got past the part where it gives error at opening of the form...
    VB Code:
    1. Private Sub mnuBotConfig_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         'Load the user credentials.
    3.         Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    4.  
    5.         'Populate the text boxes with the user credentials.
    6.         Me.UserName.Text = currentUser.UserName
    7.         Me.password.Text = currentUser.Password
    8.  
    9.         'load info into correct boxes

    but it dont load the credentials...maybe i dont have any yet...but I can fix this later...

    when i hit break it goes here
    VB Code:
    1. Public Class UserCredentials
    2.  
    3.     Private m_UserName As String
    4.     Private m_CDkey As String
    5.     Private m_Password As String
    6.  
    7.     Public Property UserName() As String
    8.         Get
    9.             Return Me.m_UserName
    10.         End Get
    11.         Set(ByVal Value As String) 'highlighted ByVal Value As String
    12.             Me.UserName = Value
    13.         End Set
    14.     End Property
    15.  
    16.     Public Property Password() As String
    17.         Get
    18.             Return Me.m_Password
    19.         End Get
    20.         Set(ByVal Value As String)
    21.             Me.m_Password = Value
    22.         End Set
    23.     End Property
    24. End Class
    its caused by this
    VB Code:
    1. Private Sub Apply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Apply.Click
    2.         'Save the updated user credentials.
    3.         'Load the user credentials.
    4.         Dim currentUser As UserCredentials = Me.LoadUserCredentials()
    5.         currentUser.UserName = Me.UserName.Text
    6.         currentUser.Password = Me.password.Text
    7.         Me.SaveUserCredentials(currentUser)
    8.     End Sub
    good thing is, is at least it loads the form...
    Last edited by PlaGuE; Jun 7th, 2005 at 03:23 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  26. #26
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    If the code you have posted is correct, you are referring to the UserName property when you are setting the UserName property. This would create an infinite recursion and lead to an OutOfMemoryException if I'm not mistaken. You should be referring to the m_UserName variable, i.e.
    VB Code:
    1. Me.UserName = Value
    should be
    VB Code:
    1. Me.m_UserName = Value
    If this is just a typo on your part, please let me know the details of the exception.

  27. #27

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    hmm...
    Last edited by PlaGuE; Jun 7th, 2005 at 03:31 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  28. #28

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    THANKS... now it works...


    If i wasnt straight id say " I LUV YOU"

    if i had a million dollars is give it to you...

    you... you... I LUV YOU MAN... this was giving me a headache ... and you gave me the asprin..lol...


    Sorry... im jsut very happy
    Last edited by PlaGuE; Jun 7th, 2005 at 03:50 AM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  29. #29
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    Your extremely heterosexual thanks is quite enough.
    Now, about the fact that your password is saved as plain text...

  30. #30

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    well accually for wut its being used for ... its okay that its not encrypted...

    how would i encrypt it tho...if needbe?
    Last edited by PlaGuE; Jun 7th, 2005 at 04:51 PM.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  31. #31
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...

    I haven't used encryption a lot so there is a great deal I don't know about it. Whenever I have employed encryption, though, I have used the Data Protection API. Go to the MSDN library on-line and search for DPAPI for more info.

  32. #32

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: read/write to ini file help...

    koo..will do
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  33. #33
    Fanatic Member sridharavijay's Avatar
    Join Date
    Sep 2002
    Location
    http://www.vijaysridhara.in
    Posts
    589

    Re: read/write to ini file help...[SOLVED]

    How can I read such a xml file till the end?
    not able to figureout!

  34. #34
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: read/write to ini file help...[SOLVED]

    Quote Originally Posted by sridharavijay
    How can I read such a xml file till the end?
    not able to figureout!
    When you call Deserialize it reads the entire file and constructs an instance of the class from the contents. One interesting point to note is that no constructor is called.

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