应用程序池的使用方法

应用程序池的使用方法,第1张

很多开机启动程序仅仅加在启动项里面,只有登陆后才真正启动。windows服务在开机未进行用户登录前就启动了。正是利用这一点,解决一些服务器自动重启后特定软件也自动启动的问题。

1新建一个服务项目 visual C#----windows----windows服务;

2添加一个dataset(xsd),用于存储启动目标的路径,日志路径等。

在dataset可视化编辑中,添加一个datatable,包含两列 StartAppPath 和 LogFilePath。分别用于存储目标的路径、日志路径。

我认为利用datasetxsd存储配置参数的优势在于可以忽略xml解析的具体过程直接使用xml文件。

在dataset中 提供了ReadXml方法用于读取xml文件并将其转换成内存中的一张datatable表,数据很容易取出来!同样,WriteXml方法用于存储为xml格式的文件,也仅仅需要一句话而已。

3 programcs文件 作为程序入口,代码如下:

view plaincopy to clipboardprint

using SystemCollectionsGeneric;

using SystemServiceProcess;

using SystemText;

namespace WindowsServices_AutoStart

{

static class Program

{

/// <summary>

/// 应用程序的主入口点。

/// </summary>

static void Main()

{

ServiceBase[] ServicesToRun;

// 同一进程中可以运行多个用户服务。若要将

// 另一个服务添加到此进程中,请更改下行以

// 创建另一个服务对象。例如,

//

// ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};

//

ServicesToRun = new ServiceBase[] { new WindowsServices_AutoStart() };

ServiceBaseRun(ServicesToRun);

}

}

}

using SystemCollectionsGeneric;

using SystemServiceProcess;

using SystemText;

namespace WindowsServices_AutoStart

{

static class Program

{

/// <summary>

/// 应用程序的主入口点。

/// </summary>

static void Main()

{

ServiceBase[] ServicesToRun;

// 同一进程中可以运行多个用户服务。若要将

// 另一个服务添加到此进程中,请更改下行以

// 创建另一个服务对象。例如,

//

// ServicesToRun = new ServiceBase[] {new Service1(), new MySecondUserService()};

//

ServicesToRun = new ServiceBase[] { new WindowsServices_AutoStart() };

ServiceBaseRun(ServicesToRun);

}

}

}

4servicecs主文件,代码如下:

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemIO;

using SystemDiagnostics;

using SystemServiceProcess;

using SystemText;

namespace WindowsServices_AutoStart

{

public partial class WindowsServices_AutoStart : ServiceBase

{

public WindowsServices_AutoStart()

{

InitializeComponent();

}

string StartAppPath =""; //@"F:\00exe";

string LogFilePath ="";// @"f:\WindowsServicetxt";

protected override void OnStart(string[] args)

{

string exePath = SystemThreadingThreadGetDomain()BaseDirectory;

//

if (!FileExists(exePath + @"\ServiceAppPathxml"))

{

dsAppPath ds = new dsAppPath();

object[] obj=new object[2];

obj[0]="0";

obj[1]="0";

dsTables["dtAppPath"]RowsAdd(obj);

dsTables["dtAppPath"]WriteXml(exePath + @"\ServiceAppPathxml");

return;

}

try

{

dsAppPath ds = new dsAppPath();

dsTables["dtAppPath"]ReadXml(exePath + @"\ServiceAppPathxml");

DataTable dt = dsTables["dtAppPath"];

StartAppPath = dtRows[0]["StartAppPath"]ToString();

LogFilePath = dtRows[0]["LogFilePath"]ToString();

}

catch { return; }

if (FileExists(StartAppPath))

{

try

{

Process proc = new Process();

procStartInfoFileName = StartAppPath; //注意路径

//procStartInfoArguments = "";

procStart();

}

catch (SystemException ex)

{

//MessageBoxShow(this, "找不到帮助文件路径。文件是否被改动或删除?\n" + exMessage, "提示", MessageBoxButtonsOK, MessageBoxIconInformation);

}

FileStream fs = new FileStream(LogFilePath, FileModeOpenOrCreate, FileAccessWrite);

StreamWriter m_streamWriter = new StreamWriter(fs);

m_streamWriterBaseStreamSeek(0, SeekOriginEnd);

m_streamWriterWriteLine("WindowsService: Service Started" + DateTimeNowToString() + "\n");

m_streamWriterFlush();

m_streamWriterClose();

fsClose();

}

}

protected override void OnStop()

{

try

{

// TODO: 在此处添加代码以执行停止服务所需的关闭 *** 作。

FileStream fs = new FileStream(LogFilePath, FileModeOpenOrCreate, FileAccessWrite);

StreamWriter m_streamWriter = new StreamWriter(fs);

m_streamWriterBaseStreamSeek(0, SeekOriginEnd);

m_streamWriterWriteLine("WindowsService: Service Stopped " + DateTimeNowToString() + "\n");

m_streamWriterFlush();

m_streamWriterClose();

fsClose();

}

catch

{

}

}

}

}

5启动调试,成功时也会d出一个对话框大致意思是提示服务需要安装。

6把Debug文件夹下面的exe执行程序,安装为windows系统服务,安装方法网上很多介绍。我说一种常用的:

安装服务

访问项目中的已编译可执行文件所在的目录。

用项目的输出作为参数,从命令行运行 InstallUtilexe。在命令行中输入下列代码:

installutil yourprojectexe

卸载服务

用项目的输出作为参数,从命令行运行 InstallUtilexe。

installutil /u yourprojectexe

至此,整个服务已经编写,编译,安装完成,你可以在控制面板的管理工具的服务中,看到你编写的服务。

7安装好了之后在系统服务列表中可以管理服务,这时要注意将服务的属性窗口----登陆----“允许于桌面交互”勾选!这样才能在启动了你要的目标程序后不单单存留于进程。在桌面上也看得到。

8关于卸载服务,目前有两个概念:一是禁用而已;一是完全删除服务。 前者可以通过服务管理窗口直接完成。后者则需要进入注册表“HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services”找到服务名称的文件夹,整个删掉,重新启动电脑后,服务消失。

9扩展思考:经过修改代码,还可以实现:启动目标程序之前,检测进程中是否存在目标程序,存在则不再次启动

以上就是关于应用程序池的使用方法全部的内容,包括:应用程序池的使用方法、access 怎么把查询符合条件的子窗体里的数据导出到excel、手机如何关闭后台程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/9660040.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存