我有一个类库正在读取一些XSLT文件。我使用的是资源,但是它们已经在dll中预编译,我们希望能够随时更改这些文件,所以我被要求从文件系统中读取它们。如何从Web应用程序调用时获取类库的路径?
我开始使用相对路径,单元测试运行正常,但集成失败,因为在这种情况下,它们正在运行Web应用程序,并出现错误的路径。
的代码非常简单,它只是加载XSLT:
string xslFilePath = @"xslts\transformation1.xsl";
XslCompiledTransform transformation = new XslCompiledTransform();
transformation.Load(xslFilePath, new XsltSettings(true, true), new XmlUrlResolver());
在集成测试的错误是:
DirectoryNotFoundException ExceptionMessage - 找不到路径的一部分“C :\ Program Files \ Common Files \ Microsoft Shared \ DevServer \ 10.0 \ xslts \ transformation1.xsl'。
到目前为止,我已经找到了最近的解决方案是:
string codeBase = Assembly.GetAssembly(typeof(Class1)).CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string fullPath = Uri.UnescapeDataString(uri.Path);
string theDirectory = Path.GetDirectoryName(fullPath);
string relativePath = @"\xslts\transformation1.xsl";
string path = theDirectory + relativePath;
但它检索错误的道路,这一次它是一个的使用DLL中的Web应用程序的路径。我仍然在努力寻找解决方案,它必须是一种获取dll路径的方法。
要读取的文件的实际位置是什么? – abatishchev 2011-05-20 09:19:49
假设位置为:c:\ dll \ xslts \ file.xsl,并且该dll由位于c:\ webapplication中的Web应用程序使用。我开始认为这是不可能的,我需要将这些xslts移动到Web应用程序目录,但它对我来说没有多大意义,因为它们只能用于该dll。 – 2011-05-20 09:31:44
作为参考添加的dll将被复制到\ bin。所以使用绝对路径。或者将文件嵌入为资源 – abatishchev 2011-05-20 09:38:41