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

标签 .net powershell utf-8

我正在尝试使用 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

  • 我无法在 MSDN MethodInvocationException 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/

相关文章:

c# - 使用 RNGCryptoServiceProvider 生成随机字符串

.Net Core 项目中的 .Net Framework Nuget 包

arrays - Powershell 从包含大量字符串的大文本文件中删除任何行

jsp - 字符编码 JSP - 在 JSP 中显示错误但在 URL 中不显示 : "á » á é » é"

string - PHPStorm 中的复制和粘贴字符串(波斯语、阿拉伯语)是错误的

pandas - 使用 pandas 读取 csv 文件时,utf-8 和 latin-1 将不起作用

.net - Silverlight 和 WPF 兼容性

.net - 将我的 asp.net mvc web 应用程序发布到 IIS 后,无法上传我的内容文件夹中的文件

powershell - 在PowerShell中 `-Contains`和 `-In`有什么区别?

powershell - 从 PowerShell 脚本创建可执行的 .exe 文件?