public void WriteChange(FileSystemEventArgs obj, string details)
        {
            var evt = new FileMonitorEvent()
            {
                ChangeType = ChangeTypeToChangeType(obj.ChangeType),
                Path       = obj.FullPath,
                Name       = obj.Name
            };
            string timestamp = DateTime.Now.ToString("O");

            var cmd = new SqliteCommand(SQL_INSERT, DatabaseManager.Connection, DatabaseManager.Transaction);

            cmd.Parameters.AddWithValue("@run_id", this.runId);
            cmd.Parameters.AddWithValue("@row_key", CryptoHelpers.CreateHash(obj.ToString() + timestamp + watcher.NotifyFilter.ToString() + obj.ChangeType.ToString()));
            cmd.Parameters.AddWithValue("@timestamp", timestamp);
            cmd.Parameters.AddWithValue("@path", obj.FullPath);
            cmd.Parameters.AddWithValue("@old_path", "");
            cmd.Parameters.AddWithValue("@name", obj.Name);
            cmd.Parameters.AddWithValue("@old_name", "");
            cmd.Parameters.AddWithValue("@change_type", ChangeTypeToChangeType(obj.ChangeType));
            cmd.Parameters.AddWithValue("@extended_results", details);
            cmd.Parameters.AddWithValue("@notify_filters", watcher.NotifyFilter.ToString());
            cmd.Parameters.AddWithValue("@serialized", JsonConvert.SerializeObject(evt));

            cmd.ExecuteNonQuery();
        }
        public void WriteChange(FileSystemEventArgs objIn)
        {
            if (objIn != null)
            {
                string timestamp = DateTime.Now.ToString("O", CultureInfo.InvariantCulture);

                using var cmd = new SqliteCommand(SQL_INSERT, DatabaseManager.Connection, DatabaseManager.Transaction);
                cmd.Parameters.AddWithValue("@run_id", this.RunId);
                cmd.Parameters.AddWithValue("@row_key", CryptoHelpers.CreateHash(objIn.ToString() + timestamp + watcher.NotifyFilter.ToString() + objIn.ChangeType.ToString()));
                cmd.Parameters.AddWithValue("@timestamp", timestamp);
                cmd.Parameters.AddWithValue("@path", objIn.FullPath);
                cmd.Parameters.AddWithValue("@old_path", "");
                cmd.Parameters.AddWithValue("@name", objIn.Name);
                cmd.Parameters.AddWithValue("@old_name", "");
                cmd.Parameters.AddWithValue("@change_type", ChangeTypeStringToChangeType(objIn.ChangeType.ToString()));
                cmd.Parameters.AddWithValue("@extended_results", "");
                cmd.Parameters.AddWithValue("@notify_filters", watcher.NotifyFilter.ToString());
                cmd.Parameters.AddWithValue("@serialized", JsonConvert.SerializeObject(objIn));
                FileSystemMonitorResult fileSystemObject = new FileSystemMonitorResult()
                {
                    evt    = objIn,
                    filter = watcher.NotifyFilter
                };

                cmd.ExecuteNonQuery();
            }
        }
        private void FileSystemWatcherConfig_Changed(object sender, FileSystemEventArgs e)
        {
            try
            {
                // Prevents a double firing, known issue for FileSystemWatcher
                fileSystemWatcherConfig.EnableRaisingEvents = false;

                // Wait until the file is accessible
                while (!Helper.instance.IsFileReady(e.FullPath))
                {
                    Console.WriteLine("File locked by another process");
                }

                if (MessageBox.Show("The config file has been modified by another program. Do you want to reload it?", "Reload",
                                    MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    buttonReload.PerformClick();
                }
            }
            catch (Exception ex)
            {
                Logger.WriteInformation <Helper>(ex.Message, e.ToString());
            }
            finally
            {
                fileSystemWatcherConfig.EnableRaisingEvents = true;
            }
        }
Esempio n. 4
0
        private static void OnFileChanged(object sender, FileSystemEventArgs e)
        {
            try
            {
                //Create a new object from the ImportData
                //class to process the incoming file
                ImportData id = new ImportData
                {
                    strFileName = e.FullPath
                };

                Thread t = new Thread(new ThreadStart(id.Import))
                {
                    Name = "DataImportThread"
                };
                t.Start();
            }
            catch (Exception)
            {
                Trace.WriteLineIf(bs.Enabled, DateTime.Now +
                                  " - An exception occurred while queuing file: ");
                Trace.Indent();
                Trace.WriteLineIf(bs.Enabled, DateTime.Now + " - " + e.ToString());
                Trace.Unindent();
            }
            finally
            {
                Trace.Flush();
            }
        }
Esempio n. 5
0
        // Define the event handlers.
        private void OnChanged(object source, FileSystemEventArgs e)
        {
            // @TODO Specify what is done when a file is changed, created, or deleted.
            //
            StringBuilder sb = new StringBuilder();

            sb.Append(e.ToString());
            // Logger.Log.Info(sb.ToString());
        }
Esempio n. 6
0
 public void OnChanged(object source, FileSystemEventArgs e)
 {
     Console.WriteLine("changed " + e.ToString() + source.ToString());
     this.richTextBox1.AppendText(DisplayString);
     //Program.MainForm.DisplayString = "changed";
     //Program.MainForm.richTextBox1.AppendText("changed");//onchanged 不在form中的时候的用法,会跨线程
     //Program.MainForm.Refresh();
     //文件改變後的代碼
 }
Esempio n. 7
0
        /*
         *      private static void openFile()
         *      {
         *              //append to existing file and create new file if it does not exist.
         *              try
         *              {
         *                      file = new StreamWriter(new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 512, false));
         *              }
         *              catch(DirectoryNotFoundException)
         *              {
         *                      Directory.CreateDirectory(logFile.Substring(0,logFile.LastIndexOf("\\")));
         *                      file = new StreamWriter(new FileStream(logFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 512, false));
         *              }
         *      }
         */

        private static void OnFileSizeChanged(object source, FileSystemEventArgs e)
        {
            //if the file has grown large, archive
            FileInfo fileInfo = new FileInfo(logFile);

            if (fileInfo.Exists)
            {
                lock (syncObject)
                {
                    if (fileInfo.Exists)
                    {
                        if (fileInfo.Length > fileSize)
                        {
                            try
                            {
                                if (File.Exists(logFile + "-" + archiveIndex.ToString()))
                                {
                                    File.Delete(logFile + "-" + archiveIndex.ToString());
                                }
                                fileInfo.MoveTo(logFile + "-" + archiveIndex.ToString());
                            }
                            catch
                            {
                                LogWriter.Write(LogLevel.Info, e.ToString());
                            }
                            ++archiveIndex;
                            if (archiveIndex > archiveLimit)
                            {
                                archiveIndex = 1;
                            }

                            watcher.Dispose();
                            watcher = new FileSystemWatcher();
                            int idx = logFile.LastIndexOf("\\");
                            watcher.Path         = logFile.Substring(0, idx);
                            watcher.NotifyFilter = NotifyFilters.Size;
                            watcher.Filter       = logFile.Substring(idx + 1);

                            // Add event handlers.
                            watcher.Changed += new FileSystemEventHandler(OnFileSizeChanged);

                            // Begin watching.
                            watcher.EnableRaisingEvents = true;
                        }
                    }
                }
            }
        }
        void OnFolderContentsUpdated(object sender, FileSystemEventArgs e)
        {
            if (e.FullPath != null)
            {
                string folder = System.IO.Path.GetDirectoryName(e.FullPath);
                if (string.Compare(folder, AppConfig.InstallationPath, true) == 0)
                {
                    // Ignore any changes related to DB3 journal files located in the installation path.
                    if (e.FullPath.ToLowerInvariant().EndsWith("db3-journal"))
                    {
                        return;
                    }
                }
            }

            if (InvokeRequired)
            {
                Invoke(new FileSystemEventHandler(OnFolderContentsUpdated), sender, e);
                return;
            }

            Logger.LogTrace("OnFolderContentsUpdated: " + e.ToString());
            _delayedExplore.Stop();

            try
            {
                if (sender is FileSystemWatcher)
                {
                    _delayedExplore.Start();
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
            }
        }
Esempio n. 9
0
 static void watch_Created(object sender, FileSystemEventArgs e)
 {
     Console.WriteLine("错误:" + e.ToString());
 }
Esempio n. 10
0
 public void OnChanged(object source, FileSystemEventArgs e)
 {
     Console.WriteLine("changed " + e.ToString() + source.ToString());
     //文件改變後的代碼
 }
 public void FileDeleted(object sender, FileSystemEventArgs e)
 {
     Console.WriteLine("DELETED: " + sender.ToString() + e.ToString());
 }
 public void ShipmentFileCreated(object sender, FileSystemEventArgs e)
 {
     Console.WriteLine("CREATED: " + sender.ToString() + e.ToString());
 }
Esempio n. 13
0
        /// <summary>
        /// Raises the created/changed/deleted event as FSEvent.
        /// </summary>
        /// <param name='source'>
        /// Source file system watcher.
        /// </param>
        /// <param name='e'>
        /// Reported changes.
        /// </param>
        public virtual void Handle(object source, FileSystemEventArgs e)
        {
            try {
                bool isDirectory = false;
                if (e.ChangeType == WatcherChangeTypes.Deleted)
                {
                    var  obj  = this.storage.GetObjectByLocalPath(this.fsFactory.CreateFileInfo(e.FullPath));
                    Guid guid = Guid.Empty;
                    if (obj != null)
                    {
                        isDirectory = obj.Type == MappedObjectType.Folder;
                        guid        = obj.Guid;
                    }

                    this.AddEventToList(e, guid, isDirectory);
                }
                else
                {
                    bool?check = this.fsFactory.IsDirectory(e.FullPath);
                    if (check != null)
                    {
                        isDirectory = (bool)check;
                        IFileSystemInfo fsInfo = isDirectory ? (IFileSystemInfo)this.fsFactory.CreateDirectoryInfo(e.FullPath) : (IFileSystemInfo)this.fsFactory.CreateFileInfo(e.FullPath);
                        Guid            uuid   = Guid.Empty;
                        try {
                            Guid?fsGuid = fsInfo.Uuid;
                            if (fsGuid != null)
                            {
                                uuid = (Guid)fsGuid;
                            }
                        } catch (Exception) {
                            uuid = Guid.Empty;
                        }

                        this.AddEventToList(e, uuid, isDirectory);
                    }
                }
            } catch (Exception ex) {
                Logger.Warn(string.Format("Processing file system event {0} produces exception => force crawl sync", e.ToString()), ex);
                this.queue.AddEvent(new StartNextSyncEvent(true));
            }
        }
Esempio n. 14
0
 // Watcher error
 private void OnWatcherError(object sender, FileSystemEventArgs e)
 {
     showErrorAndExit("FileSystemWatcher Error: " + e.ToString());
 }