PDA

View Full Version : سوال: نحوه خواندن فايلهاي ini



سیروس مقصودی
چهارشنبه 18 شهریور 1394, 11:51 صبح
با سلام

من در c# چگونه ميتوانم فايلهاي ini را بخوانم

با تشكر

محمد رضا فاتحی
چهارشنبه 18 شهریور 1394, 12:02 عصر
این لینک کمکتون می کنه؟
http://www.codeproject.com/Articles/1966/An-INI-file-handling-class-using-C

hsgpro
چهارشنبه 18 شهریور 1394, 12:55 عصر
سلام
این کلاس هم می تونه راحت مقدار های موجود در فایل های INI رو با دو تابع GetValue و SetValue بخونه یا ویرایش کنه:


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

class INI
{
[DllImport("Kernel32" , EntryPoint = "GetPrivateProfileString")]
static extern int GetPrivateProfileString([MarshalAs(UnmanagedType.LPStr)] string lpApplicationName, string lpKeyName, string lpDefault, string lpReturnedString, int nSize, string lpFileName);
public string GetValue(string FileName , string Section,string Variable)
{
string result = new string(Convert.ToChar(0), 255);
GetPrivateProfileString(Section, Variable, "", result, result.Length, FileName);
return result.Replace(Convert.ToChar(0).ToString(), "");
}
[DllImport("Kernel32" , EntryPoint = "WritePrivateProfileString")]
static extern int WritePrivateProfileString([MarshalAs(UnmanagedType.LPStr)] string lpApplicationName, string lpKeyName, string lpString, string lpFileName);
public bool SetValue(string FileName, string Section, string Variable, string Value)
{
try
{
if (System.IO.File.Exists(FileName) == false)
{
System.IO.File.WriteAllText(FileName, "");
}
WritePrivateProfileString(Section, Variable, Value, FileName);
return true;
}
catch
{
return false;
}
}
}