简体   繁体   中英

Windows Service without the VS2005 template

I have the VS2005 standard edition and MS says this:

Note: The Windows Service Application project templates and associated functionality are not available in the Standard Edition of Visual Basic and Visual C# .NET...

Is it possible to write a Windows Service application without upgrading my VS2005 Standard edition?

If you can cut and paste, an example is enough.

A simple service to periodically log the status of another service. The example does not include the ServiceInstaller class (to be called by the install utility when installing a service application), so installing is done manually.

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

Sure, you just need to write the code yourself. It's not actually very hard. Here are a couple of references to how to do it:

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM