【发布时间】:2010-10-10 18:39:19
【问题描述】:
我有一个 asp.net mvc 项目,它在一个单独的库中使用了一些搜索方法。 这个库需要知道我的 lucene 索引文件的位置。
private static string lucenePath = ConfigurationManager.AppSettings["lucenePath"];
public static ColorList SearchColors(Query query) {
return new ColorList(
new IndexSearcher(Path.GetFullPath(lucenePath)),
query);
}
这会从 web.config 的应用程序关键节点正确读取我配置的 lucenePath。 但是我怎样才能从这个相对路径中得到正确的完整路径呢? Path.GetFullPath 给了我一个完全错误的路径。
--结论--
如果您想全力以赴,tvanfosson 的答案可能适合您。
但是,我通过使用以下方法使其脑死亡更多:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
ConfigurationManager.AppSettings["luceneIndex"].TrimStart('\\'));
这将在调用者的 app.config 中查找名为“path”的 appkey,并将其值与调用者的路径结合起来。 TrimStart() 确保配置文件可以包含前导 \ 或不包含。
【问题讨论】:
标签: c# absolute-path