Filesystemwatcher作为计划任务 [英] Filesystemwatcher as schedule task

查看:80
本文介绍了Filesystemwatcher作为计划任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我开发了一个filesystemWatcher控制台应用程序,当我运行它时它会捕获文件更改但是当我必须将其部署为时间表时任务,我无法让它捕获文件更改,因为观察者运行一秒钟,如果文件已经存在,它将无法捕获更改。



可以任何人请帮助我完成这个任务我想让这个FileSystemWatcher类来拾取创建或修改的文件,并将其路径和详细信息放在表中。



这是我的代码:

Hi All,

I have developed a filesystemWatcher console application, when I am running it its catching the file changes but when I have to deploy this as a schedule task, I am unable to make it catch the file changes because watcher runs for a second maybe if the file is already there it won't catch the changes.

Can anybody please help me in achieving this task I want to make this FileSystemWatcher class to pickup the created or modified files and put its path and details in a Table.

Here is my code:

public class Watcher
    {
        public static void Main(string[] args)
        {
            Run(args);
        }

        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Run(string[] args)
        {
            // If a directory is not specified, exit program.
            if (args.Length < 1)
            {
                // Display the proper way to call the program.
                Console.WriteLine("Usage: Watcher.exe (directory)");
                return;
            }

            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = args[0];
            /* Watch for changes in LastAccess and LastWrite times, and
               the renaming of files or directories. */
            watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
               | NotifyFilters.FileName | NotifyFilters.Size;// | NotifyFilters.DirectoryName;
            // Only watch text files.
            watcher.Filter = "*.*";

            // Add event handlers.
            watcher.Changed += new FileSystemEventHandler(OnChanged);
            watcher.Created += new FileSystemEventHandler(OnCreated);
            watcher.Renamed += new RenamedEventHandler(OnRenamed);

            // Begin watching.
            watcher.EnableRaisingEvents = true;
            watcher.IncludeSubdirectories = true;

            // Wait for the user to quit the program.
            Console.WriteLine("Press \'q\' to quit the sample.");
            while (Console.Read() != 'q') ;
        }

        //Fires if file changed.
        private static void OnChanged(object source, FileSystemEventArgs e)
        {
            if (ExecutePackage(e.FullPath))
                File.Delete(e.FullPath);
        }

        //Fires if file Created.
        private static void OnCreated(object source, FileSystemEventArgs e)
        {            
            if (ExecutePackage(e.FullPath))
                File.Delete(e.FullPath);
        }

        //Fires if file Renamed.
        private static void OnRenamed(object source, RenamedEventArgs e)
        {
            if (ExecutePackage(e.FullPath))
                File.Delete(e.FullPath);
        }

        /// 
        /// This is the root directory where the flat files are going to be loaded
        /// 
        private static bool ExecutePackage(string fileFullPath)
        {
            string errorDescription = string.Empty;
            DTSPackage _package = new DTSPackage();

            try
            {
                if (!File.Exists(fileFullPath))
                    return false;

                string strFileExt = (Path.GetExtension(fileFullPath) ?? string.Empty).ToLower();
                string directoryFullPath = Path.GetDirectoryName(fileFullPath) + @"\";
                string sourceFileName = Path.GetFileNameWithoutExtension(fileFullPath);

                if (Regex.IsMatch(strFileExt, @"\.txt|\.csv", RegexOptions.IgnoreCase))
                {
                    int result = WatcherDB.CallGetImportMetaData(fileFullPath);

                    if (result >= 1)
                        return true; 
                }
            }            
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + ex.StackTrace, "Exception Details");
            }
            finally
            {
                _package.ClearPackage();
            }
            MessageBox.Show(errorDescription);
            return false;
        }
    }





我的尝试:



我在谷歌搜索并与朋友一起询问等。



What I have tried:

I am searching in google and asking with friends etc.

推荐答案

为什么要安排任务?如果您只是让应用程序一直运行,它会在发生时检测文件系统更改。考虑改为编写Windows服务。系统启动时会加载/执行,并且无需用户登录即可运行。
Why are you scheduling a task? If you simply let the app run all the time, it will detect file system changes whenever they happen. Consider writing a Windows service instead. Those get loaded/executed when the system starts up, and can run without a user being logged on.


这篇关于Filesystemwatcher作为计划任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆