private void createStartupTask()
        {
            TaskDefinition td = ts.NewTask();

            td.RegistrationInfo.Description = "Monitors " + txtFolder.Text + " for files of format \""
                                              + txtFilter.Text + "\" with changes in";

            Array notifyFilters = Enum.GetValues(typeof(NotifyFilters));

            foreach (object s in notifyFilters)
            {
                NotifyFilters f = (NotifyFilters)Enum.Parse(typeof(NotifyFilters), s.ToString());
                if ((f & watcher.NotifyFilter) == f)
                {
                    td.RegistrationInfo.Description += ' ' + f.ToString() + ',';
                }
            }
            if (td.RegistrationInfo.Description.IndexOf(',') != td.RegistrationInfo.Description.LastIndexOf(','))
            {
                string lastItem = td.RegistrationInfo.Description.Substring(td.RegistrationInfo.Description.LastIndexOf(' '));
                lastItem.Replace(" ", " and ");
                td.RegistrationInfo.Description  = td.RegistrationInfo.Description.Remove(td.RegistrationInfo.Description.LastIndexOf(' '));
                td.RegistrationInfo.Description += lastItem;
            }
            int idx = td.RegistrationInfo.Description.LastIndexOf(',');

            if (idx > 0)
            {
                td.RegistrationInfo.Description  = td.RegistrationInfo.Description.Remove(idx);
                td.RegistrationInfo.Description += '.';
            }
            else
            {
                td.RegistrationInfo.Description += " [no attributes selected].";
            }

            td.Triggers.Add(new LogonTrigger());

            string filePath  = Application.ExecutablePath;
            string arguments = "\"" + txtFolder.Text +
                               (txtFilter.Text.Contains("*") ? "" : '\\' + txtFilter.Text) + "\" " +
                               (int)watcher.NotifyFilter + (IncludeSubdirs.Checked ? " /r" : " /nr");


            td.Actions.Add(filePath, arguments);

            setTaskName();

            td.Settings.ExecutionTimeLimit         = new TimeSpan(0);
            td.Settings.StopIfGoingOnBatteries     = false;
            td.Settings.DisallowStartIfOnBatteries = false;

            ts.RootFolder.RegisterTaskDefinition(taskName, td);
        }
Exemplo n.º 2
0
        //--------------------------------------------------------------------------------
        /// <summary>
        /// Actually creates the necessary FileSystemWatcher objects, depending oin which
        /// notify filters and change types the user specified.
        /// </summary>
        /// <param name="changeType"></param>
        /// <param name="filter"></param>
        private void CreateWatcher(bool changedWatcher, NotifyFilters filter)
        {
            Debug.WriteLine(string.Format("WatcherEx.CreateWatcher({0}, {1})", changedWatcher.ToString(), filter.ToString()));

            FileSystemWatcherEx watcher = null;
            int bufferSize = (int)this.watcherInfo.BufferKBytes * 1024;

            // Each "Change" filter gets its own watcher so we can determine *what*
            // actually changed. This will allow us to react only to the change events
            // that we actually want.  The reason I do this is because some programs
            // fire TWO events for  certain changes. For example, Notepad sends two
            // events when a file is created. One for CreationTime, and one for
            // Attributes.
            if (changedWatcher)
            {
                // if we're not handling the currently specified filter, get out
                if (HandleNotifyFilter(filter))
                {
                    watcher = new FileSystemWatcherEx(this.watcherInfo.WatchPath);
                    watcher.IncludeSubdirectories = this.watcherInfo.IncludeSubFolders;
                    watcher.Filter             = this.watcherInfo.FileFilter;
                    watcher.NotifyFilter       = filter;
                    watcher.InternalBufferSize = bufferSize;
                    switch (filter)
                    {
                    case NotifyFilters.Attributes:
                        watcher.Changed += new FileSystemEventHandler(watcher_ChangedAttribute);
                        break;

                    case NotifyFilters.CreationTime:
                        watcher.Changed += new FileSystemEventHandler(watcher_ChangedCreationTime);
                        break;

                    case NotifyFilters.DirectoryName:
                        watcher.Changed += new FileSystemEventHandler(watcher_ChangedDirectoryName);
                        break;

                    case NotifyFilters.FileName:
                        watcher.Changed += new FileSystemEventHandler(watcher_ChangedFileName);
                        break;

                    case NotifyFilters.LastAccess:
                        watcher.Changed += new FileSystemEventHandler(watcher_ChangedLastAccess);
                        break;

                    case NotifyFilters.LastWrite:
                        watcher.Changed += new FileSystemEventHandler(watcher_ChangedLastWrite);
                        break;

                    case NotifyFilters.Security:
                        watcher.Changed += new FileSystemEventHandler(watcher_ChangedSecurity);
                        break;

                    case NotifyFilters.Size:
                        watcher.Changed += new FileSystemEventHandler(watcher_ChangedSize);
                        break;
                    }
                }
            }
            // All other FileSystemWatcher events are handled through a single "main"
            // watcher.
            else
            {
                if (HandleWatchesFilter(WatcherChangeTypes.Created) ||
                    HandleWatchesFilter(WatcherChangeTypes.Deleted) ||
                    HandleWatchesFilter(WatcherChangeTypes.Renamed) ||
                    this.watcherInfo.WatchForError ||
                    this.watcherInfo.WatchForDisposed)
                {
                    watcher = new FileSystemWatcherEx(this.watcherInfo.WatchPath, watcherInfo.MonitorPathInterval);
                    watcher.IncludeSubdirectories = this.watcherInfo.IncludeSubFolders;
                    watcher.Filter             = this.watcherInfo.FileFilter;
                    watcher.InternalBufferSize = bufferSize;
                }

                if (HandleWatchesFilter(WatcherChangeTypes.Created))
                {
                    watcher.Created += new FileSystemEventHandler(watcher_CreatedDeleted);
                }
                if (HandleWatchesFilter(WatcherChangeTypes.Deleted))
                {
                    watcher.Deleted += new FileSystemEventHandler(watcher_CreatedDeleted);
                }
                if (HandleWatchesFilter(WatcherChangeTypes.Renamed))
                {
                    watcher.Renamed += new RenamedEventHandler(watcher_Renamed);
                }
                if (watcherInfo.MonitorPathInterval > 0)
                {
                    watcher.EventPathAvailability += new PathAvailabilityHandler(watcher_EventPathAvailability);
                }
            }
            if (watcher != null)
            {
                if (this.watcherInfo.WatchForError)
                {
                    watcher.Error += new ErrorEventHandler(watcher_Error);
                }
                if (this.watcherInfo.WatchForDisposed)
                {
                    watcher.Disposed += new EventHandler(watcher_Disposed);
                }
                this.watchers.Add(watcher);
            }
        }