【问题标题】:Deleting INI section using Powershell and kernel32.dll使用 Powershell 和 kernel32.dll 删除 INI 部分
【发布时间】:2015-04-18 08:23:28
【问题描述】:

我在使用 Powershell 删除 INI 文件部分时遇到问题。我在 kernel32.dll 中调用 WritePrivateProfileString,其中键和值为空字符串。以下是“之前”ini 的示例:

[Section1]
Setting1=Value1
[Section2]
Setting2=Value2

现在我调用 WritePrivateProfileString,使用导入的 kernel32.dll(完整代码如下):

$Kernel32::WritePrivateProfileString("Section2", "", "", "MyIniFile.ini")

我希望这会删除第 2 节,但我得到了这个:

[Section1]
Setting1=Value1
[Section2]
Setting2=Value2
=

显然空字符串并没有被底层代码识别出来。空字符串的定义可能有所不同?任何帮助,将不胜感激。下面是定义 $Kernel32 的代码:

$Signature = @’ 
[DllImport("kernel32.dll")] 
public static extern bool WritePrivateProfileString(
    string lpAppName, 
    string lpKeyName, 
    string lpString, 
    string lpFileName); 
‘@ 

$Kernel32 = Add-Type -MemberDefinition $Signature -Name Win32Utils -Namespace WritePrivateProfileString -Using System.Text -PassThru 
$Kernel32::WritePrivateProfileString($Section, $Key, $Value, $File)

【问题讨论】:

    标签: powershell ini kernel32


    【解决方案1】:

    使用[NullString]::Value。来源:Possible to pass null from Powershell to a .Net API that expects a string?

    例子:

    $Kernel32::WritePrivateProfileString("Section2", [NullString]::Value, [NullString]::Value, "MyIniFile.ini")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-11
      • 2019-01-26
      • 2012-09-02
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-12
      相关资源
      最近更新 更多