Code: Operate *.INI File Class in C#

简介:
None.gif using System;
None.gif using System.Text;
None.gif using System.Runtime.InteropServices;
None.gif
None.gif namespace Birdshome
ExpandedBlockStart.gif{
ExpandedSubBlockStart.gif     ///   <summary>
InBlock.gif    
///  Create a new INI file to store or load data
ExpandedSubBlockEnd.gif    
///   </summary>
InBlock.gif     public  class IniFile
ExpandedSubBlockStart.gif    {
InBlock.gif         private  string m_Path;
InBlock.gif 
InBlock.gif        # region Windows API Functions
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  long WritePrivateProfileString( string lpAppName,
InBlock.gif             string lpKeyName,  string lpReturnString,  string lpFilePath);
InBlock.gif        
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  int GetPrivateProfileString( string lpAppName,
InBlock.gif             string lpKeyName,  string lpDefault,  byte[] byBuffer,  int size,  string lpFilePath);
InBlock.gif 
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  int WritePrivateProfileSection( string lpAppName, 
InBlock.gif             string lpString,  string lpFileName);
InBlock.gif
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  int GetPrivateProfileSection( string lpAppName, 
InBlock.gif             string lpReturnString,  string lpFilePath);
InBlock.gif
InBlock.gif        [DllImport("kernel32")]
InBlock.gif         public  static  extern  int GetPrivateProfileInt( string lpAppName, 
InBlock.gif             string lpKeyName,  int iDefault,  string lpFilePath);
InBlock.gif         #endregion
InBlock.gif
ExpandedSubBlockStart.gif         ///   <summary>
InBlock.gif        
///  IniFile Constructor.
InBlock.gif        
///   </summary>
ExpandedSubBlockEnd.gif        
///   <PARAM name="IniPath"></PARAM>
InBlock.gif         public IniFile( string strIniPath)
ExpandedSubBlockStart.gif        {
InBlock.gif            m_Path = strIniPath;
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gif         ///   <summary>
InBlock.gif        
///  Write Data to the INI File
ExpandedSubBlockEnd.gif        
///   </summary>
InBlock.gif         public  long SetValue( string section,  string key,  string value)
ExpandedSubBlockStart.gif        {
InBlock.gif             return WritePrivateProfileString(section, key, value, m_Path);
ExpandedSubBlockEnd.gif        }
InBlock.gif
ExpandedSubBlockStart.gif         ///   <summary>
InBlock.gif        
///  Read Data Value From the Ini File
ExpandedSubBlockEnd.gif        
///   </summary>
InBlock.gif         private  string [] GetValues( string section,  string key)
ExpandedSubBlockStart.gif        {
InBlock.gif             byte [] buff;
InBlock.gif             int BUFFER_SIZE = 0xff;
InBlock.gif             int iCount, iBufferSize, iMultiple = 0;
InBlock.gif             do
ExpandedSubBlockStart.gif            {
InBlock.gif                iMultiple ++;
InBlock.gif                iBufferSize = BUFFER_SIZE*iMultiple;
InBlock.gif                buff =  new  byte[iBufferSize];
InBlock.gif                iCount = GetPrivateProfileString(section, key, "", buff, iBufferSize, m_Path);
ExpandedSubBlockEnd.gif            }
InBlock.gif             while(iCount == iBufferSize-2);
InBlock.gif            
InBlock.gif             forint i=0 ; i < iCount ; ++i )
ExpandedSubBlockStart.gif            {
InBlock.gif                 if ( buff[i] == 0 && iCount != i+1 && buff[i+1] != 0 )
ExpandedSubBlockStart.gif                {
InBlock.gif                    buff[i] = ( int)'\n';
InBlock.gif                     continue;
ExpandedSubBlockEnd.gif                }
InBlock.gif                 if ( i > 0 && buff[i-1] == 0 && buff[i] == 0 )  break;
ExpandedSubBlockEnd.gif            }
InBlock.gif             string strResult = Encoding.Default.GetString(buff).Trim();
ExpandedSubBlockStart.gif             return strResult.Trim( new  char [] {'\0'}).Split( new  char [] {'\n'});
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  string GetValue( string section,  string key)
ExpandedSubBlockStart.gif        {
InBlock.gif             if ( section ==  null || key ==  null )
ExpandedSubBlockStart.gif            {
InBlock.gif                 throw  new NullReferenceException("Section or Key");
ExpandedSubBlockEnd.gif            }
InBlock.gif             return GetValues(section, key)[0];
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  string [] GetKeys( string section)
ExpandedSubBlockStart.gif        {
InBlock.gif             if ( section ==  null )
ExpandedSubBlockStart.gif            {
InBlock.gif                 throw  new NullReferenceException("Section");
ExpandedSubBlockEnd.gif            }
InBlock.gif             return GetValues(section,  null);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  string [] GetSections()
ExpandedSubBlockStart.gif        {
InBlock.gif             return GetValues( nullnull);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  long SetValueInt( string section,  string key,  int value)
ExpandedSubBlockStart.gif        {
InBlock.gif             return SetValue(section, key, value.ToString());
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  int GetValueInt( string section,  string key)
ExpandedSubBlockStart.gif        {
InBlock.gif             return GetPrivateProfileInt(section, key, -1, m_Path);
ExpandedSubBlockEnd.gif        }
InBlock.gif 
InBlock.gif         public  int SetSection( string strSection,  string strKeyValue)
ExpandedSubBlockStart.gif        {
InBlock.gif             return WritePrivateProfileSection(strSection, strKeyValue, m_Path);
ExpandedSubBlockEnd.gif        }
InBlock.gif
InBlock.gif         public  int DeleteSection( string strSection)
ExpandedSubBlockStart.gif        {
InBlock.gif             return SetSection(strSection,  null);
ExpandedSubBlockEnd.gif        }
ExpandedSubBlockEnd.gif    }
ExpandedBlockEnd.gif}

本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

目录
相关文章
|
23天前
|
开发框架 前端开发 .NET
C#编程与Web开发
【4月更文挑战第21天】本文探讨了C#在Web开发中的应用,包括使用ASP.NET框架、MVC模式、Web API和Entity Framework。C#作为.NET框架的主要语言,结合这些工具,能创建动态、高效的Web应用。实际案例涉及企业级应用、电子商务和社交媒体平台。尽管面临竞争和挑战,但C#在Web开发领域的前景将持续拓展。
|
23天前
|
SQL 开发框架 安全
C#编程与多线程处理
【4月更文挑战第21天】探索C#多线程处理,提升程序性能与响应性。了解C#中的Thread、Task类及Async/Await关键字,掌握线程同步与安全,实践并发计算、网络服务及UI优化。跟随未来发展趋势,利用C#打造高效应用。
|
23天前
|
存储 安全 网络安全
C#编程的安全性与加密技术
【4月更文挑战第21天】C#在.NET框架支持下,以其面向对象和高级特性成为安全软件开发的利器。本文探讨C#在安全加密领域的应用,包括使用System.Security.Cryptography库实现加密算法,利用SSL/TLS保障网络传输安全,进行身份验证,并强调编写安全代码的重要性。实际案例涵盖在线支付、企业应用和文件加密,展示了C#在应对安全挑战的同时,不断拓展其在该领域的潜力和未来前景。
|
23天前
|
人工智能 C# 开发者
C#编程中的图形界面设计
【4月更文挑战第21天】本文探讨了C#在GUI设计中的应用,介绍了Windows Forms、WPF和UWP等常用框架,强调了简洁界面、响应式设计和数据绑定等最佳实践。通过实际案例,展示了C#在企业应用、游戏开发和移动应用中的GUI实现。随着技术发展,C#在GUI设计的未来将趋向于跨平台、更丰富的组件和AI集成,为开发者创造更多可能性。
|
23天前
|
存储 算法 C#
C#编程与数据结构的结合
【4月更文挑战第21天】本文探讨了C#如何结合数据结构以构建高效软件,强调数据结构在C#中的重要性。C#作为面向对象的编程语言,提供内置数据结构如List、Array和Dictionary,同时也支持自定义数据结构。文章列举了C#实现数组、链表、栈、队列等基础数据结构的示例,并讨论了它们在排序、图算法和数据库访问等场景的应用。掌握C#数据结构有助于编写高性能、可维护的代码。
|
23天前
|
开发框架 Linux C#
C#编程的跨平台应用
【4月更文挑战第21天】C#与.NET Core的结合使得跨平台应用开发变得高效便捷,提供统一编程模型和高性能。丰富的类库、活跃的社区支持及Visual Studio Code、Xamarin等工具强化了其优势。广泛应用在企业系统、云服务和游戏开发中,虽面临挑战,但随着技术进步,C#在跨平台开发领域的前景广阔。
|
23天前
|
人工智能 C# 云计算
C#编程的未来发展趋向
【4月更文挑战第21天】C#编程未来将深化跨平台支持,强化云计算与容器技术集成,如.NET Core、Docker。在AI和ML领域,C#将提供更丰富框架,与AI芯片集成。语言和工具将持续创新,优化异步编程,如Task、async和await,提升多核性能。开源生态的壮大将吸引更多开发者,共创更多机遇。
|
23天前
|
程序员 C#
C#编程中的面向对象编程思想
【4月更文挑战第21天】本文探讨了C#中的面向对象编程,包括类、对象、封装、继承和多态。类是对象的抽象,定义属性和行为;对象是类的实例。封装隐藏内部细节,只暴露必要接口。继承允许类复用和扩展属性与行为,而多态使不同类的对象能通过相同接口调用方法。C#通过访问修饰符实现封装,使用虚方法和抽象方法实现多态。理解并应用这些概念,能提升代码的清晰度和可扩展性,助你成为更好的C#程序员。
|
23天前
|
开发框架 安全 .NET
C#编程高手的成长之路
【4月更文挑战第21天】本文揭示了成为C#编程高手的路径:牢固掌握基础知识和面向对象编程,深入了解C#特性如泛型和委托,精通ASP.NET等框架工具,养成良好编程习惯,持续学习实践并参与开源项目,勇于挑战创新。通过这些步骤,不断提升编程技能,迈向C#编程的巅峰。