Reading/writing an INI file
The creators of the .NET framework want you to use XML-based config files, rather than INI files. So no, there is no built-in mechanism for reading them.
There are third party solutions available, though.
- INI handlers can be obtained as NuGet packages, such as INI Parser.
- You can write your own INI handler, which is the old-school, laborious way. It gives you more control over the implementation, which you can use for bad or good. See e.g. an INI file handling class using C#, P/Invoke and Win32.
How to read and write INI file with Python3?
This can be something to start with:
import configparser
config = configparser.ConfigParser()
config.read('FILE.INI')
print(config['DEFAULT']['path']) # -> "/path/name/"
config['DEFAULT']['path'] = '/var/shared/' # update
config['DEFAULT']['default_message'] = 'Hey! help me!!' # create
with open('FILE.INI', 'w') as configfile: # save
config.write(configfile)
You can find more at the official configparser documentation.
Reading from an INI file
Going to the Pinvoke.Net Web site and modifying their example worked, their Function declaration is different.
Modified Example
Imports System.Runtime.InteropServices
Imports System.Text
Module Module1
Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (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
Sub Main()
Dim res As Integer
Dim sb As StringBuilder
sb = New StringBuilder(500)
res = GetPrivateProfileString("testApp", "KeyName", "", sb, sb.Capacity, "c:\temp\test.ini")
Console.WriteLine("GetPrivateProfileStrng returned : " & res.ToString())
Console.WriteLine("KeyName is : " & sb.ToString())
Console.ReadLine();
End Sub
End Module
How to read from an .ini file?
Assuming you are using Windows, you can work this way:
First include Windows.h
#include <Windows.h>
On your function, create a LPCSTR
(heh!? Really? Yeah.)
LPCSTR ini = "C:\\config.ini";
And call GetPrivateProfileString
:
char returnValue[100];
GetPrivateProfileString("states", "title", 0, returnValue, 100, ini);
Reading from an ini file with no Sections
Reading the settings you've shown is fairly straightforward and a ReadLine
with a String.Split
on a space will do (or will be what is happening at the lowest level).
However, are there settings with more (or less) than one "word" on the RHS (no harder because String.Split
can stop at the first space)?
Are there blank lines or comments and what is the comment markers?
How To Read and Write to a INI file in Python
"Is there a way to only rewrite the value I am changing?" No, because it's a text file. You can only do a selective rewrite when you know that the thing you're writing will be exactly the same length as the thing you're replacing. That's not generally the case with text files, and they're almost never treated that way.
I'd do only a small re-organization of this function to remove redundancy:
def read_or_write_file(self, file, section, passed_option = None,
value = None, read = True):
config = ConfigParser.RawConfigParser()
with open(file) as configfile:
config.readfp(configfile)
if read:
options = config.options(section)
for option in options:
file_settings[option] = config.get(section, option)
else:
config.set(section, passed_option, value)
with open(file, 'w') as configfile:
config.write(configfile)
Read/Write INI file
You can use some of the kernel32 functions.
Private Declare Auto Function GetPrivateProfileString Lib "kernel32" (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
This will let you read an ini file
Dim sb As StringBuilder
sb = New StringBuilder(500)
GetPrivateProfileString("IMPORT-1", "SETTINGS", "", sb, sb.Capacity, "test.ini")
How to read and write to an ini file with PHP
The PEAR Config_Lite package can do almost all the work (both reading and writing) for you super-easily. Check it out here: http://pear.php.net/package/Config_Lite
Related Topics
How to Call Asynchronous Method from Synchronous Method in C#
What Is the Correct Way to Create a Single-Instance Wpf Application
What Are the Differences Between a Multidimensional Array and an Array of Arrays in C#
Fastest Way of Inserting in Entity Framework
Collection Was Modified; Enumeration Operation May Not Execute
Is There a Constraint That Restricts My Generic Method to Numeric Types
Convert a String to an Enum in C#
How to Add User-Supplied Input to an SQL Statement
Windows Service Not Shown in Add Remove Programs Under Control Panel
The Calling Thread Cannot Access This Object Because a Different Thread Owns It
How to Dynamically Compile and Execute C# Code Fragments
Embedding Dlls in a Compiled Executable
What Are the Uses of "Using" in C#
How to Convert a Hex String to a Byte Array
Can't Specify the 'Async' Modifier on the 'Main' Method of a Console App
Textboxfor Displaying Initial Value, Not the Value Updated from Code