Search code examples
c#asp.net.netvb.netprojects

Detect calling project type in a .net class library


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?


Solution

  • 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)