Is there a way for an .NET library to detect whether or not it is being run as a service?
My library can be run either way. But when its run as a service, developer
Seems I am bit late to the party, but topic seems popular.
Interesting difference when run as a service is that at app start current folder points to system directory (C:\windows\system32 by default). Its hardly unlikely user app will start from the system folder in any real life situation.
So, I use following trick (c#):
protected static bool IsRunAsService()
{
string CurDir = Directory.GetCurrentDirectory();
if (CurDir.Equals(Environment.SystemDirectory, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
return (false);
}
More details at: https://stackoverflow.com/a/60174944/8494004