0

I'va generated a class library in Dotnet. Now I have linked this library in various Clients, a console program, a service, am unit test project and a ASP.NET website. In some parts I need to load certain files (i.e. Excel templates) from disk. In this case the location for the different caller projects is different.

So my question is: What is the best way to find out, which project type is currently using my library?

Detecting the website client is easy, I just need to check if If System.Web.HttpContext.Current is not Nothing. But what about detecting the other clients?

Ulli
  • 500
  • 1
  • 7
  • 19
  • Are you saying our shared library tries to load assets from different locations, depending on the the project it is linked to? If yes, you should implement some sort of IoC. – Krumelur Jul 29 '15 at 08:23
  • 1
    Put the location of the templates/files into the application's config file. Then you can read the config entry from your library and don't have to guess. – M4N Jul 29 '15 at 08:25
  • You can check loaded assembly names or you can determine project type by checking predefined entries in web.config/app.config files (`appsettings`). – Afshar Mohebi Jul 29 '15 at 08:27

1 Answers1

4

Even if you manage to detect the different types of application now, you can never be sure whether that logic will still work in the future.

I think it's better to not guess and implement another solution, e.g:

  1. use paths which are relative to the current application's root directory, e.g. by using AppDomain.CurrentDomain.BaseDirectory (MSDN)
  2. put the location of your files into the app's config file (in the appSettings section) and read it from your library using ConfigurationManager.AppSettings.Get() (MSDN)
M4N
  • 94,805
  • 45
  • 217
  • 260
  • 1
    Ok, I will put a path in the app.config. But maybe I need the project type for other reasons as well. Is there no way to detect this by reflection? – Ulli Jul 29 '15 at 09:18