Write an ini File using vb.net

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

[doc id]
filetype=type
file1=location of file.
index.ini.
and then rename the file as
DattuAsked:
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

0
 
grayeCommented:
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.
0
 
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.
0
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 = "1.1.1.1"
        Dim docid As String = "123abc"

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

Open in new window

0
 
Éric MoreauSenior .Net ConsultantCommented:
0
 
DattuAuthor Commented:
I wish I could give 125 points to all of you. sorry guys!
0
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.