Reputation: 8514
I have a WebSite project - ASPX, 4.0. In it I have a folder like:
bin\Xslt\Template.xslt
I want to load this file in my class library. In web.config is:
<configuration>
<appSettings>
<add key="Filepath" value=".\Xslt\Template.xslt" />
</appSettings>
</configuration>
But, my class library can't find it:
mTransform = new XslCompiledTransform();
mTransform.Load(ConfigurationManager.AppSettings["Filepath"]);
throws: Could not find a part of the path 'C:\Xslt\Template.xslt'.
I know there's a catch here, but I can't remember the proper way...
How to reference a file in an aspx \bin\ folder correctly?
Upvotes: 0
Views: 454
Reputation: 8514
I found the way (since it's in an assembly and I didn't want to add dependency on System.Web):
string codeBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
string filepathRelative = ConfigurationManager.AppSettings["Filepath"];
string fullFilepath = Path.Combine(codeBase, filepathRelative);
Thanks
edit: The System.AppDomain.CurrentDomain.BaseDirectory
also does the trick...
Upvotes: 0
Reputation: 6123
Try to give the complete path in your web config Example if your file is at C:\VSPROJECTS\bin\Xslt\Template.xslt
Then in your webconfig write it as
< add key="Filepath" value="C:\VSPROJECTS\bin\Xslt\Template.xslt" />
Upvotes: 0
Reputation: 66439
Try changing your key to:
<add key="Filepath" value="~/bin/Xslt/Template.xslt" />
And change your code to:
mTransform.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["Filepath"]));
Upvotes: 1