我在ubuntu 12.04 LTS上用FileSystemWatcher
编写了一个使用mono develop的服务。该服务正常运行,但是当我尝试使用脚本作为ubuntu守护程序运行它时,我的日志文件显示该服务正在运行,但FileSystemWatcher
没有引发事件,尽管我通过双击运行脚本然后服务运行正常。
这是我的OnStart
方法:
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
protected override void OnStart (string[] args)
{
try
{
WriteLog("dir Srvc On Start");
string d = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
watcher = new FileSystemWatcher ();
watcher.Path = d;
watcher.NotifyFilter =
NotifyFilters.DirectoryName | NotifyFilters.FileName |
NotifyFilters.LastAccess | NotifyFilters.LastWrite;
watcher.IncludeSubdirectories = false;
watcher.Filter = "*.txt";
watcher.Created += new FileSystemEventHandler(OnCreated);
watcher.Error += new ErrorEventHandler (OnError);
watcher.EnableRaisingEvents = true;
}
catch(Exception ex)
{
WriteLog("On Startup Exeption: "+ex.ToString());
}
}
这是我的OnCreated
Methhod:
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
private void OnCreated(Object O, FileSystemEventArgs e)
{
try
{
WriteLog(e.FullPath.ToString() + " " + e.Name.ToString() + " "
+ e.ChangeType.ToString());
string outputdir = "OutPut";
string d = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string outdir = Path.Combine(d,outputdir);
Directory.CreateDirectory (outdir);
string outputfile = Path.Combine (outdir,e.Name);
File.Move(e.FullPath,outputfile);
}
catch(Exception ex)
{
WriteLog("On Created Exeption: "+ex.ToString());
}
}
我在rc2.d目录下的可执行脚本:
#!/bin/sh
mono-service -l:/dir/forlock/service.lock /dir/forsrvc/dirWatchSrvc.exe
我也是ubuntu的新手,对其中的脚本知之甚少。
更新
我更新了代码,但仍无法正常工作