Write an ini File using vb.net

I need to write a ini file using vb.net.
it should be like this.
ipaddress=some ipaddress
document number=docid

[doc id]
file1=location of file.
and then rename the file as
Who is Participating?
käµfm³d 👽Commented:
The easiest way in .NET would be to create a StreamWriter object and just write the file in .ini format. If you are hard-pressed to actually use Windows Ini api, then you could use Interop to call the appropriate API functions:
Imports System.Runtime.InteropServices
Imports System.Text

Public Class Ini
    <DllImport("kernel32.dll", SetLastError:=True)> _
Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, _
                        ByVal lpKeyName As String, _
                        ByVal lpString As String, _
                        ByVal lpFileName As String) As Boolean
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
                            ByVal lpKeyName As String, _
                            ByVal lpDefault As String, _
                            ByVal lpReturnedString As StringBuilder, _
                            ByVal nSize As Integer, _
                            ByVal lpFileName As String) As Integer
    End Function
End Class

Open in new window

Does it really need to be an INI file?   A typical VB.Net applicaiton would use a *.config file to store this type of information.  The config file is completely automated via the My.Settings class... and it very easy to setup and use.
DattuAuthor Commented:
Its not a config or any sort of config file. it needs to be read by another file client server and that server can read only ini files.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Mike TomlinsonMiddle School Assistant TeacherCommented:
(I like the other options better)

Using kaufmed's StreamWriter approach:
        Dim user As Integer = 1
        Dim ipaddress As String = ""
        Dim docid As String = "123abc"

        Using sw As New System.IO.StreamWriter("c:\some\path\file.ini")
            sw.WriteLine("user=" & user)
            sw.WriteLine("ipaddress=" & ipaddress)
            sw.WriteLine("document number=" & docid)
            ' etc...
        End Using

Open in new window

Éric MoreauSenior .Net ConsultantCommented:
DattuAuthor Commented:
I wish I could give 125 points to all of you. sorry guys!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.