开发者

windows service throws an error

I am a newbie in C# .net and I am trying to get a windows service work. I followed this tutorial

http://www.beansoftware.com/NET-Tutorials/Create-Windows-Services.aspx

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {

        }


        protected override void OnStop()
        {
        }
        private void FolderWatcherTest_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            TextWriter writer = new StreamWriter("C:\\FolderLog.txt", true);
            writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been created. ");
            writer.Close();
        }

        private void FolderWatcherTest_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            TextWri开发者_JS百科ter writer = new StreamWriter("C:\\FolderLog.txt", true);
            writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been deleted. ");
            writer.Close();
        }

        private void FolderWatcherTest_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            TextWriter writer = new StreamWriter("C:\\FolderLog.txt", true);
            writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been renamed. ");
            writer.Close();
        }

    }
}

I was able to get the sevice registered in the windows service. But when I try to start it, it gives me an error 2: The system cannot find the file specified

I am not sure whats going on. Also I am kinda confused where these methods are being called as the tutorial didnt make any reference to these methods

private void FolderWatcherTest_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            TextWriter writer = new StreamWriter("C:\\FolderLog.txt", true);
            writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been created. ");
            writer.Close();
        }

        private void FolderWatcherTest_Deleted(object sender, System.IO.FileSystemEventArgs e)
        {
            TextWriter writer = new StreamWriter("C:\\FolderLog.txt", true);
            writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been deleted. ");
            writer.Close();
        }

        private void FolderWatcherTest_Renamed(object sender, System.IO.RenamedEventArgs e)
        {
            TextWriter writer = new StreamWriter("C:\\FolderLog.txt", true);
            writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been renamed. ");
            writer.Close();
        }

Thanks for your help

UPDATE1

ok I was able to register the service. I was running the .exe to install as a service from a different folder where why project files were. But now it registers fine. However I am not able to fire up these private void FolderWatcherTest_* methods. They are supposed to log the changes when I change the file name on the desktop. Any help will be appreciated

thanks


You need to first make sure that your service can start. Comment out file watcher code temporarily until you able to actualy start the service. Try re installing your windows service with InstallUtil. This utility is usually located here (Windows 7):

c:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe

Run this command first, it will uninstall service:

installutil /u yourproject.exe

The service should disappear from list of registered services. If this does not work you can use sc delete command to do the same. After you removed the service, find where you exe is. It is usually located in \bin\Debug\yourproject.exe or \bin\Release\yourproject.exe folders under your project directory. Run this command to register this exe as windows service:

installutil yourproject.exe

There should be no errors and it should say "The Commit phase completed successfully."

Look at this tutorial.


The Onstart method should have some logic to trigger these events which you have subscribed via FileSystemWatcher.

Do you have those txt files in the specified drive/directory first?

Also see eventviewer under control panel for any service errors.


When you Install a windows services you have a option of running on a

  1. local system account
  2. Service account

If you are running on the service account you need to give the permission to the service account on that folder.

please check the account on which you are running.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜