我的服务代码位于OnStart()
抛出异常(I)并且服务已停止。我不知道为什么有任何前任。扔了?..这是我的代码:
public Service1()
{
InitializeComponent();
}
Thread thread;
protected override void OnStart(string[] args)
{
thread = new Thread(delegate()
{
string path = @"D:\levani\FolderListenerTest\ListenedFolder";
FileSystemWatcher listener;
listener = new FileSystemWatcher(path);
listener.Created += new FileSystemEventHandler(listener_Created);
listener.EnableRaisingEvents = true;
});
thread.Start();
}
public void listener_Created(object sender, FileSystemEventArgs e)
{
File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name);
}
protected override void OnStop()
{
thread.Abort();
}
记录
Log Name: Application
Source: .NET Runtime
Date: 6/11/2012 5:33:27 PM
Event ID: 1026
Task Category: None
Level: Error
Keywords: Classic
User: N/A
Computer: Levan-PC
Description:
Application: FolderListenerService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
Stack:
at System.IO.__Error.WinIOError(Int32, System.String)
at System.IO.File.InternalCopy(System.String, System.String, Boolean)
at System.IO.File.Copy(System.String, System.String)
at FolderListenerService.Service1.listener_Created(System.Object, System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name=".NET Runtime" />
<EventID Qualifiers="0">1026</EventID>
<Level>2</Level>
<Task>0</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime="2012-06-11T14:33:27.000000000Z" />
<EventRecordID>18314</EventRecordID>
<Channel>Application</Channel>
<Computer>Levan-PC</Computer>
<Security />
</System>
<EventData>
<Data>Application: FolderListenerService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException
Stack:
at System.IO.__Error.WinIOError(Int32, System.String)
at System.IO.File.InternalCopy(System.String, System.String, Boolean)
at System.IO.File.Copy(System.String, System.String)
at FolderListenerService.Service1.listener_Created(System.Object, System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.OnCreated(System.IO.FileSystemEventArgs)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32, System.String)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
</Data>
</EventData>
</Event>
答案 0 :(得分:3)
可能有多种原因。请参阅File.Copy()文档,尤其是例外部分,其中记录了可能引发的所有异常。
您需要包装File.Copy()并捕获任何异常,以便您做出适当的反应:
public void listener_Created(object sender, FileSystemEventArgs e)
{
try
{
File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name);
}
catch {FileNotFoundException e)
{
//do something if file isn't there
}
catch {UnauthorizedAccessException e)
{
//do something if invalid permissions
}
//etc
}
答案 1 :(得分:1)
File.Copy
中的额外参数true将覆盖该文件(如果已存在)。我认为错误是文件已经存在。
File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name,true);
将代码放在try..catch block
中并捕获IOException
异常。您可以登录文件进行进一步调试。
当文件名,目录名或卷标语法不正确时,我们得到WinIOError
错误(因为我们进入调用堆栈)。所以只需检查正确的路径和文件名。
答案 2 :(得分:0)
如果创建新线程,则需要确保处理该线程上抛出的所有异常。 Thread.Start()创建的线程上发生的任何未处理的异常都将导致应用程序终止。
具体来说,构造函数FileSystemWatcher(string path)和File.Copy(string sourceFileName, string destFileName)会抛出一些您当前代码中未处理的异常。这两个都是在一个单独的线程上调用的。由于文件已经存在,很可能会出现IOException(对同一文件进行多次更改会导致代码尝试多次复制,导致第一次复制后发生冲突)。
你应该更新你的File.Copy调用以使用File.Copy(string sourceFileName, string destFileName, bool overwrite)并将你的listener_Created
函数包装在一个try / catch块中,该块除了重新抛出之外都会执行soemthing。
答案 3 :(得分:0)
我不知道为什么但是在我通过 try {} catch {} 包围我的代码后,它的效果很好,有什么想法吗?这是代码:
public Service1()
{
InitializeComponent();
}
Thread thread;
protected override void OnStart(string[] args)
{
try
{
thread = new Thread(delegate()
{
string path = @"D:\levani\FolderListenerTest\ListenedFolder";
FileSystemWatcher listener; listener = new FileSystemWatcher(path);
listener.Created += new FileSystemEventHandler(listener_Created);
listener.EnableRaisingEvents = true;
});
thread.Start();
}
catch (Exception ex)
{
File.WriteAllText(@"D:\levani\bussite.txt", "thread: " + ex.ToString());
}
}
public void listener_Created(object sender, FileSystemEventArgs e)
{
try
{
File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\F" + e.Name);
}
catch (Exception ex)
{
File.WriteAllText(@"D:\levani\bussite.txt", "File copy ex: " + ex.ToString());
}
}
protected override void OnStop()
{
thread.Abort();
}