如何为Web应用程序创建exe? [英] How to create an exe for a web application?

查看:115
本文介绍了如何为Web应用程序创建exe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用Visual Studio 2012(ASP.Net 4.5-C#)和Web服务开发了一个Web应用程序.两者都放在一个单一的解决方案中.我需要将解决方案转换为EXE文件(为我的Web应用程序创建EXE).我真正需要的是,如果运行安装文件,它将在IIS中托管我的Web应用程序和Web服务.请提供解决问题的步骤.

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 EXE file (Creating EXE for my web application). What i need exactly is, if run my setup file, it should host my web application and web service in IIS. Kindly provide the steps to solve me the problem.

推荐答案

安静回答: 我无法在此处发布整个项目,但是我会在此处发布流程图,您可以尝试一下.

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.启动应用程序的步骤.

1. Steps in starting the application.

注意:如果您使用的是本地数据库(.mdf),请忽略第三层(已添加网站)

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";

但是请记住,您需要安装dotnet框架才能运行您的应用程序.不必担心,因为您可以在应用程序设置项目中设置先决条件.

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.

是否已安装IIS :

注意:我正在发布IIS 7及更高版本的代码.

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;
        }

安装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;
        }

是否添加了网站

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();
            }
        }

获取网站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;
        }

初始化浏览网站

您必须在Windows窗体上安装Cefsharp Chrome浏览器

You have to install Cefsharp chromium browser to your windows forms

安装软件包CefSharp.WinForms-版本75.1.143

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

谢谢..

这篇关于如何为Web应用程序创建exe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆