我有一个代码可以监视特定文件夹中是否有任何文件丢失。在文件删除时,文件将被拾取以进行进一步处理。
最近我得到以下异常。我不确定是什么原因导致此异常或如何避免它,因为它是间歇性发生的并且无法复制。也不确定这是在文件删除时发生还是仅在观察者轮询文件夹时发生。
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ArgumentOutOfRangeException
Length cannot be less than zero.
Parameter name: length
Stack:
at System.String.CtorCharPtrStartLength(Char*, Int32, Int32)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32, UInt32, System.Threading.NativeOverlapped*)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32, UInt32, System.Threading.NativeOverlapped*)
下面是我的示例代码:
{
// Code goes here
_watcher = new FileSystemWatcher();
_watcher.Path = _scanDirectory;
_watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
if (_watchSubTree)
{
_watcher.IncludeSubdirectories = true;
}
// Add event handlers.
_watcher.Created += new FileSystemEventHandler(OnCreated);
_watcher.Renamed += new RenamedEventHandler(OnRenamed);
}
最佳答案
您应该创建一个控制台应用程序来托管您的代码。由于生命周期问题,此服务器端代码不应成为 ASP.Net 应用程序的一部分。
尝试检查从控制台应用程序运行代码时问题是否仍然存在。还要检查运行程序的用户帐户是否具有对指定路径的文件系统访问权限。
关于c# - System.IO.FileSystemWatcher.CompletionStatusChanged 处的 System.ArgumentOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20679352/