简体   繁体   中英

use PowerShell to save file in Visual Studio

Using Powershell from Package Manager Console I can open and close file :

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

But when I try to save it:

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

or

$DTE.ExecuteCommand(“File.Save”)

I get error:

PM> $DTE.ExecuteCommand(“File.Save”) Command "File.Save" is not valid. At line:1 char:1 + $DTE.ExecuteCommand(“File.Save”) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

How I can save it?

Actually I want to save it in other encoding:

在此处输入图片说明

You can save the file with

$DTE.ExecuteCommand("File.SaveSelectedItems")

There is also

$DTE.ExecuteCommand("File.SaveAll")

If you're doing this only to change the file's encoding however, I would suggest simply running the following PowerShell code that doesn't require $DTE and Visual Studio at all.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM