[英]How to delete a row in one section of INI file using vb.net?
I am trying to delete a row inside the section of an INI file, but after triggering the delete code, The INI file contents stays the same.我正在尝试删除 INI 文件部分内的一行,但在触发删除代码后,INI 文件内容保持不变。 The deleted data of the INI File does not appear to be deleted. INI 文件的已删除数据似乎没有被删除。
CODE:代码:
<DllImport("kernel32.dll", SetLastError:=True)> _
Public Shared Function WritePrivateProfileSection( _
ByVal lpAppName As String, _
ByVal lpString As String, _
ByVal lpValue As String, _
ByVal lpFileName As String) As Boolean
End Function
Public Sub DeleteSection(ByRef sApp As String, ByRef sKey As String)
Try
Dim ret As Boolean
Dim sBuff As New System.Text.StringBuilder
sBuff.Capacity = 256
ret = WritePrivateProfileSection(sApp, sKey, "", m_sIniFile)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
USAGE:用法:
m_ini.DeleteSection("AppName", "AppKey")
The main purpose of this is to Delete the key and its value.这样做的主要目的是删除键及其值。
But the problem is after deleting it, nothing has changed on the INI File.但问题是删除它后,INI 文件没有任何变化。
Regards,问候,
EDIT:编辑:
As pointed out by Jimi in the comments, WritePrivateProfileString
can delete a key.正如 Jimi 在评论中指出的那样, WritePrivateProfileString
可以删除一个键。 It appears that you should indeed be calling WritePrivateProfileString
rather than WritePrivateProfileSection
and the third argument should be Nothing
rather than an empty String
.看来您确实应该调用WritePrivateProfileString
而不是WritePrivateProfileSection
并且第三个参数应该是Nothing
而不是空String
。
Also, while the actual names don't matter when executing, it's a good idea to be consistent in your code to avoid confusion.此外,虽然实际名称在执行时并不重要,但最好在代码中保持一致以避免混淆。 As such, the parameters, in order, should be lpAppName
, lpKeyName
, lpString
and lpFileName
.因此,按顺序,参数应该是lpAppName
、 lpKeyName
、 lpString
和lpFileName
。 If you want to use .NET-style naming instead, use appName
, keyName
, [string]
and fileName
.如果您想改用 .NET 风格的命名,请使用appName
、 keyName
、 [string]
和fileName
。 You might use value
instead of [string]
to avoid the need to escape a keyword.您可以使用value
而不是[string]
来避免转义关键字。
ORIGINAL:原来的:
I've never used either so I'm not completely confident in this answer but, from what I can see, you are mangling WritePrivateProfileSection
and WritePrivateProfileString
into a combination that doesn't do either.我从来没有使用过,所以我对这个答案并不完全有信心,但是,据我所见,你正在将WritePrivateProfileSection
和WritePrivateProfileString
组合成一个也不起作用的组合。 Your function is named WritePrivateProfileSection
but that function has three parameters, not four.您的 function 被命名为WritePrivateProfileSection
但 function 有三个参数,而不是四个。 It is WritePrivateProfileString
that has four but that doesn't do what you want.是WritePrivateProfileString
有四个,但不能满足您的要求。
WritePrivateProfileString
will write a value to a key, which is fine if you want to clear that key but if you want to delete it, it's not right. WritePrivateProfileString
会将一个值写入一个键,如果您想清除该键,这很好,但如果您想删除它,那就不对了。 WritePrivateProfileSection
takes a single String
containing all the keys and values to write to a section so that's what you need to use and you need to omit the key you want deleted from that String
. WritePrivateProfileSection
采用一个包含所有键和值的单个String
来写入一个部分,因此这是您需要使用的,并且您需要省略要从该String
中删除的键。
It appears that what you actually need to do is read all the keys and values in that section and then construct a String
from them, leaving out the key you want to delete.看来您实际需要做的是读取该部分中的所有键和值,然后从中构造一个String
,而忽略要删除的键。 You then call WritePrivateProfileSection
with that String
to overwrite the section with the modified data.然后,您使用该String
调用WritePrivateProfileSection
以使用修改后的数据覆盖该部分。 The GetPrivateProfileSection
function will get a String
containing all the current keys and values, so you should call that, process the result and then pass the manipulated data to WritePrivateProfileSection
. GetPrivateProfileSection
function 将获得一个包含所有当前键和值的String
,因此您应该调用它,处理结果,然后将操作数据传递给WritePrivateProfileSection
。
I also recommend using WritePrivateProfileString, but you should declare it correct.我还建议使用 WritePrivateProfileString,但您应该声明它是正确的。
' Writes String values
<DllImport("Kernel32.dll", CharSet:=CharSet.Unicode,
SetLastError:=True, EntryPoint:="WritePrivateProfileStringW")>
Public Shared Function WritePrivateProfileString(
Section As String,
Key As String,
WriteStr As String,
IniFile As String) As Integer
End Function
In .NET the standard is Unicode, so use the EntryPoint with the W ending (= wide)在 .NET 中,标准是 Unicode,所以使用带有 W 结尾(= 宽)的 EntryPoint
Then deleting of a Key/Entry is easy:然后删除键/条目很容易:
Public Sub DeleteEntry(IniFile As String, Section As String, Key As String)
WritePrivateProfileString(Section, Key, vbNullString, IniFile)
End Sub
You could also delete a complete section with WritePrivateProfileString:您还可以使用 WritePrivateProfileString 删除完整的部分:
Public Sub DeleteSection(IniFile As String, Section As String)
WritePrivateProfileString(Section, vbNullString, vbNullString, IniFile)
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.