How to create an exe for a web application?

前端 未结 2 1124
灰色年华
灰色年华 2021-01-16 04:59

I had developed a web application using Visual studio 2012 (ASP.Net 4.5 - C#) and a web service. Both are laying in a single solution. I need to convert my solution to an EX

2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 05:22

    Quiet a late answer: I cannot post the whole project here but i'll post the flow chart here and you may try it.

    All the steps in the flowchart here should be controlled programmatically.

    1. Steps in starting the application.

    NOTE: Please ignore the third layer (Is website added) if you are using a local database(.mdf)

    The connection string to be used for a local database:

    public string ConnectionString =
                "Data Source=(local);Initial Catalog=YOUR_DATABASE_NAME;Integrated Security=True";
    

    But still remember that you need dotnet framework to be installed inorder to run your application. don't worry as you can set the pre-requisites in your application setup project.

    All the codes for the flowchart process below.

    Is IIS Installed:

    NOTE: I am posting the code for IIS 7 and above.

    public bool IsIISInstalled()
            {
                return Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp",
                    "VersionString", null) != null;
            }
    

    Install IIS

    public int InstallIIS()
            {
                string DISM_CMD_CODE = "START /WAIT DISM /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI /FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer /FeatureName:IIS-FTPSvc /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HostableWebCore /FeatureName:IIS-HttpCompressionDynamic /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-HttpErrors /FeatureName:IIS-HttpLogging /FeatureName:IIS-HttpRedirect /FeatureName:IIS-HttpTracing /FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-IISCertificateMappingAuthentication /FeatureName:IIS-IPSecurity /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-LegacyScripts /FeatureName:IIS-LegacySnapIn /FeatureName:IIS-LoggingLibraries /FeatureName:IIS-ManagementConsole /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ManagementService /FeatureName:IIS-Metabase /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ODBCLogging /FeatureName:IIS-Performance /FeatureName:IIS-RequestFiltering /FeatureName:IIS-RequestMonitor /FeatureName:IIS-Security /FeatureName:IIS-ServerSideIncludes /FeatureName:IIS-StaticContent /FeatureName:IIS-URLAuthorization /FeatureName:IIS-WebDAV /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-WebServerRole /FeatureName:IIS-WindowsAuthentication /FeatureName:IIS-WMICompatibility /FeatureName:WAS-ConfigurationAPI /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ProcessModel /FeatureName:WAS-WindowsActivationService";
                string command = DISM_CMD_CODE;
                ProcessStartInfo pStartInfo = new ProcessStartInfo("cmd.exe", "/c " + command);
                Process p = new Process();
                p.StartInfo = pStartInfo;
                //p.WaitForExit();
                p.Start();
                return 1;
            }
    

    Is Website added

    public bool IsWebsiteAddedInIIS(string WebsiteName)
            {
                ServerManager serverManager = new ServerManager();
                var site = serverManager.Sites.FirstOrDefault(s => s.Name == WebsiteName);
                if (site == null)
                {
                    //No site added
                    return false;
                }
                else
                {
                    //site added
                    return true;
                }
            }
    
    public int CreateNewWebsite(string SiteName, string PublishedFilesPath)
            {
                ServerManager serverManager = new ServerManager();
                var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
                if (site == null)
                {
                    serverManager.Sites.Add(SiteName, "http", "*:8080:", PublishedFilesPath);
                    serverManager.CommitChanges();
                    return 1;
                }
                else
                {
                    return 2;
                }
            }
    
    public void StartWebsite(string SiteName)
            {
                ServerManager serverManager = new ServerManager();
                var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
                if (site != null)
                {
                    site.Stop();
                    site.Start();
                }
            }
    
            public void StopWebsite(string SiteName)
            {
                ServerManager serverManager = new ServerManager();
                var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
                if (site != null)
                {
                    site.Stop();
                }
            }
    

    Get website URL

    public string GetWebsiteURL(string SiteName)
            {
                //string SiteUrl = "";
                //ServerManager serverManager = new ServerManager();
                //var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
                //var siteBindings = site.GetCollection("bindings");
                //string protocol = (string)siteBindings["protocol"];
                //string bindingInfo = (string)siteBindings["bindingInformation"];
                //if (protocol.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                //{
                //    string[] parts = bindingInfo.Split(':');
                //    if (parts.Length == 3)
                //    {
                //        //Get the port in use HERE !!!
                //        string port = parts[1];
                //        SiteUrl = "localhost:" + port;
                //    }
                //}
                //return SiteUrl;
    
                int port = 0;
                string SiteUrl = "";
                ServerManager serverManager = new ServerManager();
                var site = serverManager.Sites.FirstOrDefault(s => s.Name == SiteName);
                foreach (Binding binding in site.Bindings)
                {
                    port = binding.EndPoint.Port;
                    SiteUrl = "localhost:" + port + "/index.aspx";
                    break;
                }
                return SiteUrl;
            }
    

    Init Browsing the website

    You have to install Cefsharp chromium browser to your windows forms

    Install-Package CefSharp.WinForms -Version 75.1.143

    public void InitBrowser(string Address)
            {
                Cef.Initialize(new CefSettings());
                browser = new ChromiumWebBrowser(Address);
                Controls.Add(browser);
                browser.Dock = DockStyle.Fill;
            }
    

    Thank you..

提交回复
热议问题