C# FileSystemWatcher异常-已达到网络BIOS命令限制

C# FileSystemWatcher异常-已达到网络BIOS命令限制,c#,.net,windows-services,windows-server-2008,filesystemwatcher,C#,.net,Windows Services,Windows Server 2008,Filesystemwatcher,我在windows 2008 Server上有一个基于C#(.Net framework 3.5)的windows服务应用程序,它正在远程服务器/网络路径上监视22个文件夹。偶尔我会在日志中看到一个异常 System.ComponentModel.Win32异常:已达到网络BIOS命令限制 我已尝试将最大值(64K)增加,但对异常没有任何影响 我对它的定义如下: 在班级层面: FileSystemWatcher fsw; 在初始化方法中 fsw = new FileSystemWatcher(

我在windows 2008 Server上有一个基于C#(.Net framework 3.5)的windows服务应用程序,它正在远程服务器/网络路径上监视22个文件夹。偶尔我会在日志中看到一个异常

System.ComponentModel.Win32异常:已达到网络BIOS命令限制

我已尝试将最大值(64K)增加,但对异常没有任何影响

我对它的定义如下:

在班级层面:

FileSystemWatcher fsw;
在初始化方法中

fsw = new FileSystemWatcher("FolderUNC");
fsw.IncludeSubdirectories = false;
//m_fsw.InternalBufferSize = 65536; -- Commented out to default 8K
fsw.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.Size;
fsw.Renamed += new RenamedEventHandler(OnChanged);
fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnChanged);
fsw.Error += new ErrorEventHandler(OnFileSystemWatcherError);
fsw.EnableRaisingEvents = true;   
我还要确保在出现错误或停止服务时处置
FileSystemWatcher
对象。像

if (fsw != null)
    fsw.Dispose();
我尝试了以下操作,但由于我的服务器是Windows server 2008,因此我没有
MaxCmds
MaxMpxCT
等的注册表项。我已经研究了Stackoverflow的相关问题,如和,但似乎没有一个解决问题

感谢您在解决问题时提供的任何帮助或指导

编辑:我还添加了一个类似的,关于如何提高Windows 2008 server上的
MaxCMDs
的限制


编辑2:我在承载windows服务的服务器上为MaxCmds和MaxMpxCt创建了密钥,并将它们的值设置为
250
,但没有任何效果。我看了这篇文章

如果您没有这些键,在Windows注册表上,您可以将其创建为DWORD,其值介于0和65535之间


HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters

下面是一个文件系统轮询器示例。我想知道它在您的场景中的表现

void Main()
{
    var watcher = new DirectoryWatcher("C:\\test\\", new TimeSpan(0, 0, 1));
    watcher.Notification += (sender, args) => 
        Console.WriteLine(string.Format("{0} was {1}", args.FileName, args.NotificationType));

    watcher.Start();

    Console.WriteLine("Press enter to stop.");
    Console.ReadLine();

    watcher.Stop();
}

public class DirectoryWatcher
{
    public DirectoryWatcher(string directory, TimeSpan pollingFrequency)
    {
        this.Directory = directory;
        this.PollingFrequency = pollingFrequency;
    }

    public string Directory { get; set; }
    public TimeSpan PollingFrequency { get; set; }

    public System.Threading.Timer Timer { get; set; }

    private long ProcessCount;

    public void Start()
    {
        this.Timer = new Timer(Tick, null, 0, (int)PollingFrequency.TotalMilliseconds);
    }

    public void Stop()
    {
        this.Timer.Dispose();
    }

    DirectoryState previousState;

    private void Tick(object stateInfo)
    {
        if(Interlocked.Increment(ref ProcessCount) == 1)
        {
            try
            {
                if(previousState == null)
                {
                    // First Run.
                    // Tell listeners about files that already exist in the directory.
                    previousState = new DirectoryState(this.Directory);

                    foreach(var file in previousState.Files)
                    {
                        RaiseNotification(file.Key, DirectoryWatcherNotifiction.StartUp);
                    }
                }
                else
                {
                    var currentState = new DirectoryState(this.Directory);
                    NotifyChanges(previousState, currentState);
                    previousState = currentState;
                }
            }
            catch(Exception ex)
            {
                if(this.Error != null)
                {
                    this.Error(this, new ErrorEventArgs(ex));
                }
            }
        }
        Interlocked.Decrement(ref ProcessCount);
    }

    private void NotifyChanges(DirectoryState previous, DirectoryState current)
    {
        // Notify changes and deletions.
        foreach(string fileName in previous.Files.Keys)
        {
            if(current.Files.ContainsKey(fileName))
            {
                if(!current.Files[fileName].Equals(previous.Files[fileName]))
                {
                    RaiseNotification(fileName, DirectoryWatcherNotifiction.Changed);
                }
            }
            else
            {
                RaiseNotification(fileName, DirectoryWatcherNotifiction.Deleted);
            }
        }

        // Notify new files.
        foreach(string fileName in current.Files.Keys)
        {
            if(!previous.Files.ContainsKey(fileName))
            {
                RaiseNotification(fileName, DirectoryWatcherNotifiction.Created);
            }
        }
    }

    private void RaiseNotification(string fileName, DirectoryWatcherNotifiction notificationType)
    {
        if(this.Notification != null)
        {
            this.Notification(this, new DirectoryWatcherEventArgs(fileName, notificationType));
        }
    }

    public EventHandler<DirectoryWatcherEventArgs> Notification { get; set; }
    public EventHandler<ErrorEventArgs> Error { get; set; }
}

public class DirectoryWatcherEventArgs
{
    public DirectoryWatcherEventArgs(string fileName, DirectoryWatcherNotifiction notificationType)
    {
        this.FileName = fileName;
        this.NotificationType = notificationType;
    }

    public string FileName { get; set; }
    public DirectoryWatcherNotifiction NotificationType { get; set; }
}

public enum DirectoryWatcherNotifiction
{
    StartUp,
    Deleted,
    Changed,
    Created
}

public class DirectoryState
{
    private DirectoryState()
    {
        this.Files = new Dictionary<string, DirectoryFileInfo>();
    }

    public DirectoryState(string directory) 
        : this()
    {
        this.DirectoryName = directory;

        foreach(var file in Directory.EnumerateFiles(directory))
        {
            this.Files.Add(file, new DirectoryFileInfo(file));
        }
    }

    public string DirectoryName { get; set; }
    public Dictionary<string, DirectoryFileInfo> Files { get; set; }
}

public class DirectoryFileInfo
{
    public DirectoryFileInfo(string fileName)
    {   
        var info = new FileInfo(fileName);
        this.LastWriteTime = info.LastWriteTime;
        this.FileSize = info.Length;
    }

    public DateTime LastWriteTime { get; set; }
    public long FileSize { get; set; }

    public bool Equals(DirectoryFileInfo other)
    {
        return this.LastWriteTime == other.LastWriteTime && this.FileSize == other.FileSize;
    }
}
void Main()
{
var-watcher=newdirectorywatcher(“C:\\test\\”,新的时间跨度(0,0,1));
watcher.Notification+=(发送方,参数)=>
WriteLine(string.Format(“{0}是{1}”,args.FileName,args.NotificationType));
watcher.Start();
控制台。WriteLine(“按enter键停止”);
Console.ReadLine();
watcher.Stop();
}
公共类DirectoryWatcher
{
public DirectoryWatcher(字符串目录、TimeSpan轮询频率)
{
this.Directory=目录;
this.PollingFrequency=PollingFrequency;
}
公共字符串目录{get;set;}
公共时间间隔轮询频率{get;set;}
public System.Threading.Timer计时器{get;set;}
私有长进程计数;
公开作废开始()
{
this.Timer=new Timer(滴答、null、0,(int)PollingFrequency.total毫秒);
}
公共停车场()
{
this.Timer.Dispose();
}
目录状态先前状态;
私有无效勾号(对象状态信息)
{
if(联锁增量(参考进程计数)==1)
{
尝试
{
if(previousState==null)
{
//第一轮。
//告诉侦听器目录中已经存在的文件。
previousState=新目录状态(this.Directory);
foreach(previousState.Files中的var文件)
{
RaiseNotification(file.Key,directoryWatcherNotification.StartUp);
}
}
其他的
{
var currentState=new DirectoryState(this.Directory);
通知更改(先前状态、当前状态);
previousState=当前状态;
}
}
捕获(例外情况除外)
{
if(this.Error!=null)
{
this.Error(this,newerrorEventArgs(ex));
}
}
}
联锁。减量(参考进程计数);
}
私有void NotifyChanges(DirectoryState先前,DirectoryState当前)
{
//通知更改和删除。
foreach(previous.Files.Keys中的字符串文件名)
{
if(当前.Files.ContainsKey(文件名))
{
如果(!current.Files[fileName].Equals(previous.Files[fileName]))
{
RaiseNotification(文件名,directoryWatcherNotification.Changed);
}
}
其他的
{
RaiseNotification(文件名,directoryWatcherNotification.Deleted);
}
}
//通知新文件。
foreach(当前.Files.Keys中的字符串文件名)
{
如果(!previous.Files.ContainsKey(文件名))
{
RaiseNotification(文件名,directoryWatcherNotification.Created);
}
}
}
private void RaiseNotification(字符串文件名、DirectoryWatcherNotificationType)
{
如果(this.Notification!=null)
{
this.Notification(this,new DirectoryWatcherEventArgs(fileName,notificationType));
}
}
公共事件处理程序通知{get;set;}
公共事件处理程序错误{get;set;}
}
公共类DirectoryWatcherEventArgs
{
public DirectoryWatcherEventArgs(字符串文件名,DirectoryWatcherNotificationType)
{
this.FileName=文件名;
this.NotificationType=NotificationType;
}
公共字符串文件名{get;set;}
public DirectoryWatcherNotificationType{get;set;}
}
public enum directoryWatcherNotification
{
启动,
删除,
改变,
创建
}
公共类目录状态
{
私有目录状态()
{
this.Files=新字典();
}
公共目录状态(字符串目录)
:此()
{
this.DirectoryName=目录;
foreach(目录中的var文件。枚举文件(目录))
{
添加(文件,新目录文件信息(文件));
}
}
公共字符串目录名{get;set;}
公共字典文件{get;set;}
}
公共类DirectoryFileInfo
{
public DirectoryFileInfo(字符串文件名)