4

C# で記述された Visual Studio パッケージを開発しています。

アクティブなエディターの完全なパスをプログラムで取得するにはどうすればよいですか?

4

4 に答える 4

3

マクロを使用するときに使用できます

DTE.ActiveDocument.Path + DTE.ActiveDocument.Name

フルパスを取得します。VS パッケージを作成するとき、これは C# でも同じでしょうか?

于 2010-12-30T20:46:55.247 に答える
3

これは、Visual Studio でフォーカスされた (アクティブな) ドキュメントの完全なパスを取得する方法です。

DTE dte = (DTE)GetService(typeof(DTE));
string document = dte.ActiveDocument.FullName;
于 2015-06-12T16:54:19.647 に答える
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 を使用するときに、編集中のファイルの近くにあるファイルにすばやく移動するのに役立ちます。

于 2011-01-26T17:34:51.440 に答える
0

VS 2010 および 2008 では、上部のタブを右クリックし、コンテキスト メニューから [フル パスのコピー] を選択します。以下の私の画像を参照してください。 代替テキスト

于 2010-12-30T20:23:46.577 に答える