c# - System.IO.FileSystemWatcher.CompletionStatusChanged 处的 System.ArgumentOutOfRangeException

标签 c# asp.net exception

我有一个代码可以监视特定文件夹中是否有任何文件丢失。在文件删除时,文件将被拾取以进行进一步处理。

最近我得到以下异常。我不确定是什么原因导致此异常或如何避免它,因为它是间歇性发生的并且无法复制。也不确定这是在文件删除时发生还是仅在观察者轮询文件夹时发生。

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/

相关文章:

c# - 如何在 SQLite 表的类中将字段标记为可为空?

asp.net - 如何获取当前页面的html?

c# - Redis 作为分布式缓存不执行?

asp.net - ASP.NET 的 MultiView 控件的适当用途是什么?

c++ - 无法在 `catch(const std::exception &ex)` 中捕获 std::invalid_argument

java - 为什么这段java代码没有捕获所有异常

android - 选择 Android Spinner 会导致 WindowManager$BadTokenException

c# - FindControl 不适用于绑定(bind)服务器标签,为什么?

c# - 在哪里设置变量,以便它们在回发时不为空?

c# - 在 winforms 中创建动态 UI