C#操作INI文件

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

最近用到保存文件的相关东西,主要是封装两个函数,代码如下:可以直接使用

using System;

using System.Collections.Specialized;

using System.IO;

using System.Runtime.InteropServices;

using System.Text;

namespace IniFileTest

{

public class INI

{

const int COUNT = 0xFFFF;

[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[] buffer, int size, string filePath);

/// <summary>

/// ini关联的文件

/// </summary>

private string FileName;

public INI(string filename)

{

FileInfo file = new FileInfo(filename);

if (file.Exists)

{

FileName = new FileInfo(filename).FullName;

}

}

/// <summary>

/// 删除Section

/// </summary>

/// <param name="SectionName"></param>

/// <returns></returns>

public bool EraseSection(string SectionName)

{

return WritePrivateProfileString(SectionName, null, null, FileName);

}

/// <summary>

/// 写入section 键值对,如果没有section 则创建并写入

/// </summary>

/// <param name="section"></param>

/// <param name="key"></param>

/// <param name="value"></param>

/// <returns></returns>

public bool Write(string section, string key, string value)

{

return WritePrivateProfileString(section, key, value, FileName);

}

/// <summary>

/// 删除键

/// </summary>

/// <param name="section"></param>

/// <param name="key"></param>

/// <returns></returns>

public bool EraseKey(string section, string key)

{

return WritePrivateProfileString(section, key, null, FileName);

}

/// <summary>

/// 获取所有section

/// </summary>

/// <returns></returns>

public StringCollection GetSections()

{

StringCollection sections = new StringCollection();

byte[] buffer = new byte[COUNT];

int length = GetPrivateProfileString(null, null, null, buffer, COUNT, FileName);

int start = 0;

for (int i = 0; i < length; i++)

{

if (buffer[i] == 0 && i > start)

{

string str = Encoding.Default.GetString(buffer, start, i - start);

sections.Add(str);

start = i + 1; ;

}

}

return sections;

}

/// <summary>

/// 获取section下的所哟keys

/// </summary>

/// <param name="section"></param>

/// <returns></returns>

public StringCollection GetSectionKeys(string section)

{

StringCollection keys = new StringCollection();

byte[] buffer = new byte[COUNT];

int length = GetPrivateProfileString(section, null, null, buffer, COUNT, FileName);

int start = 0;

for (int i = 0; i < length; i++)

{

if (buffer[i] == 0 && i > start)

{

string str = Encoding.Default.GetString(buffer, start, i - start);

keys.Add(str);

start = i + 1; ;

}

}

return keys;

}

/// <summary>

/// 查寻指定的key的值,没有则返回defaultvalue

/// </summary>

/// <param name="section"></param>

/// <param name="key"></param>

/// <param name="defaultvalue"></param>

/// <returns></returns>

public String GetValue(String section, string key, string defaultvalue)

{

byte[] bytes = new byte[COUNT];

int s = GetPrivateProfileString(section, key, defaultvalue, bytes, COUNT, FileName);

string str = Encoding.GetEncoding(0).GetString(bytes, 0, s);

return defaultvalue;

}

}

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航