wpf中讀寫配置文件的方法

2022-08-31 15:18:20 字數 3208 閱讀 6511

wpf

配置檔案讀取與寫入:

1、在程式根目錄下新建文字文件,將字尾名改成.ini檔案;(將已存在的param.dat的字尾改成。

ini)

譬如:param.ini

1) 文件的寫法:ini檔案由節、鍵、值組成。

2、vs專案中新建類:

inifile.cs

class inifile

private string filepath;

[dllimport("kernel32")]

private static extern long writeprivateprofilestring(string strsection, string strkey, string strval, string strfilepath);

[dllimport("kernel32")]

private static extern int getprivateprofilestring(string strsection, string strkey, string strdef, stringbuilder sbretval, int intsize, string strfilepath);

public inifile(string strfilepath)

this.filepath = strfilepath;

public void write(string strsection, string strkey, string strvalue)

writeprivateprofilestring(strsection, strkey, strvalue, this.filepath);

public string read(string strsection, string strkey)

stringbuilder sb = new stringbuilder(255);

int i = getprivateprofilestring(strsection, strkey, "", sb, 255, this.filepath);

return sb.tostring();

public string filepath

get 

set 

3、呼叫類方法:

//定義

inifile ini_param = new inifile(environment.currentdirectory + @"\param.ini");

//讀取配置文件引數

private bool readparam()

tryfloat buff;

"read setting information...");

buff = convert.tosingle(ini_param.read("sysparam", "range"));

rangetb.text = buff.tostring();

buff = convert.tosingle(ini_param.read("sysparam", "grade"));

combobox.text = buff.tostring();

buff = convert.tosingle(ini_param.read("sysparam", "marginerror"));

marginerrortxt.text = buff.tostring();                         

return true;

catch(exception ex)

errorlog.writelog("讀取引數失敗:

" + ex.message.tostring());

return false;

//讀取工位資訊

private listreadstation()

liststationinfo = new list();            

stationinfo.add(ini_param.read("stationinfo", "station1"));

stationinfo.add(ini_param.read("stationinfo", "station2"));

stationinfo.add(ini_param.read("stationinfo", "station3"));

stationinfo.add(ini_param.read("stationinfo", "station4"));

stationinfo.add(ini_param.read("stationinfo", "station5"));

stationinfo.add(ini_param.read("stationinfo", "station6"));

stationinfo.add(ini_param.read("stationinfo", "station7"));

stationinfo.add(ini_param.read("stationinfo", "station8"));

stationinfo.add(ini_param.read("stationinfo", "station9"));

stationinfo.add(ini_param.read("stationinfo", "station10"));

return stationinfo;

//將資料更新到配置文件中

private void updateparam()

trystring buff;

buff = rangetb.text;

ini_param.write("sysparam", "range", buff);

buff = combobox.text;

ini_param.write("sysparam", "grade", buff);

buff = marginerrortxt.text;

ini_param.write("sysparam", "marginerror", buff);

catch(exception ex)

errorlog.writelog("寫入引數失敗:

" + ex.message.tostring());

文件的讀寫

法一 include include include using namespace std int main char ch while in.eof in.close 法二 file fp1 fopen in.txt r 絕對路徑雙反斜槓 file fp2 fopen out.txt w fre...

wpf中xps文件合併功能實現

原文 wpf中xps文件合併功能實現 如果要是直接先開啟乙個xps檔案,用新檔案去寫fixeddocument,或者documentsequence 新增到新的檔案中的話,會報錯,提示,system.windows.xps.xpspackagingexception 型別的未經處理的異常在 reac...

讀寫appSettings配置節方法

key socket path mw data value d monitor socket mw data 新增system.configuration.dll引用 引用using system.configuration命名空間 socket path mw data 的方法進行訪問,但該方法不...