2

Is it possible to specify a path that is relative to the root of both a console and an asp.net webapplication? So

  • For the Console Application, it should be relative to the .exe file
  • For the ASP.NET Webapplication, it should be relative to the root Web.Config

Example: Let's say I have a file called "log.txt". I have a logging component in a separate dll, that uses this log.txt under "logs" folder. I have two applications that use this component, one is a Console Application, the other one is an ASP.NET Web Application.

Is it possible to specify a relative path in the logging component, that will find the file at the following places?:

mywebapp/
-Web.Config
-bin/logging.dll
-other web files
-logs/log.txt

myconsoleapp/
-myconsoleapp.exe
-logging.dll
-some other dlls
-logs/log.txt

Kornél Regius
  • 2,799
  • 5
  • 26
  • 36

2 Answers2

4

Use AppDomain.CurrentDomain.BaseDirectory

Reference:

Community
  • 1
  • 1
Kornél Regius
  • 2,799
  • 5
  • 26
  • 36
1

I would recommend a little change:

Don't determine the path of your log within your logging component. Make it as a parameter. On the webàpplication you can use Server.MapPath (MSDN Server.MapPath Method) to map relative to absolute paths.

csteinmueller
  • 2,297
  • 1
  • 17
  • 30
  • That's my only idea too. Too bad that my problem is a lot more complex, this logging thing was just an example (I actually try to find multiple custom config files for multiple components). I can't accept this, since it's not an answer to my question, but I'll still give an upvote for it. – Kornél Regius Feb 18 '14 at 09:08
  • What's within your reach? Can you make changes within these components? And what webapplication? WCF?ASP? – csteinmueller Feb 18 '14 at 10:11
  • I can make changes in the component, and it's an ASP.NET application. – Kornél Regius Feb 18 '14 at 10:13