C#读写INI文件

INI文件格式

[StartUp]
StartKey
=StartValue

[sectionName]
KeyName
=KeyValue
操作
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Diagnostics;
using Microsoft.Win32;
using System.IO;
using System.Runtime.InteropServices;

namespace UseINI
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(GetPrivateProfileString(
"sectionName""KeyName"));
        }
        
/// <summary>
        
///  软件安装路径,以此exe所在的路径为准。
        
/// </summary>
        string installDirectory = null;
        
private string InstallDirectory
        {
            
get
            {
                
if (string.IsNullOrEmpty(installDirectory))
                {
                    
string sPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                    installDirectory 
= Path.GetDirectoryName(sPath) + @"\";
                }
                
return installDirectory;
            }
        }

        
private string startUpIniFileName = null;
        
public string StartUpIniFileName
        {
            
get
            {
                
if (string.IsNullOrEmpty(startUpIniFileName))
                {
                    startUpIniFileName 
= InstallDirectory + "StartUp.ini";
                }

                
return startUpIniFileName;
            }
        }

        [DllImport(
"kernel32")]
        
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        [DllImport(
"kernel32")]
        
private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);

        
/// <summary>
        
/// 从Ini文件获取数据
        
/// </summary>
        
/// <param name="section">应用程序</param>
        
/// <param name="key">键的名称</param>
        
/// <returns>键的值</returns>
        private string GetPrivateProfileString(string section, string key)
        {
            
int nCapacity = 255;
            StringBuilder temp 
= new StringBuilder(nCapacity);
            
int i = GetPrivateProfileString(section, key, "", temp, nCapacity, StartUpIniFileName);

            
if (i < 0)
                
return "";

            
return temp.ToString();
        }


        
/// <summary>
        
/// 向Ini文件中写入值
        
/// </summary>
        
/// <param name="section">应用程序</param>
        
/// <param name="key">键的名称</param>
        
/// <param name="value">键的值</param>
        
/// <returns>执行成功为True,失败为False。</returns>
        public long WritePrivateProfileString(string section, string key, string value)
        {
            
if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)
                
return 0;

            
return WritePrivateProfileString(section, key, value, StartUpIniFileName);
        }
    }
}

源码下载:http://revit.5d6d.com/thread-1044-1-1.html

posted @ 2011-05-26 12:55  大气象  阅读(4276)  评论(1编辑  收藏  举报
http://www.tianqiweiqi.com