Diễn đàn    Dotnet framework    Đọc và ghi file INI trong windows application

Thành viênTrả lời
khanhjin

khanhjin
17  bài
9-6-2008 22:5:2
Đơn giản lắm chỉ cần mấy dòng thế này thôi

----------

Public Class IniFile
Public path As String
<DllImport("kernel32")> Private Shared Function WritePrivateProfileString(ByVal section As String, ByVal key As String, ByVal val As String, ByVal filePath As String) As Long
End Function
<DllImport("kernel32")> Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
End Function

Public Sub New(ByVal Path As String)
Me.path = Path
End Sub

Public Sub WriteValue(ByVal section As String, ByVal key As String, ByVal value As String)
WritePrivateProfileString(section, key, value, Me.path)
End Sub

Public Function ReadValue(ByVal section As String, ByVal key As String) As String
Dim tmp As New StringBuilder(255)
Dim i As Integer = GetPrivateProfileString(section, key, "", tmp, 255, Me.path)
Return tmp.ToString()
End Function
End Class
 
khanhjin

khanhjin
17  bài
9-6-2008 22:5:51
Còn trên C# thì
--------------

public class IniFile
{
private string path = "";
[DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
[DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filepath);
public IniFile(string thePath)
{
//
// TODO: Add constructor logic here
//
this.path = thePath;
}

public string ReadValue(string section, string key)
{
StringBuilder tmp = new StringBuilder(255);
int i = GetPrivateProfileString(section, key, "", tmp, 255, this.path);
return tmp.ToString();
}

public void WriteValue(string section, string key, string val)
{
WritePrivateProfileString(section, key, val, this.path);
}

}
 
khanhjin

khanhjin
17  bài
9-6-2008 22:8:11
download tại đây

http://coder.awas.vn/downloads.ashx?id=67e41a03e53a4aa292c1fb77c61e9397
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM