Hypershift
Hypershift

Reputation: 21

VB.NET How to write text to a .ini file?

I'm creating a program that will help users create a certain config file for another program that usually has to be done by hand. The programs config file reads it like; 'This_Setting = 0/1 (Off/On)'

So I want to make it so that if the user ticks say a checkbox on my program, it will write in the text file '1' and if it is unticked, it will write '0'.

Another way I thought about doing it was having a text box, the user ticks the boxes they want, and then click a button and then it would paste the config code in the text box so they could copy/paste it. I personally think this would be a better option, but I still have not the slightest clue how to do it.

Any help is appreciated!

Upvotes: 2

Views: 8579

Answers (5)

Sameh Al-Srory
Sameh Al-Srory

Reputation: 1

A Class for reading and writing .INI files easily. I didn't make these. Instructions

  1. Put this code into your project. VB Code:
    Class INIReadWrite
 
    <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
 
    <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

 
    Public Shared Function ReadINI(ByVal File As String, ByVal Section As >String, ByVal Key As String) As String
        Dim sb As New StringBuilder(500)
        GetPrivateProfileString(Section, Key, "", sb, sb.Capacity, File)
        Return sb.ToString
   End Function

    Public Shared Sub WriteINI(ByVal File As String, ByVal Section As >String, ByVal Key As String, ByVal Value As String)
        WritePrivateProfileString(Section, Key, Value, File)
    End Sub

End Class
  1. To read an .INI file Code:
ReadINI(File, Section, Key)
  1. To write to an .INI file Code:
WriteINI(File, Section, Key, Value)

Upvotes: 0

Codename K
Codename K

Reputation: 744

Here is the VB.NET code to write to INI file,

Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("kernel32")>
    Private Shared Function WritePrivateProfileString(ByVal lpSectionName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    End Function

    Private Function SetIniValue(section As String, key As String, filename As String, Optional defaultValue As String = "") As String
        Dim sb As New StringBuilder(500)
        If WritePrivateProfileString(section, key, defaultValue, filename) > 0 Then
            Return sb.ToString
        Else
            Return defaultValue
        End If
    End Function

    Private Sub WriteToINI()
        SetIniValue("default", "This_Setting", "C:\myconfigfile.ini", "1")
    End Sub
End Class

Reference: http://vbnet.mvps.org/index.html?code/file/pprofilebasic.htm

Upvotes: 2

ABPerson
ABPerson

Reputation: 521

This should be very easy. What you would want to do is use the following code.

FileOpen(1, "WHATEVER-THE-FILE-PATH-IS.ini", OpenMode.Output)
PrintLine(1, "WHATEVER-TEXT-YOU-WANT-TO-WRITE")
FileClose(1)

All you have to do is just change some things to make it suit your needs. First of all, on FileOpen() you want to change where it says "WHATEVER-THE-FILE-PATH-IS.ini" to your file path (make sure you have .ini on the end.)

The next thing you have to do to make this work is change where it says OpenMode.Output. You use OpenMode.Output to write to a text file, you use OpenMode.Input when you want to read from a text file (you would use that when you load the application) and you use OpenMode.Append to just add text on.

Now there are some things you need to look out for:

  1. When you use OpenMode.Output it actually clears all the text from the text file first and then writes the text you want to write.

  2. When you use OpenMode.Input you can't use PrintLine(1, "WHATEVER") as that is for writing to the text file not reading - so it will just crash. When using OpenMode.Input to read from the text file you have to use LineInput(1)

    For Example:

    Dim bool As Boolean FileOpen(1, "WHATEVER", OpenMode.Input) If LineInput(1) = "1" Then bool = True Else bool = False FileClose(1)

    This code will read the .ini file and if it says 1 in it then it will set the value to True and if it has 0 in it then it will set the value to False!

So here is the code after all going through all that!

To load the values:

Dim bool As Boolean
FileOpen(1, "WHATEVER.ini", OpenMode.Input)
If LineInput(1) = "1" Then bool = True Else bool = False
FileClose(1)

To save the values:

Dim bool As Boolean
FileOpen(1, "WHATEVER.ini", OpenMode.Input)
If bool = True Then PrintLine(1, "1") Else PrintLine(1, "0")
FileClose(1)

(don't forget you can add as many PrintLine(1, "") and LineInput(1) as you want)

Upvotes: 1

SSS
SSS

Reputation: 5403

The traditional way is to use GetPrivateProfileString (or GetPrivateProfileSection) to retrieve INI settings, and WritePrivateProfileString (or WritePrivateProfileSection) to change them.

You can find the syntax at PInvoke

Upvotes: 2

Douglas Barbin
Douglas Barbin

Reputation: 3615

If you just need to create a file, then File.WriteAllText is probably what you need. If it is a large file, you can use the StringBuilder class to build up the contents of the file, or if it is a small file, you can use simple string concatenation. After you have your string, you can use File.WriteAllText to write it to disk.

Upvotes: 2

Related Questions