?
Solved

How to Store Textbox text in .ini file

Posted on 2004-11-17
2
Medium Priority
?
312 Views
Last Modified: 2010-04-24
I'd like to store a username and password in a .txt or .ini file so the user can store their passwords in it to use instead of having to retype them all the time.

How would I save txtUserName.Text  and txtPassword.Text to an .ini or .txt file in (C:\Program Files\MyProgram\settings.ini or .txt) and retrieve those values later on?

Code would be greatly appreciated.
0
Comment
Question by:Matrix1000
2 Comments
 
LVL 27

Accepted Solution

by:
planocz earned 2000 total points
ID: 12608742
Here is a sample code of reading and write to ini....

'FORM 1

Public Class Form1
    Inherits System.Windows.Forms.Form
    Friend WithEvents btnWrite As System.Windows.Forms.Button
    Friend WithEvents btnRead As System.Windows.Forms.Button
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ComponentModel.Container
        Me.btnWrite = New System.Windows.Forms.Button
        Me.btnRead = New System.Windows.Forms.Button
        Me.btnReadKeyInSection = New System.Windows.Forms.Button
        Me.btnReadSections = New System.Windows.Forms.Button
        Me.btnDeleteSection = New System.Windows.Forms.Button
        Me.btnDeleteKeyInSection = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'btnWrite
        '
        Me.btnWrite.Location = New System.Drawing.Point(16, 16)
        Me.btnWrite.Name = "btnWrite"
        Me.btnWrite.Size = New System.Drawing.Size(116, 23)
        Me.btnWrite.TabIndex = 0
        Me.btnWrite.Text = "Write"
        '
        'btnRead
        '
        Me.btnRead.Location = New System.Drawing.Point(16, 56)
        Me.btnRead.Name = "btnRead"
        Me.btnRead.Size = New System.Drawing.Size(116, 23)
        Me.btnRead.TabIndex = 1
        Me.btnRead.Text = "Read"
        '
        'btnReadKeyInSection
        '
        Me.btnReadKeyInSection.Location = New System.Drawing.Point(16, 100)
        Me.btnReadKeyInSection.Name = "btnReadKeyInSection"
        Me.btnReadKeyInSection.Size = New System.Drawing.Size(116, 23)
        Me.btnReadKeyInSection.TabIndex = 2
        Me.btnReadKeyInSection.Text = "ReadKeyInSection"
        '
        'btnReadSections
        '
        Me.btnReadSections.Location = New System.Drawing.Point(16, 148)
        Me.btnReadSections.Name = "btnReadSections"
        Me.btnReadSections.Size = New System.Drawing.Size(116, 23)
        Me.btnReadSections.TabIndex = 3
        Me.btnReadSections.Text = "ReadSections"
        '
        'btnDeleteSection
        '
        Me.btnDeleteSection.Location = New System.Drawing.Point(16, 232)
        Me.btnDeleteSection.Name = "btnDeleteSection"
        Me.btnDeleteSection.Size = New System.Drawing.Size(116, 23)
        Me.btnDeleteSection.TabIndex = 4
        Me.btnDeleteSection.Text = "DeleteSection"
        '
        'btnDeleteKeyInSection
        '
        Me.btnDeleteKeyInSection.Location = New System.Drawing.Point(20, 192)
        Me.btnDeleteKeyInSection.Name = "btnDeleteKeyInSection"
        Me.btnDeleteKeyInSection.Size = New System.Drawing.Size(116, 23)
        Me.btnDeleteKeyInSection.TabIndex = 5
        Me.btnDeleteKeyInSection.Text = "DeleteKeyInSection"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.btnDeleteKeyInSection)
        Me.Controls.Add(Me.btnDeleteSection)
        Me.Controls.Add(Me.btnReadSections)
        Me.Controls.Add(Me.btnReadKeyInSection)
        Me.Controls.Add(Me.btnRead)
        Me.Controls.Add(Me.btnWrite)
        Me.Name = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region

    Dim fsINIPath As String = Application.StartupPath & "\Test.ini"

    Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
        Dim sValue As String
        'GROUP is a section name
        INIWrite(fsINIPath, "GROUP", "NewKey1", "Value1") ' build INI file
        INIWrite(fsINIPath, "Section1", "Key1-1", "Value1-1") ' build INI file
        INIWrite(fsINIPath, "Section2", "Key2-1", "Value2-1")
        MsgBox("done")
    End Sub

    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        'reads all keys in section
        Dim sValue As String
        Dim arr() As String
        Dim i As Integer

        sValue = INIRead(fsINIPath, "GROUP") ' get all keys in section
        sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
        MessageBox.Show(sValue, "All the Keys in section GROUP", MessageBoxButtons.OK)
        arr = Split(sValue, "|")
        Dim msg As String
        For i = 0 To UBound(arr)
            msg &= arr(i) & vbCrLf
        Next
        MessageBox.Show(msg)
    End Sub

    Private Sub btnReadKeyInSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadKeyInSection.Click
        Dim sValue As String
        'if key is unknown return the admin address (deal@...)
        sValue = INIRead(fsINIPath, "GROUP", "Iouri", "bla") ' specify all
        MessageBox.Show(sValue, "Key does not exist return default", MessageBoxButtons.OK)
    End Sub
    Private Sub btnReadSections_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReadSections.Click
        Dim sValue As String

        sValue = INIRead(fsINIPath) ' get all section names
        sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
        MessageBox.Show(sValue, "All sections pre delete", MessageBoxButtons.OK)
    End Sub

    Private Sub btnDeleteSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteSection.Click
        Dim sValue As String

        INIDelete(fsINIPath, "section1") ' delete section
        sValue = INIRead(fsINIPath) ' get all section names
        sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
        MessageBox.Show(sValue, "section1 deleted", MessageBoxButtons.OK)
    End Sub

    Private Sub btnDeleteKeyInSection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteKeyInSection.Click
        Dim sValue As String

        INIDelete(fsINIPath, "section1", "key1-2") ' delete middle entry in section 1
        sValue = INIRead(fsINIPath, "section1") ' get all keys in section again
        sValue = sValue.Replace(ControlChars.NullChar, "|"c) ' change embedded NULLs to pipe chars
        MessageBox.Show(sValue, "key1-2 in section1 deleted", MessageBoxButtons.OK)
    End Sub

    Friend WithEvents btnReadKeyInSection As System.Windows.Forms.Button
    Friend WithEvents btnReadSections As System.Windows.Forms.Button
    Friend WithEvents btnDeleteSection As System.Windows.Forms.Button
    Friend WithEvents btnDeleteKeyInSection As System.Windows.Forms.Button

 
End Class


'MODULE

Module ModuleINI
#Region "API Calls"
    ' standard API declarations for INI access
    ' changing only "As Long" to "As Int32" (As Integer would work also)
    Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
      Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpString As String, _
      ByVal lpFileName As String) As Int32

    Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
      Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
      ByVal lpKeyName As String, ByVal lpDefault As String, _
      ByVal lpReturnedString As String, ByVal nSize As Int32, _
      ByVal lpFileName As String) As Int32
#End Region

    Public Overloads Function INIRead(ByVal INIPath As String, _
      ByVal SectionName As String, ByVal KeyName As String, _
      ByVal DefaultValue As String) As String
        ' primary version of call gets single value given all parameters
        Dim n As Int32
        Dim sData As String
        sData = space$(1024) ' allocate some room
        n = GetPrivateProfileString(SectionName, KeyName, DefaultValue, _
          sData, sData.Length, INIPath)
        If n > 0 Then ' return whatever it gave us
            INIRead = sdata.Substring(0, n)
        Else
            iniread = ""
        End If
    End Function

#Region "INIRead Overloads"
    Public Overloads Function INIRead(ByVal INIPath As String, _
      ByVal SectionName As String, ByVal KeyName As String) As String
        ' overload 1 assumes zero-length default
        Return INIRead(inipath, sectionname, KeyName, "")
    End Function

    Public Overloads Function INIRead(ByVal INIPath As String, _
      ByVal SectionName As String) As String
        ' overload 2 returns all keys in a given section of the given file
        Return INIRead(inipath, sectionname, Nothing, "")
    End Function

    Public Overloads Function INIRead(ByVal INIPath As String) As String
        ' overload 3 returns all section names given just path
        Return INIRead(inipath, Nothing, Nothing, "")
    End Function
#End Region

    Public Sub INIWrite(ByVal INIPath As String, ByVal SectionName As String, _
      ByVal KeyName As String, ByVal TheValue As String)
        Call WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)
    End Sub

    Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String, _
      ByVal KeyName As String) ' delete single line from section
        Call WritePrivateProfileString(SectionName, KeyName, Nothing, INIPath)
    End Sub

    Public Overloads Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String)
        ' delete section from INI file
        Call WritePrivateProfileString(SectionName, Nothing, Nothing, INIPath)
    End Sub

End Module


0
 
LVL 18

Expert Comment

by:armoghan
ID: 12612512
Hi planocz,
The process is called MRU, Most recently used.
This functionality is usually required for not only textbox but for many controls like u see in IE combobox
See the following articles,
http://www.reflectionit.nl/UserSettings.aspx
http://www.thecodeproject.com/csharp/mru_files.asp
http://www.codeproject.com/cs/combobox/urlcombo.asp
Cheers!
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 19 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question