简体   繁体   English

在Visual Studio(2010)中以编程方式打开文件

[英]Programmatically open a file in Visual Studio (2010)

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...). 我正在构建一个VS包,并且试图将命令从包发送到Visual Studio,以在新选项卡中打开用户选择的文件(就像用户通过转到File-> Open一样。 ..)。

I remember seeing at some point how to do this. 我记得在某个时候看到过该怎么做。 Can anybody refresh my memory? 谁能刷新我的记忆?

I believe you want one of: 我相信您想要以下之一:

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

In the end, I think they all boil down to the same behavior. 最后,我认为他们都归结为相同的行为。

I like to use the DTE method ExecuteCommand("commandName") as you can test the command in the VS Command Window 我喜欢使用DTE方法ExecuteCommand(“ commandName”),因为您可以在VS Command Window中测试命令

In this case ExecuteCommand("File.OpenFile") 在这种情况下ExecuteCommand(“ File.OpenFile”)

You can add parameters to the command in a second optional string parameter if you wish. 您可以根据需要在第二个可选字符串参数中向命令添加参数。

You can use this function: VsShellUtilities.OpenDocument 您可以使用此功能: VsShellUtilities.OpenDocument

If you call it from VSPackage method it looks like: 如果从VSPackage方法调用它,则它看起来像:

VsShellUtilities.OpenDocument(this, fileName);

Namespace: Microsoft.VisualStudio.Shell 命名空间: Microsoft.VisualStudio.Shell

Assemblies: Microsoft.VisualStudio.Shell.15.0.dll , Microsoft.VisualStudio.Shell.14.0.dll 程序集: Microsoft.VisualStudio.Shell.15.0.dllMicrosoft.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. 注意:此功能在VS 2015及更高版本中可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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