2010-03-11 50 views
4

我使用了一個組件(System.ComponentModel.Component),並且我想要獲取我的項目的應用程序路徑以便在其中創建一個文件。如何在設計時獲得項目路徑

THX

弗洛裏安

+0

你在使用Windows窗體還是wpf? – Terry 2010-03-11 14:59:40

回答

0

使用AppDomain.CurrentDomain.BaseDirectory。

+1

它不起作用。當我拖動我的具有此方法的組件時: public override void InitializeNewComponent(IDictionary defaultValues) { MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory); } 該消息框出現:C:\ Program Files(x86)\ Microsoft Visual Studio 8 \ Common7 \ IDE \ – Florian 2010-03-11 15:23:00

+0

我建議你用VS測試應用程序嗎? – sashaeve 2010-03-11 15:45:40

0

這是行不通的?

New Uri(Assembly.GetCallingAssembly().CodeBase).AbsolutePath 

(「CallingAssembly」,因爲你可以把方法來檢索執行路徑爲服務層(組裝))

+2

它不起作用:這裏的結果 C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll – Florian 2010-03-11 15:25:02

0

我認爲羅伯特幾乎是正確的。

這似乎工作:這似乎工作(始終)對我來說是獲得EnvDTE.DTE(從IServiceProvider的你的EditValue()獲得)

Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) 
+2

這在設計時返回臨時VS路徑 – 2013-09-25 05:18:58

3

的唯一的事情,即:

EnvDTE.DTE dte = envProvider.GetService(typeof(EnvDTE.DTE)) as EnvDTE.DTE; 
string dir = Path.GetDirectoryName(dte.Solution.FullName); 

當我嘗試使用Assembly.GetXAssembly時,我得到了VS在設計時使用的臨時路徑。

+0

我相信你是對的。但只是可以得到這個在VB中工作。 – GameAlchemist 2011-11-19 14:38:01

0
string solutionpath = Directory.GetParent(Application.ExecutablePath).Parent.Parent.Parent.FullName; 

我覺得這是最好的解決辦法,因爲你沒有添加比「使用System.IO」任何庫更多:)

0

我做了一個簡單的測試(只是作爲一個臨時的設計,我添加一個字符串屬性來保存當前目錄值)(我沒有描述數據綁定的所有過程。請參閱我的一些關於設計時間/運行時綁定的文章)

在主的空構造函數窗口ViewModel類(專用於設計時間綁定)

public MainWindow_ViewModel():this(null) 
{ 
    dtpath = Directory.GetCurrentDirectory(); 
    Console.WriteLine("CTor finished"); 
} 

在mainwindow.xaml文件我添加一個文本框來顯示結果(不介意網格行值)

<TextBox Text="{Binding dtpath}" Grid.Row="3"/> 

而且我在VS(設計視圖像弗洛裏安的評論得到了但在4年之後具有更新的值): C:\ Program Files(x86)\ Microsoft Visual Studio 14。0 \ Common7 \ IDE

enter image description here

0

只需撥打GetMyPath這是如果你談論的是WPF設計定義爲

string GetMyPath([CallerFilePath] string from = null) 
{ 
    return from; 
} 
0

,請使用 「上下文」 屬性/類型

詳細信息: - 在設計時你有modelItem的實例(我假設它,你知道它),如果不是,那麼你可以在Override implementationatio中實例化它激活方法正

//在DesignAdorner類

public class DesignAdorner : PrimarySelectionAdornerProvider 
{ 
     protected override void Activate(ModelItem item) 
     { 
       modelItem = item; 
     } 
} 

現在你可以使用下面的一行代碼訪問當前應用程序的路徑

string aplicationPathDir = System.IO.Directory.GetParent(modelItem.Context.ToString()).FullName; 

讓我知道,如果它不能幫助你。

相關問題