SoFunction
Updated on 2025-03-07

C# Winform code to call system interface operation INI configuration file


// System interface class
public static class WinAPI
{
[DllImport("kernel32")] // Interface to write configuration files
private static extern long WritePrivateProfileString(
string section, string key, string val, string filePath);

[DllImport("kernel32")] // Interface for reading configuration files
private static extern int GetPrivateProfileString(
string section, string key, string def,
StringBuilder retVal, int size, string filePath);

// Write values ​​to the configuration file
public static void ProfileWriteValue(
string section, string key, string value, string path)
{
WritePrivateProfileString(section, key, value, path);
}

// Read the value of the configuration file
public static string ProfileReadValue(
string section, string key, string path)
{
StringBuilder sb = new StringBuilder(255);
GetPrivateProfileString(section, key, "", sb, 255, path);
return ().Trim();
}
}