我有VS2005标准版,MS这样说:
注意:Windows 服务应用程序项目模板和相关功能在 Visual Basic 和 Visual C# .NET 标准版中不可用...
是否可以在不升级我的 VS2005 标准版的情况下编写 Windows 服务应用程序?
我有VS2005标准版,MS这样说:
注意:Windows 服务应用程序项目模板和相关功能在 Visual Basic 和 Visual C# .NET 标准版中不可用...
是否可以在不升级我的 VS2005 标准版的情况下编写 Windows 服务应用程序?
如果你可以剪切和粘贴,一个例子就足够了。
定期记录另一个服务状态的简单服务。该示例不包括ServiceInstaller 类(在安装服务应用程序时由安装实用程序调用),因此安装是手动完成的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;
namespace SrvControl
{
public partial class Service1 : ServiceBase
{
Timer mytimer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (mytimer == null)
mytimer = new Timer(5 * 1000.0);
mytimer.Elapsed += new ElapsedEventHandler(mytimer_Elapsed);
mytimer.Start();
}
void mytimer_Elapsed(object sender, ElapsedEventArgs e)
{
var srv = new ServiceController("MYSERVICE");
AppLog.Log(string.Format("MYSERVICE Status {0}", srv.Status));
}
protected override void OnStop()
{
mytimer.Stop();
}
}
public static class AppLog
{
public static string z = "SrvControl";
static EventLog Logger = null;
public static void Log(string message)
{
if (Logger == null)
{
if (!(EventLog.SourceExists(z)))
EventLog.CreateEventSource(z, "Application");
Logger = new EventLog("Application");
Logger.Source = z;
}
Logger.WriteEntry(message, EventLogEntryType.Information);
}
}
}
当然,您只需要自己编写代码。这实际上并不难。以下是有关如何执行此操作的一些参考:
http://msdn.microsoft.com/en-us/magazine/cc301845.aspx
http://www.aspfree.com/c/a/C-Sharp/Creating-a-Windows-Service-with-C-Sharp-introduction/