2012-03-30 133 views
2

我試圖從Silverlight應用程序中的.Web項目中獲取ClientBin目錄的路徑。目前,我的方法是這樣的,但必須有一個更好/更簡潔的方式來獲得這個路徑:獲取Silverlight ClientBin目錄的路徑

public static string GetClientBinPath() 
{ 
    var applicationPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase); 

    if (applicationPath.StartsWith(@"file:\")) 
     applicationPath = applicationPath.Remove(0, 6); 

    applicationPath = Path.Combine(applicationPath, @"..\ClientBin"); 

    return applicationPath; 
} 

回答

3

在你應該使用HostingEnvironment類的靜態方法MapPath Web項目。

string pathToClientBin = HostingEnvironment.MapPath("~/ClientBin"); 

類位於組裝的System.Web的命名空間System.Web.Hosting

+0

不錯!謝謝你的快速反應。我知道必須有一種更簡單的方法將虛擬空間「映射」到物理空間。 – 2012-03-30 13:29:37

0

我知道這是舊的,但在這裏你去:

currentDir = Path.GetDirectoryName(Application.Current.Host.Source.LocalPath); 

它適用於出的瀏覽器應用程序。我沒有在瀏覽器中測試它。