【问题标题】:Get file path from Visual Studio editor从 Visual Studio 编辑器获取文件路径
【发布时间】:2011-06-01 17:45:24
【问题描述】:

我正在开发一个用 C# 编写的 Visual Studio 包。

如何以编程方式获取活动编辑器的完整路径?

【问题讨论】:

    标签: c# visual-studio vs-extensibility vspackage


    【解决方案1】:

    这是您在 Visual Studio 中获取焦点(活动)文档的完整路径的方式:

    DTE dte = (DTE)GetService(typeof(DTE));
    string document = dte.ActiveDocument.FullName;
    

    【讨论】:

      【解决方案2】:

      我在开发 ASP.NET Web 窗体自定义服务器控件时遇到了类似的问题。 为了获得对 DTE 对象的引用并创建正在编辑的文件目录的虚拟路径,我在自定义服务器控制文件中使用了以下代码:

          [Bindable(true)]
          [Category("Behavior")]
          [DefaultValue("")]
          [Editor(typeof(System.Web.UI.Design.UrlEditor), typeof(System.Drawing.Design.UITypeEditor))]
          public string Url
          {
              get
              { 
                  object urlObject = ViewState["Url"];
                  if (urlObject == null)
                  {
                      if (DesignMode)
                      { 
                          // Get a reference to the Visual Studio IDE
                          EnvDTE.DTE dte = this.Site.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE;
      
                          // Interface for accessing the web application in VS
                          IWebApplication webApplication = (IWebApplication)this.Site.GetService(typeof(IWebApplication));
      
                          // Path of document being edited (Web form in web application)
                          string activeDocumentPath = dte.ActiveDocument.Path;
      
                          // Physical path to the web application root
                          string projectPath = webApplication.RootProjectItem.PhysicalPath;
      
                          // Create virtal path
                          string relativePath = activeDocumentPath.Replace(projectPath, "~\\");
      
                          return relativePath.Replace('\\','/');
                      }
                      else
                      {
                          return String.Empty;
                      }
                  }
                  else
                  {
                      return (string)urlObject;
                  }
              }
              set
              {
                  ViewState["Url"] = value;
              }
          }
      

      在使用 UrlEditor 时快速导航到正在编辑的文件附近的文件很有用

      【讨论】:

        【解决方案3】:

        在使用宏时,您可以使用

        DTE.ActiveDocument.Path + DTE.ActiveDocument.Name
        

        获取完整路径。在制作 VS 包时,C# 中可能也是这样吗?

        【讨论】:

          【解决方案4】:

          在 VS 2010 和 2008 中,右键单击顶部的选项卡并从上下文菜单中选择“复制完整路径”。请看我下面的图片。

          【讨论】:

          • 我从问题中假设 OP 以编程方式表示。
          • 我认为这不是 OP 想要的。
          • 我通常不希望能够投票以将 答案 移至 stackexchange 姐妹网站之一,但我现在愿意。
          猜你喜欢
          • 2015-09-18
          • 1970-01-01
          • 2017-08-08
          • 1970-01-01
          • 1970-01-01
          • 2011-12-12
          • 2014-03-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多