C# 读写 Ini 文件

using System.Runtime.InteropServices;
public class IniFile
{
    
private string path;

    
public IniFile(string iniPath)
    {
        
this.path = iniPath;
    }

    [DllImport(
"kernel32")]
    
private static extern int GetPrivateProfileString
        (
string section, string key, string def, StringBuilder retVal, int size, string filePath);
    [DllImport(
"kernel32")]
    
private static extern long WritePrivateProfileString
        (
string section, string key, string val, string filePath);

    
public string IniReadValue(string section, string key)
    {
        
try
        {
            StringBuilder retVal 
= new StringBuilder(0xff);
            
if (GetPrivateProfileString(section, key, "", retVal, 0xffthis.path) == 0)
            {
                
return string.Empty;
            }
            
return retVal.ToString();
        }
        
catch (Exception exception)
        {
            
return exception.ToString();
        }
    }

    
public void IniWriteValue(string Section, string Key, string Value)
    {
        WritePrivateProfileString(Section, Key, Value, 
this.path);
    }
}
原文地址:https://www.cnblogs.com/anjou/p/1584715.html