[RunInstaller(true)]
public partial class ProjectInstaller : Installer
{
private ServiceProcessInstaller serviceProcessInstaller;
private ServiceInstaller serviceInstaller;
public ProjectInstaller()
{
serviceProcessInstaller = new ServiceProcessInstaller();
serviceInstaller = new ServiceInstaller();
// Here we can set properties on serviceProcessInstaller
//or register event handlers
serviceProcessInstaller.Account = ServiceAccount.LocalService;
serviceInstaller.ServiceName = MyNewService.MyServiceName;
this.Installers.AddRange(new Installer[] {
serviceProcessInstaller, serviceInstaller });
}
private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}
}
public partial class MyNewService : ServiceBase
{
FileSystemWatcher myWatcher = new FileSystemWatcher("C:\\Users\\Ahmed\\Desktop\\demo");
public const string MyServiceName = "MyNewService";
private FileSystemWatcher watcher = null;
public MyNewService()
{
InitializeComponent();
myWatcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
}
//private static void OnChanged(object source, FileSystemEventArgs e)
//{
// WatcherChangeTypes wct = e.ChangeType;
// Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());
//}
protected void FileCreated(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
if (Directory.Exists(e.FullPath))
{ Console.WriteLine("Directory Exists"); } // a directory
else { Console.WriteLine("File"); }
// a file
}
}
protected override void OnStart(string[] args)
{
this.ServiceName = MyServiceName;
FileSystemWatcher watcher = new FileSystemWatcher("C:\\Users\\Ahmed\\Desktop\\demo", "*.txt");
//Watch for changes in LastAccess and LastWrite times, and
//the renaming of files or directories.
watcher.NotifyFilter = NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.FileName
| NotifyFilters.DirectoryName;
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
}
protected override void OnStop()
{
watcher.EnableRaisingEvents = false;
watcher.Dispose();
LogEvent("Monitoring Stopped");
}
void OnChanged(object sender, FileSystemEventArgs e)
{
string mgs = string.Format("File {0} | {1}",e.FullPath, e.ChangeType);
LogEvent(mgs);
}
void OnRenamed(object sender, RenamedEventArgs e)
{
string log = string.Format("{0} | Renamed from {1}",
e.FullPath, e.OldName);
LogEvent(log);
}
private void LogEvent(string message)
{
string eventSource = "File Monitor Service";
DateTime dt = new DateTime();
dt = System.DateTime.UtcNow;
message = dt.ToLocalTime() + ": " + message;
EventLog.WriteEntry(eventSource, message);
}
private void eventLog1_EntryWritten(object sender, EntryWrittenEventArgs e)
{
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyNewService()
};
ServiceBase.Run(new ServiceBase[] { new MyNewService() });
}
}
答案 0 :(得分:0)
这里的问题是this.ServiceName = MyServiceName;
应该在构造函数中,而不是OnStart方法。构造函数将为每个实例设置它,但它会抛出异常,因为在调用OnStart时服务已经被认为是在运行。
ServiceName标识服务控制管理器的服务。此属性的值必须与相应安装程序类的ServiceInstaller.ServiceName属性中为服务记录的名称相同。在代码中,服务的ServiceName通常设置在可执行文件的main()函数中。