C#调用win32 API读写INI文件
2014-06-27 18:07
489 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace INI { class ClassINI:IDisposable { [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 ref Val, int size, string filePath); private bool bDisposed = false; private string _FilePath = string.Empty; public string FilePath { get { if (_FilePath == null) return string.Empty; else return _FilePath; } set { if (_FilePath != value) _FilePath = value; } } ///<summary> ///構造函數 ///</summary> ///<param name="path">檔案路徑</param> public ClassINI(string path) { _FilePath = path; } ///<summary> ///析構函數 ///</summary> ~ClassINI() { Dispose(false); } ///<summary> ///釋放資源(程序設計師呼叫) ///</summary> public void Dispose() { Dispose(true); GC.SuppressFinalize(this); //要求系統不要呼叫指定物件的完成項 } ///<summary> ///釋放資源(給系統呼叫的) ///</summary> protected virtual void Dispose(bool isDisposing) { if (bDisposed) { return; } if (isDisposing) { } bDisposed = true; } ///<summary> ///設定KeyValue值 ///</summary> ///<param name="IN_Section">Section</param> ///<param name="IN_Key">Key</param> ///<param name="IN_Value">Value</param> public void setKeyValue(string IN_Section,string IN_Key,string IN_Value) { WritePrivateProfileString(IN_Section, IN_Key, IN_Value, this._FilePath); } ///<summary> ///取得Key相對的Value值 ///</summary> ///<param name="IN_Section">Section</param> ///<param name="IN_Key">Key</param> public string getKeyValue(string IN_Section, string IN_Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(IN_Section, IN_Key, "", temp, 255, this._FilePath); return temp.ToString(); } ///<summary> ///取得key相對的value值,若無,則使用預設值(DefaulValue) ///</summary> ///<param name="Section">Section</param> ///<param name="Key">Key</param> ///<param name="DefaultValue">DefaultValue</param> public string getKeyValue(string Section, string Key, string DefaultValue) { StringBuilder sbResult = null; try { sbResult = new StringBuilder(255); GetPrivateProfileString(Section, Key, "", sbResult, 255, this._FilePath); return (sbResult.Length > 0) ? sbResult.ToString() : DefaultValue; } catch { return string.Empty; } } } }
相关文章推荐
- C# 调用win32的api 读写 ini 文件
- C#调用API读写INI文件
- c#中调用操作INI文件的API
- c#调用win32 Api示例
- C# 使用API读写Ini文件的类
- C# API 大全- Win32调用
- C#调用Win32 API,实现简单的IP地址修改
- C#调用WIN32的API
- C#中调用WIN32的API
- c# 调用win32 API
- C#调用WIN32的API(转贴)
- C#调用Win32 API如何处理指针类型的参数
- 从.NET平台调用Win32 API(C#调用的dll动态链接库)
- C#调用非托管的 win32 api
- C#中调用WIN32的API
- Win32关于GDI 的API (Win32的API函数是微软自己的东西,可以直接在C#中直接调用,在做WinForm时还是很有帮助的。有时候我们之直接调用Win32 的API,可以很高效的实现想要)
- 继续深入C#调用win32 API的学习
- C#调用win32 API或称非托管代码
- C#中调用WIN32的API
- VB调用API读写INI文件