Write an ini File using

Posted on 2009-12-29
Last Modified: 2012-08-25
I need to write a ini file using
it should be like this.
ipaddress=some ipaddress
document number=docid

[doc id]
file1=location of file.
and then rename the file as
Question by:Dattu
    LVL 41

    Expert Comment

    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.

    Author Comment

    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.
    LVL 74

    Accepted Solution

    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

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    (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

    LVL 69

    Assisted Solution

    by:Éric Moreau

    Author Closing Comment

    I wish I could give 125 points to all of you. sorry guys!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now