删除ini文件的部分及其数据VB.NET/C# [英] Delete an ini file's section and its data VB.NET/C#

查看:332
本文介绍了删除ini文件的部分及其数据VB.NET/C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在VB.NET中删除ini文件的部分及其数据?

实际上,我只能删除数据但不能删除该部分的标题:



How can I delete an ini file's section and its data in VB.NET?
Actually, I can delete only data but not the title of the section:

Private Declare Ansi Function WritePrivateProfileString _
Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Integer

推荐答案

algoram你可以使用我的库用于在C#或VB.NET中处理INI文件 [ ^ ],它可以让你使用文件的部分及其键,就像使用.NET中的任何其他集合一样,例如:

algoram you can use my library for INI file's processing in C# or VB.NET[^], it can enable you to work with file's sections and their keys just like with any other collection in .NET, for example like this:
Dim ini As New IniFile()
ini.Load("path to a .ini file")

' Remove section, with a specified name, together with its keys.
ini.Sections.Remove("section name")
' Remove section, on specified location, together with its keys.
ini.Sections.RemoveAt(0)


您可以使用与此相同的功能删除该部分:

You can use the same function like this to delete the section:
Private Declare Ansi Function DeletePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
    ByVal Section As String, _
    ByVal NoKey As Integer, _
    ByVal NoSetting As Integer, _
    ByVal FileName As String) _
As Integer

Public Sub EraseINISection(ByVal INIFile As String, ByVal Section As String)
    DeletePrivateProfileSection(Section, 0, 0, INIFile)
End Sub


说实话,你不应该再使用INI文件了 - 还有更现代的(更友好!处理存储此类数据的方法。



Windows不提供删除INI文件部分(或实际上,任何其他信息)的任何方法,因此您唯一的解决方案是将整个文件复制到该部分,跳过它,然后将其余部分复制到一个新文件中。重命名原文,然后重命名新文件。



说真的:看一些更现代的东西!
To be honest, you shouldn't be using INI files any more - there are much more modern (and more friendly!) ways to deal with storing such data.

Windows doesn't provide any method for deleting INI file sections (or indeed, any other information) so your only solution would be to copy the whole file up to the section, skip it, and copy the rest into a new file. Rename the original, then rename the new file.

Seriously: look at something a bit more modern!


这篇关于删除ini文件的部分及其数据VB.NET/C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆