gpt4 book ai didi

.net - Powershell – WriteAllLines 方法 – ‘Access is Denied’

转载 作者:行者123 更新时间:2023-12-03 15:10:43 25 4
gpt4 key购买 nike

我正在尝试使用 technique outlined in another SO question 通过 PowerShell 将文件格式转换为 UTF-8 .

我正在使用以下语句:

[IO.File]::WriteAllLines((Get-Item -Path ".\" -Verbose).FullName,"Test.txt")

第一个参数为文件路径(C:\users\rsax\documents\Test),第二个参数为文件名。

但是,该命令不起作用,并返回以下错误:

Exception calling "WriteAllLines" with "2" argument(s): "Access to the path 'C:\users\rsax\documents\Test' is denied."
At line:1 char:25
+ [IO.File]::WriteAllLines <<<< ((Get-Item -Path ".\" -Verbose).FullName,"Test.txt")
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
  • 我正在以管理员身份从 PowerShell 运行命令。
  • 我已确认文件在文件夹中。
  • 我可以在访问该文件的目录中运行其他 cmdlet,例如如:

    获取内容测试.txt |输出文件TestOut.txt

  • 我无法在 MSDNMethodInvocationException page 上找到答案.

我做错了什么?

最佳答案

我不确定您是否理解您正在使用的过载。第一个参数应该是文件路径,而第二个参数是内容(不是文件名)。 (Get-Item -Path ".\"-Verbose).FullName 将是文件夹的路径,而不是文件的路径。此外,不需要 -Verbose 开关。

PS> [IO.File]::WriteAllLines.OverloadDefinitions
static void WriteAllLines(string path, string[] contents)

示例:

$content = Get-Content .\Test.txt
$outfile = Join-Path (Get-Item -Path ".\Test.txt").DirectoryName "TestOut.txt"
[IO.File]::WriteAllLines($outfile, $content)

关于.net - Powershell – WriteAllLines 方法 – ‘Access is Denied’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35517144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com