 |
|
C# Read INI การอ่านไฟล์ INI ด้วย C#, C# อ่าน เขียนไฟล์ INI |
|
 |
|
|
 |
 |
|
ก็สวัสดีเพื่อนๆ พี่ๆ น้องๆทุกท่าน ที่มีความชื่นชอบการในพัฒนา Software ทุกๆท่านนะครับ
สำหรับในวันนี้ผมจะนำเสนอการอ่านไฟล์ INI ด้วย C# กันนะครับ
ในการอ่านไฟล์ INI ด้วย C# นั้นผมจะอาศัยส่วน DLL (Dynamic-link library) ของ ตัว WINDOWS เอางนะครับที่มีชื่อว่า KERNEL32.DLL กันนะครับ แบบว่าเขียน Code ไม่กี่บรรทัดเราก็จะได้ Class ที่ใช้ในการอ่าน + เขียนไฟล์ INI ด้วย C# กันนะครับ รับรองง่ายนิดเดียว
ก่อนการจะเรียก KERNEL32 มาใช้งานนั้นจะมีวิธีการ Import ตัว DLL (Dinamic-link library) มาใช้งานใน Class ของเราได้โดยการทำแบบนี้นะครับ
ขั้นตอนที่ 1 Using using System.Runtime.InteropServices เข้ามาใช้งานก่อน
ขั้นตอนที่ 2 ใช้ [DllImport("Kernel32")] ในการ Include ตัว Dll เข้ามาใช้งานใน Class ของเรา
งั้นเรามาเริ่มเขียนกันเลยนะครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace Fw
{
class IniFile
{
public 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 INIPath)
{
path = INIPath;
}
public void IniWriteValue(string Section, string Key, string Value)
{
WritePrivateProfileString(Section, Key, Value, this.path);
}
public string IniReadValue(string Section, string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
return temp.ToString();
}
}
}
เท่านี้แหละครับ เราก็จะได้ Class ที่ใช้ในการอ่านไฟล์ INI โดยเขียนจาก C# เป็นของเราเองแล้วครับ
ทีนี้ก็มาดูวิธีการใช้งานกันบ้าง
อืม... Code ตัวอย่างการใช้งานนี้ผมจะเขียนเฉพาะส่วนที่สำคัญๆ ที่จะใช้เท่านั้นนะครับ ในความเป็นจริงก็ไม่มีอะไรมากหรอกครับ เมื่อเราได้ Class ที่ผ่านไฟล์ INI ที่เราเขียนจาก C# แล้ว ตอนที่เราจะเรียกใช้งานก็แค่ Instance object class ขึ้นมาและเรียกใช้งาน Method + Property ต่างๆที่สามารถ Access ได้ภายใน Class ที่เรา Instance มาเท่านั้นเองแหละครับ ที่จริงก็เป็น Foundation ที่ทุกๆคนมีอยู่แล้ว
ผมสมมุติให้ไฟล์ INI ของผมเป็นแบบนี้นะครับ
[MY_SECTION]
KEY1=Thailand
KEY2=Bangkok
KEY3=Khonkean
Code (C#)
Fw.IniFile ini = new Fw.IniFile(@"c:\\iniTest.ini");
String _value = ini.IniReadValue("MY_SECTION", "KEY1");
ส่วนผลลัพธ์ ที่เราจะได้ก็คือ Thailand นั่นเองนะครับ
Tag : .NET, C#, Windows
|
|
 |
 |
 |
 |
Date :
2012-11-20 11:34:47 |
By :
ผู้ที่ต้องการเผยแพร่ความรู้ |
View :
10393 |
Reply :
10 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เยี่ยมไปเลยครับ
|
 |
 |
 |
 |
Date :
2012-11-21 12:09:05 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เวลาเรียกใช้นี่ ใช้ภายในclass IniFile ได้เลยรึเปล่าครับ
|
 |
 |
 |
 |
Date :
2013-06-13 09:43:05 |
By :
lackk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

ช่วยหน่อยครับ
|
 |
 |
 |
 |
Date :
2013-06-13 11:47:06 |
By :
lackk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ทำไมยังใช้ .ini ล่ะ
เป็นโปรเจ็คเก่าเหรอ
.NET เค้าใช้ .config กันแล้ว
ซึ่งถูกเก็บในรูปแบบ xml
มีเครื่องมือพร้อมใช้
อ่านข้อมูลก็ง่าย
ไม่ต้องเขียนโค้ดเพิ่ม
|
 |
 |
 |
 |
Date :
2013-06-13 12:09:17 |
By :
watcharop |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|

|
 |
 |
 |
 |
Date :
2013-06-13 16:44:05 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (C#)
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Linq;
using Microsoft.Win32;
namespace Fw
{
class IniFile
{
[DllImport("kernel32")]
private static extern int GetPrivateProFileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
static void Main(string[] args)
{
string readLocation = test("Setting","LocationName","C:\\preconfig.ini");
if (readLocation == "")
{
Console.WriteLine("NOT Found");
}
else
{
Console.WriteLine(readLocation);
}
}
static string test(string Section, string Key, string FilePath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProFileString(Section, Key, "", temp, 255, FilePath); บรรทัดนี้เออเร่อครับ
return temp.ToString();
}
}
}
ผู้รู้ช่วยทีครับ
|
 |
 |
 |
 |
Date :
2013-06-18 16:15:03 |
By :
lackk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
error บรรทัด 32 ว่า Unable to find an entry point named 'GetPrivateProFileString' in DLL 'kernel32'.
|
 |
 |
 |
 |
Date :
2013-06-18 16:19:51 |
By :
lackk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมได้แล้วครับ แต่ถ้าจะนำข้อมูลที่อ่านไปเชื่อมต่อกับ MySql จะนำค่าไปเชื่อมต่อยังไงครับ
|
 |
 |
 |
 |
Date :
2013-06-19 10:18:41 |
By :
lackk |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมอยากรู้ด้วยคนครับ มีผู้รู้ไหม ผมคล้ายๆเม้นบน คือผมต้องเอาค่าที่อ่านออกมาได้จาก ini File ไปเชื่อมต่อกับ Mysql แล้วดึงข้อมูลตามที่ต้องการจาค่าในไฟล์ ini
|
 |
 |
 |
 |
Date :
2013-06-19 17:10:57 |
By :
Blackreborn |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|