Пример #1
0
        private void DummyOutputWindow_Load(object sender, EventArgs e)
        {
            string filename = "log/console.log";

            if (File.Exists(filename))
            {
                FileInfo          info    = new FileInfo(filename);
                FileSystemWatcher watcher = new FileSystemWatcher("log");

                startReaderIndex = info.Length;

                //初始化监听
                watcher.BeginInit();
                //设置监听文件类型
                watcher.Filter = "*.*";
                //设置是否监听子目录
                watcher.IncludeSubdirectories = true;
                //设置是否启用监听?
                watcher.EnableRaisingEvents = true;
                //设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
                watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
                //设置监听的路径
                watcher.Path = "log";
                //注册创建文件或目录时的监听事件
                //watcher.Created += new FileSystemEventHandler(watch_created);
                //注册当指定目录的文件或者目录发生改变的时候的监听事件
                watcher.Changed += new FileSystemEventHandler(Watcher_Changed);
                //注册当删除目录的文件或者目录的时候的监听事件
                //watcher.Deleted += new FileSystemEventHandler(watch_deleted);
                //当指定目录的文件或者目录发生重命名的时候的监听事件
                //watcher.Renamed += new RenamedEventHandler(watch_renamed);
                //结束初始化
                watcher.EndInit();
            }
        }
Пример #2
0
        /// <summary>
        /// Executes the callback when the properties file changed on the disk.
        /// </summary>
        public void OnPropertiesFileChanged(Action callback)
        {
            // init file watcher
            if (_ConfigFileWatcher == null)
            {
                _ConfigFileWatcher = new FileSystemWatcher();
                _ConfigFileWatcher.BeginInit();
                _ConfigFileWatcher.Path   = ConfigFolder;
                _ConfigFileWatcher.Filter = Path.GetFileName(_ConfigFilepath);
                _ConfigFileWatcher.IncludeSubdirectories = false;
                _ConfigFileWatcher.NotifyFilter          = NotifyFilters.LastWrite;
                _ConfigFileWatcher.EnableRaisingEvents   = true;
                _ConfigFileWatcher.EndInit();
            }

            // Changed event may be invoked twice on single save due to possibility
            // of multiple write operations by editor used to change the file.
            // To prevent exceptions, delay parsing of properties file
            // also see https://failingfast.io/a-robust-solution-for-filesystemwatcher-firing-events-multiple-times/
            Timer timer = new Timer((state) =>
            {
                Log.Debug("Attempt reloading properties from file.");
                ParseProperties();
                callback.Invoke();
            }, null, Timeout.Infinite, Timeout.Infinite);

            _ConfigFileWatcher.Changed += new FileSystemEventHandler((sender, e) =>
            {
                Log.Information("Invoked changed event");
                timer.Change(500, Timeout.Infinite);
            });
        }
Пример #3
0
        public FileWatcher(string directory, string filter)
        {
            _dispatcher = Dispatcher.CurrentDispatcher;

            _watcher = new FileSystemWatcher();
            _watcher.BeginInit();
            _watcher.Path   = directory;
            _watcher.Filter = filter;
            _watcher.IncludeSubdirectories = false;
            _watcher.InternalBufferSize    = InitialBufferSize;

            _watcher.NotifyFilter
                = NotifyFilters.DirectoryName
                  | NotifyFilters.FileName
                  | NotifyFilters.LastWrite
                  | NotifyFilters.Attributes
                  | NotifyFilters.Security
                  | NotifyFilters.Size;

            // note: all events are on threadpool threads!
            _watcher.Created += onCreated;
            _watcher.Deleted += onDeleted;
            _watcher.Changed += onChanged;
            _watcher.Renamed += onRenamed;
            _watcher.Error   += onError;

            _watcher.EndInit();

            _watcher.EnableRaisingEvents = true;
        }
Пример #4
0
        public void Watch()
        {
            var fsw = new FileSystemWatcher(sourceDir);

            fsw.BeginInit();
            fsw.EnableRaisingEvents   = true;
            fsw.IncludeSubdirectories = true;
            fsw.EndInit();

            for (; ;)
            {
                if (IsBuildRequired(sourceDir, OutputFile))
                {
                    try
                    {
                        Build(sourceDir, OutputFile, repository).Wait();
                        Console.WriteLine("Build succeeded.");
                        var result = Run().Result;
                        Console.WriteLine("Exit code: {0}", result);
                        Util.StartProcess(Environment.CommandLine);
                        Environment.Exit(0);
                    }
                    catch (AggregateException e)
                    {
                        Console.WriteLine("Build failed.");
                        Console.WriteLine(e.InnerExceptions[0]);
                    }
                }
                Console.WriteLine("Waiting for changes in {0}", sourceDir);
                fsw.WaitForChanged(WatcherChangeTypes.All);
                Thread.Sleep(TimeSpan.FromMilliseconds(500));
            }
        }
Пример #5
0
        protected FileSystemWatcher CreateFileSystemWatcher(string directory)
        {
            //Console.WriteLine("Watcher creating {0}", directory);
            var watcher = new FileSystemWatcher()
            {
                Path                  = directory,
                NotifyFilter          = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
                Filter                = FileFilter,
                IncludeSubdirectories = true
            };

            watcher.BeginInit();

            watcher.Changed += OnModified;
            watcher.Created += OnModified;
            watcher.Deleted += OnModified;
            watcher.Renamed += OnModified;
            watcher.Error   += WatcherOnError;

            watcher.EndInit();
            watcher.EnableRaisingEvents = true;

            //Console.WriteLine("Watcher created {0}", directory);
            return(watcher);
        }
Пример #6
0
 /// <summary>
 /// 监听服务接收的指令信息
 /// </summary>
 public static void FileMonitoringCommand(FileSystemWatcher curWatcher)
 {
     //FileStream fs = new FileStream(@"E:\log.txt", FileMode.OpenOrCreate, FileAccess.Write);
     //StreamWriter sw = new StreamWriter(fs);
     //sw.BaseStream.Seek(0, SeekOrigin.End);
     //sw.WriteLine("WindowsService: Service Started" + DateTime.Now.ToString() + AppDomain.CurrentDomain.BaseDirectory + "\n");
     //sw.Flush();
     //sw.Close();
     //fs.Close();
     try
     {
         curWatcher = new FileSystemWatcher();
         curWatcher.BeginInit();
         curWatcher.Filter = "App.config";//只监视config文件的修改动作(例:fsw.Filter = "*.txt|*.doc|*.jpg")
         curWatcher.IncludeSubdirectories = false;
         curWatcher.Path = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");
         LogerHelper.WriteOperateLog("监控文件路径:" + curWatcher.Path);
         curWatcher.Changed            += new FileSystemEventHandler(OnFileChanged);
         curWatcher.EnableRaisingEvents = true;//开启监控
         curWatcher.EndInit();
     }
     catch (Exception ex)
     {
         LogerHelper.WriteErrorLog(ex);
     }
 }
        private static void CreateExternConfigFileWatcher(string filePath)
        {
            lock (Lockobject)
            {
                DisposeExternConfigFileWatcher();

                FExternConfigFileWatcher = new FileSystemWatcher();
                try
                {
                    FExternConfigFileWatcher.BeginInit();
                    try
                    {
                        FExternConfigFileWatcher.Path                = Path.GetDirectoryName(filePath);
                        FExternConfigFileWatcher.Filter              = Path.GetFileName(filePath);
                        FExternConfigFileWatcher.NotifyFilter        = NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.CreationTime | NotifyFilters.Size;
                        FExternConfigFileWatcher.Changed            += OnConfigFileChanged;
                        FExternConfigFileWatcher.Created            += OnConfigFileChanged;
                        FExternConfigFileWatcher.Deleted            += OnConfigFileChanged;
                        FExternConfigFileWatcher.EnableRaisingEvents = true;
                    }
                    finally
                    {
                        FExternConfigFileWatcher.EndInit();
                    }
                }
                catch (Exception ex)
                {
                    DisposeExternConfigFileWatcher();
                    RIExceptionManager.Publish(ex, string.Format("ReflectInsightConfig: Cannot create FileSystemWatcher for file: {0}", filePath));
                }
            }
        }
        private void StartWatchingDesiredLocalConfigChanges(string watchFolder)
        {
            try
            {
                this.watcher = new FileSystemWatcher();
                watcher.Path = watchFolder;

                /* Watch for changes in LastWrite times, and the renaming of files or directories. */
                watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
                // Only watch xml files.
                watcher.Filter = "*.xml";

                // Add event handlers.
                watcher.Changed += new FileSystemEventHandler(OnEditingChanges);
                watcher.Created += new FileSystemEventHandler(OnNonEditingChanges);
                watcher.Deleted += new FileSystemEventHandler(OnNonEditingChanges);
                watcher.Renamed += new RenamedEventHandler(OnNonEditingChanges);


                // Send events.
                watcher.EnableRaisingEvents = true;

                watcher.BeginInit();
            }
            catch (Exception exception)
            {
                this.logger.ErrorException(string.Format("Exception while trying to watch folder {0} for changes", watchFolder), exception);
            }
        }
Пример #9
0
 /// <summary>
 /// 初始化监听
 /// </summary>
 /// <param name="StrWarcherPath">需要监听的目录</param>
 /// <param name="FilterType">需要监听的文件类型(筛选器字符串)</param>
 /// <param name="IsEnableRaising">是否启用监听</param>
 /// <param name="IsInclude">是否监听子目录</param>
 private static void WatcherStrat(string StrWarcherPath, string FilterType, bool IsEnableRaising, bool IsInclude)
 {
     //初始化监听
     watcher.BeginInit();
     //设置监听文件类型
     watcher.Filter = FilterType;
     //设置是否监听子目录
     watcher.IncludeSubdirectories = IsInclude;
     //设置是否启用监听?
     watcher.EnableRaisingEvents = IsEnableRaising;
     //设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
     watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
     //设置监听的路径
     watcher.Path = StrWarcherPath;
     //注册创建文件或目录时的监听事件
     watcher.Created += new FileSystemEventHandler(watch_created);
     //注册当指定目录的文件或者目录发生改变的时候的监听事件
     //watcher.Changed += new FileSystemEventHandler(watch_changed);
     //注册当删除目录的文件或者目录的时候的监听事件
     // watcher.Deleted += new FileSystemEventHandler(watch_deleted);
     //当指定目录的文件或者目录发生重命名的时候的监听事件
     //watcher.Renamed += new RenamedEventHandler(watch_renamed);
     //结束初始化
     watcher.EndInit();
 }
Пример #10
0
        private void WhatchPluginFolder()
        {
            var thread = new Thread(() =>
            {
                var watcher = new FileSystemWatcher();
                //初始化监听
                watcher.BeginInit();
                //设置监听文件类型
                watcher.Filter = "*.dll";
                //设置是否监听子目录
                watcher.IncludeSubdirectories = false;
                //设置是否启用监听?
                watcher.EnableRaisingEvents = true;
                //设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
                watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
                //设置监听的路径
                watcher.Path = mPluginDir;
                //注册创建文件或目录时的监听事件
                watcher.Changed += OnPluginsChanged;
                //结束初始化
                watcher.EndInit();
            });

            thread.Start();
        }
Пример #11
0
 public void Init()
 {
     fileSystemWatcher.BeginInit();
     fileSystemWatcher.Created += new FileSystemEventHandler(OnFileCreate);
     fileSystemWatcher.Changed += new FileSystemEventHandler(OnFileChange);
     fileSystemWatcher.Deleted += new FileSystemEventHandler(OnFileDelete);
     fileSystemWatcher.Renamed += new RenamedEventHandler(OnFileRenamed);
     fileSystemWatcher.EndInit();
 }
Пример #12
0
 /// <summary>
 /// Starts to watch the directory.
 /// </summary>
 /// <param name="dirPath">The directory path.</param>
 public void StartHandleDirectory(string dirPath)
 {
     m_dirWatcher = new FileSystemWatcher();
     m_dirWatcher.BeginInit();
     m_dirWatcher.Path = dirPath;
     m_dirWatcher.EnableRaisingEvents = true;
     m_dirWatcher.Created            += CreateFile;
     m_dirWatcher.EndInit();
 }
 public UpdateChecker(string path)
 {
     FileWatcher.Path                = path;
     FileWatcher.NotifyFilter        = NotifyFilters.CreationTime;
     FileWatcher.Filter              = "*.csv";
     FileWatcher.Created            += new FileSystemEventHandler(OnCreated);
     FileWatcher.EnableRaisingEvents = true;
     FileWatcher.BeginInit();
 }
Пример #14
0
 private void InitializeWatcher(string directory, ISynchronizeInvoke synchronizationProvider)
 {
     watcher = new FileSystemWatcher(directory, "*.dll");
     watcher.BeginInit();
     watcher.SynchronizingObject = synchronizationProvider;
     watcher.Created            += OnFileCreated;
     watcher.EnableRaisingEvents = true;
     watcher.EndInit();
 }
Пример #15
0
 public void Start()
 {
     if (watcher == null)
     {
         watcher = new FileSystemWatcher(GetInputFolder());
         watcher.BeginInit();
         watcher.NotifyFilter        = NotifyFilters.Attributes | NotifyFilters.LastWrite;
         watcher.Changed            += watcher_Changed;
         watcher.Created            += watcher_Changed;
         watcher.EnableRaisingEvents = true;
         watcher.EndInit();
     }
 }
Пример #16
0
        private FileSystemWatcher InitWatcher(string path, Action <string> onShortcutAdded)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.BeginInit();
            watcher.Path   = path;
            watcher.Filter = "*.lnk";
            watcher.IncludeSubdirectories = false;
            watcher.NotifyFilter          = NotifyFilters.FileName;
            watcher.EnableRaisingEvents   = true;
            watcher.Created += new FileSystemEventHandler((sender, e) => onShortcutAdded(e.FullPath));
            watcher.EndInit();
            return(watcher);
        }
Пример #17
0
        /// <summary>
        /// 添加监听树节点
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        private static KeyValuePair <string, FileSystemWatcher> CreateListener(FileInfo info)
        {
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.BeginInit();
            watcher.Path   = info.DirectoryName;
            watcher.Filter = info.Name;
            watcher.IncludeSubdirectories = false;
            watcher.EnableRaisingEvents   = true;
            watcher.NotifyFilter          = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Size;
            watcher.Changed += new FileSystemEventHandler(ConfigChangeListener);
            watcher.EndInit();
            return(new KeyValuePair <string, FileSystemWatcher>(info.FullName, watcher));
        }
Пример #18
0
        public Time2RunSrv(string srvName, string srvDesc)
        {
            this.srvName = srvName;
            this.srvDesc = srvDesc;
            watch.BeginInit();
            watch.Path                = AppDomain.CurrentDomain.BaseDirectory;
            watch.NotifyFilter        = NotifyFilters.LastWrite;
            watch.EnableRaisingEvents = true;
            string fileName = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            watch.Filter = fileName.Substring(fileName.LastIndexOf('\\') + 1);
            watch.IncludeSubdirectories = false;
            watch.Changed += watch_Changed;
            watch.EndInit();
        }
Пример #19
0
        private void WalkDirectoryTree(System.IO.DirectoryInfo root, int depth = 1)
        {
            System.IO.FileInfo[]      files   = null;
            System.IO.DirectoryInfo[] subDirs = null;
            files = root.GetFiles("*.*");
            if (files != null)
            {
                foreach (System.IO.FileInfo fi in files)
                {
                    string ext = fi.Extension.ToLower().Replace('.', ' ').Trim();
                    if (!UtilitiesManager.Instance.mimeTypes.ContainsKey(ext))
                    {
                        LoggingFacility.GetConfigurationDefault().WriteLine("unsupported file type " + fi.FullName);
                        continue;
                    }

                    string filekey = "";

                    fi.DirectoryName
                    .Split(Path.DirectorySeparatorChar)
                    .Reverse()
                    .Take(depth)
                    .Reverse()
                    .ToList()
                    .ForEach(s => filekey += s + Path.DirectorySeparatorChar);

                    this.content.Add(filekey + fi.Name, new ContentFile(fi.FullName,
                                                                        UtilitiesManager.Instance.mimeTypes[ext]));
                }

                // Now find all the subdirectories under this directory.
                subDirs = root.GetDirectories();

                foreach (System.IO.DirectoryInfo dirInfo in subDirs)
                {
                    FileSystemWatcher fsw = new FileSystemWatcher(dirInfo.FullName);
                    fsw.Changed            += HandleChanged;
                    fsw.Created            += HandleCreated;
                    fsw.Deleted            += HandleDeleted;
                    fsw.EnableRaisingEvents = true;
                    fsw.BeginInit();
                    fswl.Add(fsw);

                    // Resursive call for each subdirectory.
                    WalkDirectoryTree(dirInfo, depth + 1);
                }
            }
        }
Пример #20
0
        /// <summary>
        /// Creates new instance
        /// </summary>
        /// <param name="directoryPath"></param>
        /// <param name="eventDispatcher"></param>
        public LocalFileSystemWatcher(string directoryPath, IEventDispatcher eventDispatcher)
        {
            Ensure.Condition.DirectoryExists(directoryPath, "directoryPath");
            Ensure.NotNull(eventDispatcher, "eventDispatcher");
            this.watcher         = new FileSystemWatcher(directoryPath);
            this.eventDispatcher = eventDispatcher;

            watcher.BeginInit();
            watcher.EnableRaisingEvents   = true;
            watcher.IncludeSubdirectories = true;
            watcher.NotifyFilter          = NotifyFilters.FileName | NotifyFilters.DirectoryName;
            watcher.Created += OnFileCreated;
            watcher.Deleted += OnFileDeleted;
            watcher.Renamed += OnFileRenamed;
            watcher.EndInit();
        }
Пример #21
0
        /// <summary>
        /// Запустить сканирование
        /// </summary>
        /// <param name="ScanningDir"></param>
        /// <param name="MadeChanges"></param>
        /// <returns></returns>
        public override bool Start(string ScanningDir)
        {
            LastException = null;

            if (State == enScanningThreadState.Worked)
            {
                // Поток в данный момент работает
                return(false);
            }

            if (!Directory.Exists(ScanningDir))
            {
                return(false);
            }

            ScanningPath = ScanningDir;

            // Пытаемся запустить все все сканеры файлов
            List <string> ScannersPaths = m_FileScanners.Keys.ToList();

            for (int i = 0; i < ScannersPaths.Count;)
            {
                CFileScanner scanner = m_FileScanners[ScannersPaths[i]];
                scanner.ScanningPath = ScannersPaths[i];
                if (scanner.Start(scanner.ScanningPath))
                {
                    i++;
                }
                else
                {       // Запустить сканер почему-то не получилось
                    lock (DBManagerApp.m_AppSettings.m_SettingsSyncObj)
                        DBManagerApp.m_AppSettings.m_Settings.dictFileScannerSettings.Remove(scanner.ScanningPath);

                    m_FileScanners.Remove(ScannersPaths[i]);
                    ScannersPaths.RemoveAt(i);
                }
            }

            m_PathWatcher.BeginInit();
            m_PathWatcher.Path = ScanningDir;
            m_PathWatcher.EnableRaisingEvents = true; // Эту операцию нужно делать после запуска всех сканеров файлов
            m_PathWatcher.EndInit();

            State = enScanningThreadState.Worked;

            return(true);
        }
Пример #22
0
        public void watchFunc()
        {
            WriteToLog("开始卸载原来的监控……");
            try {
                for (int i = 0; i < watchpaths.Length; i++)
                {
                    watcherlist[i].Dispose();
                    watcherlist.RemoveAt(i);
                }
            } catch (Exception e) {
                WriteToLog("卸载出现问题:" + e.ToString());
            }
            WriteToLog("卸载完成。");

            int pathcount = 0;

            while (pathcount < watchpaths.Length)
            {
                //Console.WriteLine(watchpaths[pathcount]);
                WriteToLog(watchpaths[pathcount]);

                try
                {
                    FileSystemWatcher curWatcher = new FileSystemWatcher();
                    curWatcher.BeginInit();
                    curWatcher.IncludeSubdirectories = true;
                    curWatcher.Path   = @watchpaths[pathcount];
                    curWatcher.Filter = "*.rar";
                    //curWatcher.Changed += new FileSystemEventHandler(OnFileChanged);
                    curWatcher.Created += new FileSystemEventHandler(OnFileCreated);
                    //curWatcher.Deleted += new FileSystemEventHandler(OnFileDeleted);
                    //curWatcher.Renamed += new RenamedEventHandler(OnFileRenamed);
                    curWatcher.EnableRaisingEvents = true;
                    curWatcher.EndInit();
                    watcherlist.Add(curWatcher);
                    pathcount++;
                }
                catch (Exception e)
                {
                    //Console.WriteLine("监控路径失败!");
                    WriteToLog("监控路径失败!" + e.ToString());
                    msgToFQ("您好,管理员,小月月在监控" + @watchpaths[pathcount] + @"时发生错误:\n" + e.ToString());
                    pathcount++;
                }
            }
        }
Пример #23
0
        private void SourceFileSelector_Load(object sender, EventArgs e)
        {
            SourceFolderControl.Description    = "Select a folder containing COLLADA (.dae) files to convert";
            SourceFolderControl.FolderChanged += OnFolderChanged;

            ColladaFilesToolTip.SetToolTip(this.FilterTextBox,
                                           "Regular Expression that filters the files \n" +
                                           "displayed from the selected folder.");

            m_FileWatcher.BeginInit();

            m_FileWatcher.Created += OnFileAddedOrRemoved;
            m_FileWatcher.Deleted += OnFileAddedOrRemoved;
            m_FileWatcher.Renamed += OnFileRenamed;

            m_FileWatcher.EndInit();
        }
Пример #24
0
        public IDisposable NotifyChange(ContentPath contentPath, Action <ContentPath> onChanged)
        {
            var fsPath           = this.GetFileSystemPath(contentPath);
            var directoryToWatch = fsPath = (Directory.Exists(fsPath)) ? fsPath : Path.GetDirectoryName(fsPath);
            var w = new FileSystemWatcher(directoryToWatch);

            w.BeginInit();
            FileSystemEventHandler changed = (s, e) =>
            {
                onChanged(contentPath);
            };

            w.Changed += changed;
            w.EndInit();
            w.EnableRaisingEvents = true;
            return(w);
        }
Пример #25
0
 public void SetPaths(List <ListenPath> lp)
 {
     foreach (ListenPath path in lp)
     {
         foreach (String f in filter)
         {
             FileSystemWatcher watcher = new FileSystemWatcher();
             watcher.BeginInit();
             watcher.Path = path.Path;
             watcher.EnableRaisingEvents = true;
             watcher.Filter   = f;
             watcher.Created += new FileSystemEventHandler(WatchCreated);
             watcher.EndInit();
             watchers.Add(watcher);
         }
     }
 }
Пример #26
0
        private static void AddWatch(object sender, AssemblyLoadEventArgs args)
        {
            Console.WriteLine("监视到了加载程序集:" + args.LoadedAssembly.CodeBase);
            FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
            string            text  = args.LoadedAssembly.CodeBase.Replace("file:///", "");
            string            text2 = text.Substring(text.LastIndexOf('/') + 1);
            string            path  = text.Substring(0, text.Length - text2.Length);

            fileSystemWatcher.BeginInit();
            fileSystemWatcher.Path                  = path;
            fileSystemWatcher.NotifyFilter          = (NotifyFilters.FileName | NotifyFilters.Attributes | NotifyFilters.LastWrite);
            fileSystemWatcher.EnableRaisingEvents   = true;
            fileSystemWatcher.Filter                = text2;
            fileSystemWatcher.IncludeSubdirectories = false;
            fileSystemWatcher.Changed              += new FileSystemEventHandler(MainUtil.watch_Changed);
            fileSystemWatcher.Deleted              += new FileSystemEventHandler(MainUtil.watch_Deleted);
            fileSystemWatcher.EndInit();
        }
Пример #27
0
        private static void AddWatch(object sender, AssemblyLoadEventArgs args)
        {
            Console.WriteLine("监视到了加载程序集:" + args.LoadedAssembly.CodeBase);
            FileSystemWatcher watch       = new FileSystemWatcher();
            string            fileAbsPath = args.LoadedAssembly.CodeBase.Replace(@"file:///", "");
            string            fileName    = fileAbsPath.Substring(fileAbsPath.LastIndexOf('/') + 1);
            string            dir         = fileAbsPath.Substring(0, fileAbsPath.Length - fileName.Length);

            watch.BeginInit();
            watch.Path                  = dir;
            watch.NotifyFilter          = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.Attributes;
            watch.EnableRaisingEvents   = true;
            watch.Filter                = fileName;
            watch.IncludeSubdirectories = false;
            watch.Changed              += watch_Changed;
            watch.Deleted              += watch_Deleted;
            watch.EndInit();
        }
Пример #28
0
        private FileSystemWatcher CreateWatcher(String directory)
        {
            String parentDirectory = Path.GetDirectoryName(directory);

            if (!Directory.Exists(parentDirectory))
            {
                return(null);
            }
            FileSystemWatcher watcher = new FileSystemWatcher(parentDirectory);

            watcher.BeginInit();
            watcher.Deleted              += Watcher_Deleted;
            watcher.NotifyFilter          = NotifyFilters.DirectoryName;
            watcher.IncludeSubdirectories = false;
            watcher.EnableRaisingEvents   = true;
            watcher.EndInit();
            return(watcher);
        }
        public void AttachToSystem()
        {
#if MONO && DEBUG
            return;
#endif
            if (_fileSystemWatcher != null)
            {
                return;
            }
            _fileSystemWatcher = new FileSystemWatcher(ProjectPath);
            _fileSystemWatcher.BeginInit();
            _fileSystemWatcher.Created += FileSystemWatcherCreated;
            _fileSystemWatcher.Renamed += FileSystemWatcherRenamed;
            _fileSystemWatcher.Deleted += FileSystemWatcherDeleted;
            _fileSystemWatcher.IncludeSubdirectories = true;
            _fileSystemWatcher.NotifyFilter          = NotifyFilters.DirectoryName | NotifyFilters.FileName;
            _fileSystemWatcher.EnableRaisingEvents   = true;
            _fileSystemWatcher.EndInit();
        }
Пример #30
0
 public static void WatcherStratBchao(string StrWarcherPath, string FilterType, bool IsEnableRaising, bool IsInclude)
 {
     //初始化监听
     watcherb.BeginInit();
     watcherb.InternalBufferSize = 5 * 1024 * 1024;
     //设置监听文件类型
     watcherb.Filter = FilterType;
     //设置是否监听子目录
     watcherb.IncludeSubdirectories = IsInclude;
     //设置是否启用监听?
     watcherb.EnableRaisingEvents = IsEnableRaising;
     //设置需要监听的更改类型(如:文件或者文件夹的属性,文件或者文件夹的创建时间;NotifyFilters枚举的内容)
     watcherb.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
     //设置监听的路径
     watcherb.Path = StrWarcherPath;
     //注册创建文件或目录时的监听事件
     watcherb.Created += new FileSystemEventHandler(watch_createdbchao);
     watcherb.EndInit();
 }