1. 程式人生 > 實用技巧 >C# 利用動態庫DllImport("kernel32")讀寫ini檔案

C# 利用動態庫DllImport("kernel32")讀寫ini檔案

1 ini檔案讀寫

kernel32.dll是Windows中非常重要的32位動態連結庫檔案,屬於核心級檔案。它控制著系統的記憶體管理、資料的輸入輸出操作和中斷處理,當Windows啟動時,kernel32.dll就駐留在記憶體中特定的防寫區域,使別的程式無法佔用這個記憶體區域。

為什麼要來討論讀寫ini檔案呢,難道有資料庫我們就不用讀寫檔案了嗎,什麼資料都從資料庫讀取出來嗎,有些東西是根據客戶的習慣,就沒必要去讀取資料庫了,或者說,比如你要做一個記住密碼的功能,如果在web端,你還可以用cookie這東西,但是要是winform呢,這時候ini檔案就可以派上用場了。我們可以把使用者和密碼存在ini檔案裡。

注意事項:

  • ini檔案路徑必須完整
  • 可將ini放在程式所在目錄,此時IpFileName引數為“.\FileName.ini”

2 方法

(1)GetPrivateProfileInt :使用該方法可獲取ini型別資料,未獲取到時則會取設定的預設資料

UINT WINAPI GetPrivateProfileInt
(
_In_LPCTSTR lpAppName, //ini檔案中區塊名稱
_In_LPCTSTR lpKeyName, //鍵名
_In_INT nDefault, //預設值
_In_LPCTSTR lpFileName //ini檔案路徑
);

(2)GetPrivateProfileString:使用該方法可獲取string型別資料,未獲取到時則會取設定的預設資料

UINT WINAPI GetPrivateProfileString
(
_In_LPCTSTR lpAppName, //ini檔案中區塊名稱
_In_LPCTSTR lpKeyName, //鍵名
_In_INT nDefault, //預設值
_In_LPSTR lpReturnedString,//接受ini檔案中值的CString物件,指定一個字串緩衝區,長度至少為nSize
_In_DWORD nSize,//指定裝載到IpReturnedString緩衝區的最大字元數
_In_LPCTSTR lpFileName //ini檔案路徑
);

(3)WritePrivateProfileString:向ini中寫值,所以僅有寫入string就足夠了

BOOL WritePrivateProfileString(
LPCTSTR lpAppName,//ini檔案中區塊名
LPCTSTR lpKeyName,//鍵名
LPCTSTR lpString,//鍵值
LPCTSTR lpFileName
);

3 例子

using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;

namespace WpfApplication3
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
      
            IniClass ini = new IniClass();
            string IniPath = System.Windows.Forms.Application.StartupPath + "\\temp.ini";//引用裡新增form才可以
            string section = "BigBang";
            ini.SetPath(IniPath);
            ini.IniWriteValue(section, "1", "Amy");
            ini.IniWriteValue(section, "2", "Sheldon");
            string section1 = "Family";
            ini.SetPath(IniPath);
            ini.IniWriteValue(section1, "1", "Phil");
            ini.IniWriteValue(section1, "2", "Jay");
            string re = ini.IniReadValue(section, "1");
            System.Windows.MessageBox.Show(re);
        }


    }
    public class IniClass
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key,
           string value, string filepath);

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

        private string iniPath;
        public void SetPath(string iniPath)
        {
            this.iniPath = iniPath;
        }
        public void IniWriteValue(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, this.iniPath);
        }
        public string IniReadValue(string section, string key)
        {
            StringBuilder temp = new StringBuilder(500);
            GetPrivateProfileString(section, key, "", temp, 500, iniPath);
            return temp.ToString();
        }
        public bool ExistFile()
        {
            return File.Exists(this.iniPath);
        }
    }
}

轉載:https://www.cnblogs.com/zknu/archive/2013/10/30/3397326.html

https://blog.csdn.net/weixin_30486037/article/details/98994368