简体   繁体   English

从Visual Studio编辑器获取文件路径

[英]Get file path from Visual Studio editor

I'm developing a Visual Studio Package, written in C#. 我正在开发一个用C#编写的Visual Studio包。

How do I get the full path of the active editor programatically? 如何以编程方式获取活动编辑器的完整路径?

This is how you get the full path of the focused (active) document in Visual Studio: 这是您在Visual Studio中获取焦点(活动)文档的完整路径的方法:

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

When working macros you can use 在使用宏时,您可以使用

DTE.ActiveDocument.Path + DTE.ActiveDocument.Name

to get the full path. 获得完整的路径。 Likely this is the same in C# when making VS packages? 在制作VS包时,C#中的可能性是一样的吗?

I had a similar problem when developing ASP.NET Web Forms custom server controls. 在开发ASP.NET Web Forms自定义服务器控件时遇到了类似的问题。 In order to obtain a reference to the DTE object and create a virtual path to the directory of the file being edited I used the following code inside my custom server control file: 为了获得对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;
        }
    }

This is useful quickly navigating to a file near the one being edited when using the UrlEditor 使用UrlEditor时,这可以快速导航到正在编辑的文件附近的文件

In VS 2010 and 2008, you right click the tab at the top and select "Copy Full Path" from the context menu. 在VS 2010和2008中,右键单击顶部的选项卡,然后从上下文菜单中选择“复制完整路径”。 See my image below. 请参阅下面的图片。 替代文字

暂无
暂无

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

相关问题 如何在 visual studio 中获取文件的相对路径? - How to get relative path of a file in visual studio? 在Visual Studio文本编辑器扩展中获取当前的源文件方法 - Get current source file methods in Visual Studio Text Editor Extension 为什么从 Visual Studio Code 编辑器调试时出现错误“文件不包含有效的 CIL 图像”? - Why do I get error 'File does not contain a valid CIL image' when debugging from Visual Studio Code editor? 尝试从编辑器上的右键菜单选项“ Visual Studio 2015加载项:SDK,API…”中获取文件名和行号。 - Trying to get file name and line number from editor on right click menu option Visual Studio 2015 Add-In: SDK, API… 如何在visual studio 2013解决方案资源管理器中获取文件路径? - How to get a file path in visual studio 2013 solution explorer? 创建Visual Studio菜单命令以获取当前的活动文件路径 - Creating a Visual Studio menu command to get current active file path 如何在 Visual Studio 项目中获取或设置动态绝对文件路径? - How to get or setup dynamic absolute File Path in Visual Studio Project? Visual Studio 文件夹文件路径 - Visual Studio Folder file path 如何从VSPackage获取当前运行的Visual Studio安装路径 - How get the current running Visual Studio installation path from VSPackage 如何从Visual Studio 2012获取已安装应用程序的文件夹路径 - How to get folder path of Installed application, from Visual Studio 2012
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM