C#編程實踐

來源:互聯網
上載者:User
編程 最近一段時間學習使用C#編程,因為用慣了Delphi,發現C#類庫還是不太完善(我用的是.Net Framework 1.0,不知道.Net Framework 1.1有哪些改進),此外Visual Studio 2002也有不完善的地方,不知道Visual Studio 2003有哪些改進呢。比如沒有提供Ini檔案的訪問類,比如輸入框不能像Delphi那樣指定預設的IME( 更正:為了控制IME,.NET類庫在System.Windows.Forms.InputLanguage類中提供了支援),為此我不得不寫了一個Ini訪問類和根據IME名稱切換IME的類。

問題列表:

C# Ini訪問類
C# IME切換類
使用C#讀寫檔案
格式化字串
從Assemble中載入自訂資源
對StringCollection進行排序
C#Builder的Open Tools Api的Bug
使用反射動態設定組件屬性
將字串複製到剪貼簿
擷取程式檔案的版本資訊
利用反射動態載入Assembly動態執行類型方法
其他問題

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 修正了對中文系統的支援。
/// 1.2 增加了UpdateFile方法,實現了對Win9x的支援
/// 1.3 增加了讀寫布爾,整數的操作
/// 1.4 修正了寫Ini雖然成功,但是會拋出異常的錯誤
/// 1.5 ReadString返回的是Trim後的字串
/// 1.6 統一併擴大了讀寫緩衝區的大小
/// </summary>
public class IniFile {
public string FileName; //INI檔案名稱
//聲明讀寫INI檔案的API函數
[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);
//類的建構函式,傳遞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(系統預設的字碼頁)的編碼方式,否則無法支援中文
string s=Encoding.GetEncoding(0).GetString(Buffer);
s=s.Substring(0,bufLen);
return s.Trim();
}

//讀整數
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;
}
}

//寫整數
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;
}
}

[1] [2] [3] [4] [5] 下一頁  



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.