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

发布于 2024-10-10 09:42:39 字数 68 浏览 0 评论 0原文

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

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

I'm developing a Visual Studio Package, written in C#.

How do I get the full path of the active editor programatically?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

悲喜皆因你 2024-10-17 09:42:39

使用宏时,您可以使用它

DTE.ActiveDocument.Path + DTE.ActiveDocument.Name

来获取完整路径。在 C# 中制作 VS 包时这可能是相同的吗?

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?

溺深海 2024-10-17 09:42:39

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

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

This is how you get the full path of the focused (active) document in Visual Studio:

DTE dte = (DTE)GetService(typeof(DTE));
string document = dte.ActiveDocument.FullName;
太阳公公是暖光 2024-10-17 09:42:39

我在开发 ASP.NET Web Forms 自定义服务器控件时遇到了类似的问题。
为了获取对 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;
        }
    }

这对于在使用时快速导航到正在编辑的文件附近的文件非常有用网址编辑器

I had a similar problem when developing ASP.NET Web Forms custom server controls.
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:

    [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

离旧人 2024-10-17 09:42:39

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

In VS 2010 and 2008, you right click the tab at the top and select "Copy Full Path" from the context menu. See my image below. alt text

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文