[API] Ini - Ghi các setting cực tốt !

Đăng bởi : 4/14/2014

Với một phần mềm có chức năng tùy chỉnh thì việc lưu các setting là rất quan trọng.
Hôm nay mình xin hướng dẫn các bạn một trong các kiểu ghi/đọc các setting cực kì phổ biến -- Đọc/Ghi Ini

1. Tổng quan một file ini
1 file được ghi theo kiểu Ini sẽ có dạng sau đây :

[Section]
Key=Value

Ví dụ mình muốn lưu cái "Tự động đăng nhập" cho phần mềm của mình thì File ini sẽ có dạng sau :
[TuDongDangNhap]
KichHoat=1
TaiKhoan=buiducduy111

2. Thực hiện trong C#

2.1 . Đừng quên " using System.Runtime.InteropServices;" để có thể sử dụng các hàm API
2.2. Sử dụng DLL :  kernel32

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

OK vậy là bạn có thể Đọc/Ghi dữ liệu từ file setting theo cách trên

Nếu chưa làm được hãy xem code dưới đây (Bạn chỉ cần sử dụng INI.READ và INI.WRITE thôi)
public class INI
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
        [DllImport("kernel32")]
        private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filepath);

                      // Viết thêm 2 hàm mới cho dễ  
        public static string READ(string szFile, string szSection, string szKey)
        {
            StringBuilder tmp = new StringBuilder(255);
            long i = GetPrivateProfileString(szSection, szKey, "", tmp, 255, szFile);
            return tmp.ToString();
        }
        public static void WRITE(string szFile, string szSection, string szKey, string szData)
        {
            WritePrivateProfileString(szSection, szKey, szData, szFile);
        }
    }

3. Thực hành
Ví dụ : (Sử dụng class INI được xây dựng bên trên)
INI.WRITE("Data.ini","TuDongDangNhap","TaiKhoan", "buiducduy");
INI.WRITE("Data.ini","TuDongDangNhap","KichHoat", "1");
Kết quả trong Data.ini :
[TuDongDangNhap]
TaiKhoan=buiducduy111
KichHoat=1

string s = INI.READ("Data.ini","TuDongDangNhap","TaiKhoan");
MessageBox.Show(s);
Kết quả "buiducduy";

OK. Nếu chưa hiểu bạn cứ comment ở dưới nhé!


{ 4 comments }

  1. Đoạn này hình như có gì đó sai...
    INI.WRITE("Data.ini","TuDongDangNhap","TaiKhoan", "buiducduy");
    INI.WRITE("Data.ini","TuDongDangNhap","KichHoat", "1");
    Kết quả trong Data.ini :
    [TuDongDangNhap]
    TaiKhoan=buiducduy111 // có 111 đúng k
    KichHoat=1

    ReplyDelete
  2. File data.ini nó được lưu vào đâu vậy, vậy còn muốn xóa thì làm sao delete ?

    ReplyDelete
    Replies
    1. file ini của bạn sẽ nằm ở C:\Windows khi bạn thực hiện hàm trên

      Delete
    2. Được lưu ngay tại thư mục thực thi chương trình bạn ah

      Delete

Nhận ngay 100$ cho VPS

Mua hàng ủng hộ page

Ủng hộ page

Nhãn

Code (45) Team Foundation Server (17) Database (14) News (14) product (13) toolbox (10) Linq (9) SoftDesign (8) XNA (6) API (5) Project (5) item (4)

- Bản quyền thuộc về Thư Viện WinForm - Giao diện: Metrominimalist - Thiết kế: Johanes Djogan -