Windows服务引发错误

时间:2011-08-17 03:16:02

标签: c# visual-studio-2010 windows-services

我是C#.net的新手,我正在努力让Windows服务工作。我按照本教程

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

    }
}

我能够获得在Windows服务中注册的服务。但是当我尝试启动它时,它会给我一个错误2:系统无法找到指定的文件

我不确定最新情况。我也很困惑,因为教程没有对这些方法进行任何引用,因此调用这些方法

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

感谢您的帮助

UPDATE1

好的,我能够注册该服务。我正在运行.exe来从一个不同的文件夹安装服务,其中包含项目文件的原因。但现在它注册正常。但是我无法解雇这些 private void FolderWatcherTest_ *方法。当我在桌面上更改文件名时,他们应该记录更改。任何帮助将不胜感激

感谢

3 个答案:

答案 0 :(得分:2)

您首先需要确保您的服务可以启动。暂时注释文件观察程序代码,直到您能够实际启动服务。尝试使用InstallUtil重新安装Windows服务。此实用程序通常位于此处(Windows 7):

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

首先运行此命令,它将卸载服务:

installutil /u yourproject.exe

该服务应从已注册服务列表中消失。如果这不起作用,您可以使用sc delete命令执行相同操作。删除服务后,找到您的exe所在的位置。它通常位于项目目录下的\bin\Debug\yourproject.exe\bin\Release\yourproject.exe个文件夹中。运行此命令将此exe注册为Windows服务:

installutil yourproject.exe

应该没有错误,应该说“提交阶段已成功完成。”

看看这个tutorial

答案 1 :(得分:1)

Onstart方法应该有一些逻辑来触发你通过FileSystemWatcher订阅的这些事件。

您是否首先在指定的驱动器/目录中有这些txt文件?

另请参阅控制面板下的eventviewer,了解任何服务错误。

答案 2 :(得分:1)

安装Windows服务时,您可以选择在

上运行
  1. 本地系统帐户
  2. 服务帐户
  3. 如果您在服务帐户上运行,则需要授予该文件夹上服务帐户的权限。

    请检查您正在运行的帐户。