Reputation: 1179
Any reliable elegant way of detecting whether the current app is a win or web forms (or other) application?
We have a common config class which needs to open either app.config or web.config.
Currently, I'm catching ArgumentException when I try OpenExeConfiguration but it's not very elegant and might mask other issues.
Upvotes: 0
Views: 579
Reputation: 5320
I usually check if there's a HttpContext available (if it's a Web Application since in a Web Service HttpContenxt.Current
is null)
To do this, you should add System.Web
to your references.
if(HttpContext.Current!=null)
//It's a web application
else
//it's a win application
Upvotes: 3
Reputation: 2313
You could try getting the executing process via Process.GetCurrentProcess()
, possibly you could check the Process.MainWindowHandle
is not IntPtr.Zero
, or check the process name, or potentially scan the loaded modules of the Process. This would have an advantage (I would assume) of not requiring your to load assemblies that are not required for your current execution context (e.g. don't load System.Windows.Forms.dll when that app is a web app).
However this seems inelegant.
Upvotes: 0
Reputation: 196539
Try using dependency injection so the config class doesn't have to do a switch statement.
Upvotes: 1