I'm struggling to find a way of specifying a file location in web.config
appSettings that avoids using hard-coded paths but allows a non-'web aware' C# library to find a file.
The C# library uses standard File.Open
, File.Exists
methods, etc. to operate on a data file, which is stored in my web application (ASP.NET MVC) tree, e.g. under:
\content\data\MyDataFile.txt
Requirements:
<appSettings> this--> <add key="MyFileLocation" value="~\content\data\MyDataFile.txt" /> not --> <add key="MyFileLocation" value="c:\inetpub\wwwroot\foo\content\data\MyDataFile.txt" /> </appSettings>
Any suggestions on how I can do this cleanly? Thanks!
You could use Path.Combine
to combine AppDomain.CurrentDomain.BaseDirectory
and your relative path.
This will give you a path relative to the ASP.NET root directory (~/) in an ASP.NET app, or a path relative to the directory containing the executable in a WinForms or Console application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With