

The config.xml file is expected to be in the same directory as your binary executable. ObjConfig.Value("control", "joystick") = "digital" ' Will be appended to the config.xml file MsgBox(objConfig.Value("video", "width", "oops")) ' Display the value of the video/width tag ("oops" if not found) ' If the SECTION no longer contains any keys, we remove it If Not ((pstrKey)) Thenĭ(pstrKey, GetType(String)) ' Check the specified KEY already exists and if it doesn't, create it ' Check the specified SECTION exists and if it doesn't, create it

' KEY found - The associated VALUE will be returned ' Note: The pstrDefault value (if specified) is returned in the event of a SECTION/KEY not being found Public Property Value(ByVal pstrSection As String, ByVal pstrKey As String, Optional ByVal pstrDefault As String = "") As String ' File not found or similar error - Invalid config.xml MstrFileName = (0, InStrRev(Application.ExecutablePath, "\")) + "config.xml" Private dsConfig As New DataSet("sections") MySettings.SaveSetting("OneString", TextBox1.Text) Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed TextBox1.Text = MySettings.GetSetting("OneString", "") Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Public Shared Sub SaveSetting(ByVal SettingName As String, ByVal Value As String)ĭt.Columns.Add(SettingName, GetType(String))Īdd a textbox to a form, and include the Class file above (Settings.vb) '- Return the value already in the dataset Add the column by saving the default value (see SaveSetting) If dt.Columns.Contains(SettingName) = False Then '- Get a reference to the first (and only table)ĭim dt As DataTable = dsSettings.Tables(0) Public Shared Function GetSetting(ByVal SettingName As String, ByVal DefaultValue As String) As String If you want more control,ĭsSettings.WriteXml(FileName, XmlWriteMode.WriteSchema) ' we don't have control over when this happens, it will happen
READ AND WRITE INI FILE VB NET WINDOWS
In this article we will create a class that represents INI files and uses Windows API functions to manipulate them. However, you can use Windows API functions with Platform Invocation Services (P/Invoke) to write to and read from the files. '- This happens when the object is Garbage-collected. NET framework does not support the use of INI files directly. '- We need to add a row, but before we can do that, we have to have a columnĭsSettings.Tables(0).Columns.Add("!!Default", GetType(String)) Read the XML file into the datasetĭsSettings.ReadXml(FileName, XmlReadMode.Auto) '- Does the file exist yet? (Have we already created it?) '- You can use any file, but this class names and creates it automaticallyįileName = Application.UserAppDataPath & "\settings.xml" Reading and writing to simple text files is. Private Shared dsSettings As New DataSet() The files for this tip are in the Ch7 Read and Write Files folder.

'- Use a dataset to store settings (easy to deal with) ' It uses the Shared keyword to prevent two or more instances accessing the '- This class saves settings in a local XML file instead of the registry. Open the INI file in one of the 3 following ways: // Creates or loads an INI file in the same directory as your executable // named EXE. Use freely, but don't remove this comment! Here's a utility class that I wrote to persist application settings to an XML file. Public Function IniReadValue(Section As String, Key As String) As Stringĭim i As Integer = GetPrivateProfileString(Section, Key, "", temp, 255, Me.INI files are a thing of the past. I find it very easy to write to a INI file, but am having some trouble in retrieving the data from an already created INI file. WritePrivateProfileString(Section, Key, Value, Me.path)

Public Sub IniWriteValue(Section As String, Key As String, Value As String) Private Shared Function GetPrivateProfileString(section As String, key As String, def As String, retVal As StringBuilder, size As Integer, filePath As String) As Integer Private Shared Function WritePrivateProfileString(section As String, key As String, val As String, filePath As String) As Long ''' Create a New INI file to store or load data
