代码之家  ›  专栏  ›  技术社区  ›  Alexan

使用PowerShell在Visual Studio中保存文件

  •  1
  • Alexan  · 技术社区  · 7 年前

    open and close file

    $DTE.ExecuteCommand(“File.OpenFile”, $file)
    $DTE.ExecuteCommand(“File.Close”)
    

    但当我试图保存它时:

    $DTE.ExecuteCommand(“File.Save”, $file)
    

    或

    $DTE.ExecuteCommand(“File.Save”)
    

    我得到一个错误:

    第1行字符:1 +FullyQualifiedErrorId:System.Runtime.InteropServices.COMException

    我怎样才能拯救它?

    实际上我想用其他编码保存它:

    enter image description here

    1 回复  |  直到 7 年前
        1
  •  1
  •   dee-see Tom Ritsema    7 年前

    $DTE.ExecuteCommand("File.SaveSelectedItems")
    

    还有

    $DTE.ExecuteCommand("File.SaveAll")
    

    但是,如果您这样做只是为了更改文件的编码,我建议您只需运行以下不需要 $DTE 还有VisualStudio。

    $content = Get-Content -Path $file -Encoding String
    Set-Content -Value $content -Path $file -Encoding UTF8