为了账号安全,请及时绑定邮箱和手机立即绑定

我想,既然有下面的两个函数,也一定有我所说的这种函数?

我想,既然有下面的两个函数,也一定有我所说的这种函数?

偶然的你 2023-04-22 18:14:19
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, _ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As LongPublic Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _(ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, _ByVal lpFileName As String) As LongPublic MainIniFile As String我用这套函数读写INI文件,可是怎么删除一个项,或者一个值?挨个将值设为空不好。那样太麻烦。最好可以一次删除一个项以及其下的所有值(例如[Settings]以及它下面的所有值)。还得能单独删除一个值(例如aaa=1这一行)就像DeleteSetting()函数一样,只不过是针对ini文件而已。
查看完整描述

2 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace IniFiles
{
public abstract class CustomIniFile
{
public CustomIniFile(string AFileName) 
{
FFileName = AFileName;
}
private string FFileName;
public string FileName
{
get { return FFileName; }
}
public virtual bool SectionExists(string Section)
{
List<string> vStrings = new List<string>();
ReadSections(vStrings);
return vStrings.Contains(Section);
}
public virtual bool ValueExists(string Section, string Ident)
{
List<string> vStrings = new List<string>();
ReadSection(Section, vStrings);
return vStrings.Contains(Ident);
}
public abstract string ReadString(string Section, string Ident, string Default);
public abstract bool WriteString(string Section, string Ident, string Value);
public abstract bool ReadSectionValues(string Section, List<string> Strings);
public abstract bool ReadSection(string Section, List<string> Strings);
public abstract bool ReadSections(List<string> Strings);
public abstract bool EraseSection(string Section);
public abstract bool DeleteKey(string Section, string Ident);
public abstract bool UpdateFile();
}

/// <summary>
/// 存储本地INI文件的类。
/// </summary>
public class IniFile : CustomIniFile
{
[DllImport("kernel32")]
private static extern uint GetPrivateProfileString(
string lpAppName, // points to section name 
string lpKeyName, // points to key name 
string lpDefault, // points to default string 
byte[] lpReturnedString, // points to destination buffer 
uint nSize, // size of destination buffer 
string lpFileName // points to initialization filename 
);

[DllImport("kernel32")]
private static extern bool WritePrivateProfileString(
string lpAppName, // pointer to section name 
string lpKeyName, // pointer to key name 
string lpString, // pointer to string to add 
string lpFileName // pointer to initialization filename 
);

/// <summary>
/// 构造IniFile实例。
/// <param name="AFileName">指定文件名</param>
/// </summary>
public IniFile(string AFileName)
: base(AFileName)
{
}

/// <summary>
/// 析够IniFile实例。
/// </summary>
~IniFile()
{
UpdateFile();
}

/// <summary>
/// 读取字符串值。
/// <param name="Ident">指定变量标识。</param>
/// <param name="Section">指定所在区域。</param>
/// <param name="Default">指定默认值。</param>
/// <returns>返回读取的字符串。如果读取失败则返回该值。</returns>
/// </summary>
public override string ReadString(string Section, string Ident, string Default)
{
byte[] vBuffer = new byte[2048];
uint vCount = GetPrivateProfileString(Section,
Ident, Default, vBuffer, (uint)vBuffer.Length, FileName);
return Encoding.Default.GetString(vBuffer, 0, (int)vCount);
}
/// <summary>
/// 写入字符串值。
/// </summary>
/// <param name="Section">指定所在区域。</param>
/// <param name="Ident">指定变量标识。</param>
/// <param name="Value">所要写入的变量值。</param>
/// <returns>返回写入是否成功。</returns>
public override bool WriteString(string Section, string Ident, string Value)
{
return WritePrivateProfileString(Section, Ident, Value, FileName);
}

/// <summary>
/// 获得区域的完整文本。(变量名=值格式)。
/// </summary>
/// <param name="Section">指定区域标识。</param>
/// <param name="Strings">输出处理结果。</param>
/// <returns>返回读取是否成功。</returns>
public override bool ReadSectionValues(string Section, List<string> Strings)
{
List<string> vIdentList = new List<string>();
if (!ReadSection(Section, vIdentList)) return false;
foreach (string vIdent in vIdentList)
Strings.Add(string.Format("{0}={1}", vIdent, ReadString(Section, vIdent, "")));
return true;
}

/// <summary>
/// 读取区域变量名列表。
/// </summary>
/// <param name="Section">指定区域名。</param>
/// <param name="Strings">指定输出列表。</param>
/// <returns>返回获取是否成功。</returns>
public override bool ReadSection(string Section, List<string> Strings)
{
byte[] vBuffer = new byte[16384];
uint vLength = GetPrivateProfileString(Section, null, null, vBuffer,
(uint)vBuffer.Length, FileName);
int j = 0;
for (int i = 0; i < vLength; i++)
if (vBuffer[i] == 0)
{
Strings.Add(Encoding.Default.GetString(vBuffer, j, i - j));
j = i + 1;
}
return true;
}

/// <summary>
/// 读取区域名列表。
/// </summary>
/// <param name="Strings">指定输出列表。</param>
/// <returns></returns>
public override bool ReadSections(List<string> Strings)
{
byte[] vBuffer = new byte[16384];
uint vLength = GetPrivateProfileString(null, null, null, vBuffer, 
(uint)vBuffer.Length, FileName);
int j = 0;
for (int i = 0; i < vLength; i++)
if (vBuffer[i] == 0)
{
Strings.Add(Encoding.Default.GetString(vBuffer, j, i - j));
j = i + 1;
}
return true;
}

/// <summary>
/// 删除指定区域。
/// </summary>
/// <param name="Section">指定区域名。</param>
/// <returns>返回删除是否成功。</returns>
public override bool EraseSection(string Section)
{
return WritePrivateProfileString(Section, null, null, FileName);
}

/// <summary>
/// 删除指定变量。
/// </summary>
/// <param name="Section">变量所在区域。</param>
/// <param name="Ident">变量标识。</param>
/// <returns>返回删除是否成功。</returns>
public override bool DeleteKey(string Section, string Ident)
{
return WritePrivateProfileString(Section, Ident, null, FileName);
}

/// <summary>
/// 更新文件。
/// </summary>
/// <returns>返回更新是否成功。</returns>
public override bool UpdateFile()
{
return WritePrivateProfileString(null, null, null, FileName);
}
}
}

EraseSection() // 删除一个区块
DeleteKey() // 删除一个键值

查看完整回答
反对 回复 2023-04-25
?
弑天下

TA贡献1818条经验 获得超8个赞

删除节点及节点中所有值
Public Sub EraseSection(ByVal Section As String, ByVal FileName As String)
WritePrivateProfileString Section, Null, Null, FileName
End Sub 

删除指定值Ident
WritePrivateProfileString(Section, Ident, null, FileName);

查看完整回答
反对 回复 2023-04-25
  • 2 回答
  • 0 关注
  • 160 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信