Use PowerShell to open a file

2019-02-26 23:15发布

问题:

Is there a command to open a project item (such as a class file) from Package Manager Console (a.k.a. PowerShell) within Visual Studio? That is the same thing that would happen when I double-click the file in Solution Explorer.

I tried using Invoke-Item but that happens to open a new instance of Visual Studio.

回答1:

$DTE.ExecuteCommand(“File.OpenFile”, "a path to your file")

You can refer to a local project file this way:

$path = [System.IO.Path]
$file = $path::Combine($path::GetDirectoryName($project.FileName), “your local path”)
$DTE.ExecuteCommand(“File.OpenFile”, $file)