获取Silverlight ClientBin目录的路径

时间:2012-03-30 11:43:13

标签: c# asp.net silverlight silverlight-4.0

我正在尝试从Silverlight应用程序内的.Web项目中获取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;
}

2 个答案:

答案 0 :(得分:3)

在Web项目中,您应该使用HostingEnvironment类的静态方法MapPath

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

该类位于程序集System.Web

的System.Web.Hosting命名空间中

答案 1 :(得分:0)

我知道它已经老了但是你走了:

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

它适用于浏览器应用程序。我没有在浏览器中测试它。