INI文件讀寫操作及保存窗體位置

本文介绍了一种使用C#进行Ini文件读写的方法,并提供了一个通用的Ini文件操作类。该类封装了读取字符串、读取整数、写入字符串、写入整数等常用功能。此外,还通过实例演示了如何利用此类来保存窗体的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.Ini文件的操作主要調用了以下三個API函數:

// 寫ini文件
[ DllImport (  " kernel32 "  ) ]
  
private   static   extern   bool  WritePrivateProfileString (  string  section , string  key ,  string  val ,  string  filePath ) ;

  
// 讀ini文件->字符
  [ DllImport (  " kernel32 "  ) ]
  
private   static   extern   int  GetPrivateProfileString (  string  section , string  key ,  string  def , StringBuilder retVal , int  size ,  string  filePath ) ;

  
// 讀ini文件->數字
  [ DllImport (  " kernel32 "  ) ]
  
private   static   extern   int  GetPrivateProfileInt (  string  section , string  key ,  int  def ,  string  filePath ) ;

2.以下是利用上面的API函數寫成的通用的類.

using  System;
using  System.IO;
using  System.Runtime.InteropServices;
using  System.Text;

namespace  Ini
{

  
public class IniFile
  
{

  
private string FFileName;

        
//寫ini文件
  [ DllImport ( "kernel32" ) ]
  
private static extern bool WritePrivateProfileString ( string section ,string key , string val , string filePath ) ;

  
//讀ini文件->字符
  [ DllImport ( "kernel32" ) ]
  
private static extern int GetPrivateProfileString ( string section ,string key , string def , StringBuilder retVal ,int size , string filePath ) ;

  
//讀ini文件->數字
  [ DllImport ( "kernel32" ) ]
  
private static extern int GetPrivateProfileInt ( string section ,string key , int def , string filePath ) ;

      
/// <param name="filename">文件路徑加完整文件名</param>
  public IniFile(string filename)
  
{
      FFileName 
= filename;
  }


      
/// <param name="def">默認值</param>
  public int ReadInt(string section,string key,int def)
  
{
      
return GetPrivateProfileInt(section,key,def,FFileName);
  }


      
/// <param name="def">默認值</param>
  public string ReadString(string section,string key,string def)
  
{
      StringBuilder temp 
= new StringBuilder(1024);
      GetPrivateProfileString(section,key,def,temp,
1024,FFileName);
      
return temp.ToString();
   }


  
public void WriteInt(string section,string key,int val)
  
{        
      WritePrivateProfileString(section,key,val.ToString(),FFileName);
  }


  
public void WriteString(string section,string key,string val)
  
{
      WritePrivateProfileString(section,key,val,FFileName);
  }


      
  
public void DelKey(string section,string key)
  
{
      WritePrivateProfileString(section,key,
null,FFileName);
  }


  
public void DelSection(string section)
  
{
      WritePrivateProfileString(section,
null,null,FFileName);
  }


  }


}

3.以下是一個實例,該實例主要用於保存窗體位置到Initest.ini中.

using Ini;//導入引用

 以下是代碼:

private   void  Form1_Load( object  sender, System.EventArgs e)
        
{
            
//窗體啟動時讀取INI文件
            if(File.Exists(Application.StartupPath+"/Initest.ini"))
            
{
                IniFile ini 
= new IniFile(Application.StartupPath + "/Initest.ini");
                Point p 
= new Point();
                p.X
=ini.ReadInt("Location","X",0 ); 
                p.Y
=ini.ReadInt("Location" ,"Y",0);
                
                
this.Location = p;
                
this.TextBoxX.Text = this.Location.X.ToString();
                
this.TextBoxY.Text = this.Location.Y.ToString();
            }


        }


        
private   void  Form1_Closing( object  sender, System.ComponentModel.CancelEventArgs e)
        
{
            
//窗體關閉時寫INI文件

            IniFile ini 
= new IniFile(Application.StartupPath + "/Initest.ini");
            ini.WriteInt(
"Location","X" ,this.Location.X);
            ini.WriteInt(
"Location","Y",this.Location.Y);
            ini.WriteString(
"TextBoxX","Text",this.TextBoxX.Text);
            ini.WriteString(
"TextBoxY","Text",this.TextBoxY.Text);
        }

附:Initest.ini文件內容如下,該文件路徑與程序啟動路徑相同.

[Location]
X=129
Y=191
[TextBoxX]
Text=129
[TextBoxY]
Text=191

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值