Programmatically open a file in Visual Studio (2010)

余生长醉 提交于 2021-02-20 08:32:12

问题


I'm building a VS package, and I'm trying to send a command from the package to Visual Studio to open up a user selected file in a new tab (just like a user would do it by going to File -> Open...).

I remember seeing at some point how to do this. Can anybody refresh my memory?


回答1:


I believe you want one of:

  1. IVsUIShellOpenDocument.OpenStandardEditor
  2. DTE.OpenFile
  3. DTE.ItemOperations.OpenFile

In the end, I think they all boil down to the same behavior.




回答2:


I like to use the DTE method ExecuteCommand("commandName") as you can test the command in the VS Command Window

In this case ExecuteCommand("File.OpenFile")

You can add parameters to the command in a second optional string parameter if you wish.




回答3:


You can use this function: VsShellUtilities.OpenDocument

If you call it from VSPackage method it looks like:

VsShellUtilities.OpenDocument(this, fileName);

Namespace: Microsoft.VisualStudio.Shell

Assemblies: Microsoft.VisualStudio.Shell.15.0.dll, Microsoft.VisualStudio.Shell.14.0.dll

Also it has two overloads which return additional information for opened document.

NOTE: This function is available in VS 2015 and later.



来源:https://stackoverflow.com/questions/2865909/programmatically-open-a-file-in-visual-studio-2010

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!