ARTICLE AD BOX
I have an ASP.NET webform framework 8.0 that is working fine but I would like to load the Web Url where my webform is hosted on a Winforms app and determine whether the webform url is loaded on the winforms or the browser.
I've tried all search suggestion but have not had any luck.
This is my code to determine whether is Winforms or browser:
public static class EnvironmentHelper { public static bool IsWeb => System.Web.HttpContext.Current != null; public static bool IsWinForms => !IsWeb; }When I check for
if (EnvironmentHelper.IsWinForms) { Response.Write("Loaded on Windows Form App"); }in the ASP.NET webforms project, it does not work when running the Winforms app.
Any idea what I am missing?
Thanks
760k186 gold badges1.4k silver badges1.5k bronze badges
1
En İyi Çözüm - Ortam Değişkeni Kullanmak:
public static class EnvironmentHelper { private static readonly Lazy<bool> _isWeb = new Lazy<bool>(() => { // Birden fazla kontrol yöntemi if (System.Web.HttpContext.Current != null) return true; // AppSetting'den kontrol var configValue = ConfigurationManager.AppSettings["ApplicationType"]; return string.Equals(configValue, "Web", StringComparison.OrdinalIgnoreCase); }); private static readonly Lazy<bool> _isWinForms = new Lazy<bool>(() => { // WinForms için özel kontrol var configValue = ConfigurationManager.AppSettings["ApplicationType"]; if (string.Equals(configValue, "WinForms", StringComparison.OrdinalIgnoreCase)) return true; // Assembly varlığını kontrol et return AppDomain.CurrentDomain.GetAssemblies() .Any(a => a.FullName.Contains("System.Windows.Forms")); }); public static bool IsWeb => _isWeb.Value; public static bool IsWinForms => _isWinForms.Value && !IsWeb; }New contributor
Mehmet Ali is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
2 Comments
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
2026-01-24T14:37:27.097Z+00:00
Thank you, Mehmet, I tried your suggestion and still does not work, the Winform App still keeps loading the Web Url as if it were loaded in the Browser. I added the <add key="ApplicationType" value="WinForms"/> as suggested to the Web.Config and modified the EnvironmentHelper class as suggested to the WebForm App. I am trying to get the message "Loaded on the WinForm" when using the WinForm App and "Loaded From WebForm" when using the Browser.
2026-01-24T19:22:42.67Z+00:00
Explore related questions
See similar questions with these tags.
