C# で記述された Visual Studio パッケージを開発しています。
アクティブなエディターの完全なパスをプログラムで取得するにはどうすればよいですか?
C# で記述された Visual Studio パッケージを開発しています。
アクティブなエディターの完全なパスをプログラムで取得するにはどうすればよいですか?
マクロを使用するときに使用できます
DTE.ActiveDocument.Path + DTE.ActiveDocument.Name
フルパスを取得します。VS パッケージを作成するとき、これは C# でも同じでしょうか?
これは、Visual Studio でフォーカスされた (アクティブな) ドキュメントの完全なパスを取得する方法です。
DTE dte = (DTE)GetService(typeof(DTE));
string document = dte.ActiveDocument.FullName;
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 を使用するときに、編集中のファイルの近くにあるファイルにすばやく移動するのに役立ちます。
VS 2010 および 2008 では、上部のタブを右クリックし、コンテキスト メニューから [フル パスのコピー] を選択します。以下の私の画像を参照してください。