- [DEVEXPRESS] Hướng dẫn bật tính năng Scroll Pixcel in Touch trên GridView
- [DEVEXPRESS] Hướng dẫn sử dụng TileBar viết ứng dụng duyệt hình ảnh Winform
- [DEVEXPRESS] Tô màu border TextEdit trên Winform
- [C#] Lấy dữ liệu từ Console Write hiển thị lên textbox Winform
- [C#] Hiển thị Progress bar trên Window Console
- [C#] Di chuyển control Runtime và lưu layout trên winform
- [SQLSERVER] Sử dụng hàm NULL IF
- [C#] Chia sẽ source code mã đi tuần bằng giao diện Winform
- [C#] Flash Window in Taskbar Winform
- Download và Giải nén tập tin File sử dụng Powershell
- [C#] Hướng dẫn cách lấy thông tin đăng nhập tài khoản và mật khẩu web browser trên windows
- [VB.NET] CRUD Thêm xóa sửa tìm kiếm Realtime FireBase
- [C#] Hiển thị thông báo Toast Message trong lập trình Winform
- [C#] Cấu hình định dạng ngày tháng, thời gian trên Windows cho ứng dụng Winform
- [C#] Rút gọn đường dẫn link url với TinyURL API
- [C#] Hướng dẫn cách bo tròn winform with Radius
- [C#] Chia sẽ class BackGroundOverlay Show Modal cho Winform
- [C#] Hướng dẫn Flip Image Winform
- [C#] Invoke là gì? cách sử dụng phương thức Invoke()
- [C#] Hướng dẫn chia sẽ file, folder từ ứng dụng sang Zalo Chat
[C#] Hướng dẫn ghi thông tin cài đặt setting form vào file INI
Bài viết hôm nay, mình sẽ hướng dẫn các bạn ghi các thuộc tính cài đặt của winform vào file setting INI file C#.
1. Tổng quan một file ini
Dưới đây là cấu trúc của một file setting như sau:
[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);
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é!