使用 ASP.NET 执行控制台应用程序






4.67/5 (12投票s)
使用 ASP.NET Web 应用程序执行控制台应用程序
引言
本文介绍了一种在同一服务器中修改 .NET EXE 配置并使用 ASP.NET Web 应用程序执行控制台应用程序的简单方法。
背景
有时,我们需要使用控制台应用程序来完成一项任务(例如,使用控制台应用程序来执行报告作业),但我们需要使用 Web 应用程序来执行它。本文介绍了如何使用一些参数和一个按钮来执行 .NET 控制台应用程序,分别用于修改 app.config 和执行应用程序。
Using the Code
在这里,我们将执行以下步骤
- 创建一个简单的控制台应用程序。
- 创建一个非常简单的 ASP.NET Web 应用程序(从空项目开始)。
- 创建两种方法,通过单击按钮修改 app.config 并执行控制台应用程序。
1. 创建一个简单的控制台应用程序
好的,我们从第一步开始。在这一步中,我们将使用 C# 创建一个简单的控制台应用程序(我使用 Visual Studio 2012),我们创建一个强制杀死应用程序(命名为 ProcessKiller
),使用这个应用程序,我们可以杀死后台进程中的应用程序,比如 Excel、计算器,或者任何我们想要的。这只是一个例子,您可以创建另一个适合您情况的控制台应用程序。
完整的 C# 代码如下所示,请注意,我们需要在我们的项目中包含配置管理器(命名空间 System.Configuration
)和诊断(命名空间 System.Diagnostics
)引用,所以不要错过它。
using System;
using System.Collections;
using System.Configuration;
using System.Diagnostics;
namespace ProcessKiller
{
class Program
{
static void Main(string[] args)
{
string ApplicationName =
ConfigurationManager.AppSettings["ApplicationName"].ToString();
Process[] Processes = Process.GetProcessesByName(ApplicationName);
// check to kill the right process
foreach (Process process in Processes)
{
process.Kill();
}
}
}
}
C# 代码已完成,现在我们可以在 app.config 中做一些小事情,我们在 app.config 中创建应用程序配置节点(请注意,app.config 文件是一种 XML 文件,所以我们将在 ASP.NET 中使用 XML 库来调用我们的方法)。
我们在 app.config 中创建一个键值对,将键命名为 ApplicationName
,并将值(对于默认值)设置为 "calc
"。所有的代码都像这样
<!--?xml version="1.0" encoding="utf-8" ?-->
<configuration>
<appsettings>
<add key="ApplicationName" value="calc">
</add>
</appsettings>
</configuration>
我们已经完成了这一步。构建项目。
2. 创建一个非常简单的 ASP.NET 空 Web 应用程序
我们创建一个非常简单的 Web 应用程序(命名为 WebKiller
),为 ASP.NET 空 Web 应用程序模板创建项目,并将 Default.aspx 添加到项目中。我们只需要创建一个 textbox
来更改控制台 app.config 中键 ApplicationName
的值,并且我们创建一个按钮,通过单击它来执行控制台应用程序。
ASP 代码如下所示
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="WebKiller.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server">
<asp:Button ID="Button1" runat="server"
Text="Execute" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
尝试调试它,Web 应用程序将类似于以下内容
好的,我们已经完成了 ASP 代码,记住 ASP 中的控件 ID,我们需要在 C# 代码中使用它。我们准备好在代码隐藏中做一些逻辑了。
3. 创建两种方法,通过单击按钮修改 app.config 并执行控制台应用程序
在这一步中,我们将创建 Web 应用程序背后的代码。我们为我们创建的按钮创建一个事件方法(单击时),确保您从 Visual Studio 中的事件窗口创建空事件方法。
现在,我们创建一个 void
方法来更改控制台应用程序的 app.config,记住 app.config 中的键值。我们将在稍后的按钮事件方法中调用此方法。
该方法代码将如下所示。将该方法命名为 ChangeValueOfKeyConfig()
。
private void ChangeValueOfKeyConfig(string ProcessName)
{
// I locate the ProcessKiller Console Application in E drive.
string tAppPath = @"E:\ProcessKiller\ProcessKiller\bin\Debug";
string tProcessName = TextBox1.Text;
string tAppPathWithConfig = @"" + tAppPath + "\\ProcessKiller.exe.config";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(tAppPathWithConfig);
foreach (XmlElement element in xmlDoc.DocumentElement)
{
if (element.Name == "appSettings")
{
foreach (XmlNode node in element.ChildNodes)
{
RenameItemConfig(node, "ApplicationName", ProcessName);
//RenameItemConfig(node, "Other Key", "Other Value");
}
}
}
xmlDoc.Save(tAppPathWithConfig);
}
private void RenameItemConfig(XmlNode node, string Key, string value)
{
if (node.Attributes.Item(0).Value == Key)
{
node.Attributes.Item(1).Value = value;
}
}
请注意,我们可以使用 RenameItemConfig()
方法更改 app.config 文件中键的值。然后,我们准备好在这里填写按钮事件方法。首先,我们需要“获取”我们创建的 textbox
的值,然后我们需要找到要执行的控制台应用程序的路径,然后我们可以在按钮单击事件方法中调用我们的 ChangeValueOfKeyConfig()
方法,然后再次,我们将打开命令提示符 (CMD),并且我们将通过 CMD 使用一些命令执行我们的控制台应用程序。Default.aspx.cs 中的完整代码如下所示
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace WebKiller
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ChangeValueOfKeyConfig(TextBox1.Text);
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd";
process.StartInfo.WorkingDirectory = @"E:\ProcessKiller\ProcessKiller\bin\Debug";
process.StartInfo.Arguments = "/c \"" + "ProcessKiller.exe" + "\"";
process.Start();
}
private void ChangeValueOfKeyConfig(string ProcessName)
{
// I locate the ProcessKiller Console Application in E drive.
string tAppPath = @"E:\ProcessKiller\ProcessKiller\bin\Debug";
string tProcessName = TextBox1.Text;
string tAppPathWithConfig = @"" + tAppPath + "\\ProcessKiller.exe.config";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(tAppPathWithConfig);
foreach (XmlElement element in xmlDoc.DocumentElement)
{
if (element.Name == "appSettings")
{
foreach (XmlNode node in element.ChildNodes)
{
RenameItemConfig(node, "ApplicationName", ProcessName);
//RenameItemConfig(node, "Other Key", "Other Value");
}
}
}
xmlDoc.Save(tAppPathWithConfig);
}
private void RenameItemConfig(XmlNode node, string Key, string value)
{
if (node.Attributes.Item(0).Value == Key)
{
node.Attributes.Item(1).Value = value;
}
}
}
}
演示
现在我们可以演示我们的项目,我们将强制杀死 Excel 应用程序。我们需要做的是:在我们的浏览器中打开/运行 Web 应用程序,并使用单词 "excel
"(全部小写)填充输入文本框,在单击 "执行" 按钮之前,我们打开 Microsoft Office Excel,如果 Excel 打开,我们单击 "执行" 按钮。控制台应用程序将完成它的工作,它将强制杀死/关闭 excel。现在,我们已经完成了,您可以做一些更改,您可以通过用 "calc" 填充输入文本框或任务管理器中“进程”选项卡中显示的任何其他应用程序来关闭计算器应用程序。
关注点
控制台应用程序可以通过 Web 应用程序以简单的方式在同一服务器中执行。使用此方法,我们可以创建一个控制台应用程序来生成大型 Excel 文件或其他任何东西。您可以根据您的具体情况简化和增强此代码。实际上,您不必在您的实际项目中使用此方法,此方法仅在非常罕见的情况下有用,但我希望此代码可以帮助人们了解如何执行它。
历史
- 2016 年 1 月 4 日:初始版本