問題列表:
c# ini訪問類
c# 輸入法切換類
使用c#讀寫文件
格式化字符串
從assemble中加載自定義資源
對stringcollection進行排序
c#builder的open tools api的bug
使用反射動態(tài)設定組件屬性
將字符串復制到剪貼板
獲取程序文件的版本信息
利用反射動態(tài)加載assembly動態(tài)執(zhí)行類型方法
其他問題
c# ini訪問類
using system;
using system.io;
using system.runtime.interopservices;
using system.text;
using system.collections;
using system.collections.specialized;
using system.windows.forms;
namespace sharpplus.ini {
/// <summary>
/// 一個模仿delphi的tinifile的類
/// 修訂:1.1 修正了對中文系統(tǒng)的支持。
/// 1.2 增加了updatefile方法,實現(xiàn)了對win9x的支持
/// 1.3 增加了讀寫布爾,整數(shù)的操作
/// 1.4 修正了寫ini雖然成功,但是會拋出異常的錯誤
/// 1.5 readstring返回的是trim后的字符串
/// 1.6 統(tǒng)一并擴大了讀寫緩沖區(qū)的大小
/// </summary>
public class inifile {
public string filename; //ini文件名
//聲明讀寫ini文件的api函數(shù)
[dllimport("kernel32")]
private static extern bool writeprivateprofilestring(string section,string key,string val,string filepath);
[dllimport("kernel32")]
private static extern int getprivateprofilestring(string section,string key,string def, byte[] retval,int size,string filepath);
//類的構造函數(shù),傳遞ini文件名
public inifile(string afilename) {
// 判斷文件是否存在
fileinfo fileinfo=new fileinfo(afilename);
//todo:搞清枚舉的用法
if ((!fileinfo.exists)) //|| (fileattributes.directory in fileinfo.attributes))
throw(new applicationexception("ini文件不存在"));
//必須是完全路徑,不能是相對路徑
filename = fileinfo.fullname;
}
//寫ini文件
public void writestring(string section,string ident,string value) {
if (!writeprivateprofilestring(section, ident,value,filename))
{
// todo:拋出自定義的異常
throw(new applicationexception("寫ini文件出錯"));
}
}
//讀取ini文件指定
public string readstring(string section,string ident, string default) {
//stringbuilder buffer = new stringbuilder(255);
byte[] buffer=new byte[65535];
int buflen=getprivateprofilestring(section,ident,default,buffer, buffer.getupperbound(0),filename);
//必須設定0(系統(tǒng)默認的代碼頁)的編碼方式,否則無法支持中文
string s=encoding.getencoding(0).getstring(buffer);
s=s.substring(0,buflen);
return s.trim();
}
//讀整數(shù)
public int readinteger(string section, string ident , int default){
string intstr=readstring(section, ident, convert.tostring(default));
try{
return convert.toint32(intstr);
}
catch (exception ex){
console.writeline(ex.message);
return default;
}
}
//寫整數(shù)
public void writeinteger(string section,string ident, int value){
writestring(section, ident, value.tostring());
}
//讀布爾
public bool readbool(string section, string ident, bool default){
try
{
return convert.toboolean(readstring(section, ident, convert.tostring(default) ));
}
catch (exception ex){
console.writeline(ex.message);
return default;
}
}
新聞熱點
疑難解答
圖片精選