CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 588

588
Chương 15: Khả năng liên tác mã lệnh không-được-quản-lý

private static extern bool WritePrivateProfileString(

string lpAppName, string lpKeyName,

string lpString, string lpFileName);

// Bốn hàm sau không được sử dụng trong ví dụ này,

// nhưng được khai báo cho đầy đủ.

[DllImport("kernel32.dll", EntryPoint="WritePrivateProfileInt")]

private static extern int GetPrivateProfileInt(string lpAppName,

string lpKeyName, int iDefault, string lpFileName) ;

[DllImport("kernel32.dll", EntryPoint="GetPrivateProfileSection")]

private static extern int GetPrivateProfileSection(

string lpAppName, byte[] lpReturnedString,

int nSize, string lpFileName);

[DllImport("kernel32.dll", EntryPoint="WritePrivateProfileSection")]

private static extern bool WritePrivateProfileSection(

string lpAppName, byte[] data, string lpFileName);

[DllImport("kernel32.dll",

EntryPoint="GetPrivateProfileSectionNames")]

private static extern int GetPrivateProfileSectionNames(

byte[] lpReturnedString, int nSize, string lpFileName);

public string GetIniValue(string section, string key) {

StringBuilder buffer = new StringBuilder();

string sDefault = "";

if (GetPrivateProfileString(section, key, sDefault,

buffer, buffer.Capacity, filename) != 0) {

return buffer.ToString();

} else {

return null;

}

}