Example #1
0
        /// <summary>
        /// ファイルが削除されたときに、このクラスの Deleted イベントを発生する。
        /// </summary>
        /// <param name="sender">イベントの発生元 (FileSystemWatcher)</param>
        /// <param name="args">イベント発生元からの引数</param>
        private void onDeleted(Object sender, FileSystemEventArgs e)
        {
            string lowerFullPath = e.FullPath.ToLower();

            if (this.files.Contains(lowerFullPath))
            {
                this.files.Remove(lowerFullPath);

                TvProgram          tvprg  = new TvProgram(e.FullPath, false);
                TvProgramEventArgs tvargs = new TvProgramEventArgs(WatcherChangeTypes.Deleted, tvprg);
                this.Deleted(this, tvargs);
            }
        }
Example #2
0
        public static void MakeViewed(TvProgram tvPrg)
        {
            FileStream   fs       = null;
            StreamReader sr       = null;
            StreamWriter sw       = null;
            string       contents = String.Empty;

            try
            {
                fs       = new FileStream(tvPrg.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sr       = new StreamReader(fs, Encoding.GetEncoding("shift_jis"));
                contents = sr.ReadToEnd();
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
                sr = null;
                fs = null;
            }

            DateTime now = DateTime.Now;

            contents = contents.Replace("<history_cnt>0</history_cnt>", "<history_cnt>1</history_cnt>");
            contents = contents.Replace("<last_date>0000000000</last_date>",
                                        "<last_date>" + now.Year.ToString("0000") + now.Month.ToString("00") + now.Day.ToString("00") + "00</last_date>");

            try
            {
                fs = new FileStream(tvPrg.FilePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
                sw = new StreamWriter(fs, Encoding.GetEncoding("shift_jis"));
                sw.Write(contents);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                sw = null;
                fs = null;
            }
        }
Example #3
0
        /// <summary>
        /// ファイルが更新されたときに、このクラスの Changed イベントを発生する。
        /// </summary>
        /// <param name="sender">イベントの発生元 (FileSystemWatcher)</param>
        /// <param name="args">イベント発生元からの引数</param>
        private void onChanged(Object sender, FileSystemEventArgs e)
        {
            Thread.Sleep(5);
            string lowerFullPath = e.FullPath.ToLower();

            if (this.files.Contains(lowerFullPath))
            {
                try
                {
                    TvProgram          tvprg  = new TvProgram(e.FullPath);
                    TvProgramEventArgs tvargs = new TvProgramEventArgs(WatcherChangeTypes.Changed, tvprg);
                    // ファイルを読み取る前に消えた場合、例外が発生しうるけど、とりあえず気にしない。
                    this.Changed(this, tvargs);
                }
                catch (UnauthorizedAccessException) { } // アクセス権限がない場合は無視する
                catch (FileNotFoundException) { }       // ファイルが見当たらなくても無視して、Deleted イベントに任せる。
            }
        }
Example #4
0
        private void onRenamed(object sender, RenamedEventArgs e)
        {
            Thread.Sleep(5);
            string lowerNewFullPath = e.FullPath.ToLower();
            string lowerOldFullPath = e.OldFullPath.ToLower();

            bool IsNewFullPathExist = this.files.Contains(lowerNewFullPath);
            bool IsOldFullPathExist = this.files.Contains(lowerOldFullPath);

            if (!IsNewFullPathExist && IsOldFullPathExist)
            {
                this.files.Remove(lowerOldFullPath);

                try
                {
                    TvProgram tvprg = new TvProgram(e.FullPath);
                    // ファイルを読み取る前に消えた場合、例外が発生しうるけど、とりあえず気にしない。
                    TvProgramEventArgs tvargs = new TvProgramEventArgs(WatcherChangeTypes.Created, tvprg);

                    TvProgram otvprg = new TvProgram(e.OldFullPath, false);

                    TvProgramRenamedEventArgs tvrargs = new TvProgramRenamedEventArgs(WatcherChangeTypes.Renamed, tvprg, otvprg);
                    this.Renamed(this, tvrargs);
                }
                catch (UnauthorizedAccessException) { } // アクセス権限がない場合は無視する
                catch (FileNotFoundException) { }       // ファイルが見当たらなくても無視して、Deleted イベントに任せる。

                this.files.Add(lowerNewFullPath);
            }
            else if (IsNewFullPathExist && IsOldFullPathExist)
            {
                this.onDeleted(sender,
                               new FileSystemEventArgs(System.IO.WatcherChangeTypes.Deleted, this.Path, e.OldName));
                this.onChanged(sender, e);
            }
            else if (IsNewFullPathExist && !IsOldFullPathExist)
            {
                this.onChanged(sender, e);
            }
            else
            {
                this.onCreated(sender, e);
            }
        }
Example #5
0
        public static void MakeUnviewed(TvProgram tvPrg)
        {
            FileStream   fs       = null;
            StreamReader sr       = null;
            StreamWriter sw       = null;
            string       contents = String.Empty;

            try
            {
                fs       = new FileStream(tvPrg.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                sr       = new StreamReader(fs, Encoding.GetEncoding("shift_jis"));
                contents = sr.ReadToEnd();
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
                sr = null;
                fs = null;
            }

            contents = System.Text.RegularExpressions.Regex.Replace(
                contents, "<history_cnt>[0-9]</history_cnt>", "<history_cnt>0</history_cnt>");
            contents = System.Text.RegularExpressions.Regex.Replace(
                contents, "<last_date>[0-9]{n}</last_date>", "<last_date>0000000000</last_date>");

            try
            {
                fs = new FileStream(tvPrg.FilePath, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
                sw = new StreamWriter(fs, Encoding.GetEncoding("shift_jis"));
                sw.Write(contents);
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
                sw = null;
                fs = null;
            }
        }
Example #6
0
 public TvProgramRenamedEventArgs(WatcherChangeTypes changeType, TvProgram TvProgram, TvProgram oldTvProgram)
 {
     this.ChangeType   = changeType;
     this.TvProgram    = TvProgram;
     this.OldTvProgram = oldTvProgram;
 }
Example #7
0
        /// <summary>
        /// 指定ディレクトリ以下のファイルを探索し、ファイル発見ごとにこのクラスの Found イベントを発生する。
        /// 更新日時が新しいものから順にイベントを発生する。
        /// </summary>
        /// <param name="curdirstr">ファイル検索対象のフォルダー</param>
        private void SearchAllFiles(String currentdir, bool isRecursionEntry)
        {
            // 順次 Found イベントを発生させるため、再帰的なファイル検索を独自に実装した。

            int allnum     = 0;
            int proceednum = 0;

            if (isRecursionEntry)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(currentdir);
                allnum = dirInfo.GetFileSystemInfos().Length;
            }

            // 直下のファイルの検索
            try
            {
                DirectoryInfo   dirInfo  = new DirectoryInfo(currentdir);
                List <FileInfo> fileList = new List <FileInfo>(dirInfo.GetFiles(TvProgramWatcher.FILE_NAME_FILTER));
                //fileList.Sort(delegate(FileInfo x, FileInfo y) { return -x.LastWriteTime.CompareTo(y.LastWriteTime); });
                fileList.Sort(delegate(FileInfo x, FileInfo y) { return(-x.FullName.CompareTo(y.FullName)); });
                foreach (FileInfo f in fileList)
                {
                    lock (lockTarget)
                    {
                        if (!this.files.Contains(f.FullName.ToLower()))
                        {
                            try
                            {
                                TvProgram          tvprg = new TvProgram(f.FullName);
                                TvProgramEventArgs e     = new TvProgramEventArgs(WatcherChangeTypes.Created, tvprg);
                                this.Found(this, e);
                                this.files.Add(f.FullName.ToLower());
                            }
                            catch (UnauthorizedAccessException)
                            {
                                Console.WriteLine("アクセス権限がないファイルを無視しました。");
                            }
                            catch (FileNotFoundException)
                            {
                                Console.WriteLine("処理中にファイルが削除されました。");
                            }
                        }
                    }
                }
                if (isRecursionEntry)
                {
                    proceednum++;
                    this.SearchProgress = Math.Min((1.0 * proceednum) / allnum, 1.0);
                }
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("アクセス権限がないフォルダを無視しました。");
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("処理中にフォルダが削除されました。");
            }

            // サブディレクトリの検索
            try
            {
                DirectoryInfo        dirInfo    = new DirectoryInfo(currentdir);
                List <DirectoryInfo> subDirList = new List <DirectoryInfo>(dirInfo.GetDirectories());
                //subDirList.Sort(delegate(DirectoryInfo x, DirectoryInfo y) { return -x.LastWriteTime.CompareTo(y.LastWriteTime); });
                subDirList.Sort(delegate(DirectoryInfo x, DirectoryInfo y) { return(-x.FullName.CompareTo(y.FullName)); });
                foreach (DirectoryInfo subdir in subDirList)
                {
                    SearchAllFiles(subdir.FullName, false);
                    if (isRecursionEntry)
                    {
                        proceednum++;
                        this.SearchProgress = Math.Min((1.0 * proceednum) / allnum, 1.0);
                    }
                }
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("アクセス権限がないフォルダを無視しました。");
                if (isRecursionEntry)
                {
                    proceednum++;
                    this.SearchProgress = Math.Min((1.0 * proceednum) / allnum, 1.0);
                }
            }
            catch (DirectoryNotFoundException)
            {
                Console.WriteLine("処理中にフォルダが削除されました。");
                if (isRecursionEntry)
                {
                    proceednum++;
                    this.SearchProgress = Math.Min((1.0 * proceednum) / allnum, 1.0);
                }
            }
        }
 public TvProgramEventArgs(WatcherChangeTypes changeType, TvProgram tvProgram)
 {
     this.ChangeType = changeType;
     this.TvProgram  = tvProgram;
 }