Exemplo n.º 1
0
        public ChangeNavigationPageParameter GetChangeNavigationPageParameter()
        {
            ChangeNavigationPageParameter rtn = new ChangeNavigationPageParameter();

            //Outputpath is the checking 's original path.
            using (System.IO.StreamReader sr = new System.IO.StreamReader(this.txtReplacementDir.Text.Trim()))
            {
                string filepath = "";
                while ((filepath = sr.ReadLine()) != null)
                {
                    rtn.AddOriginalRootPath(filepath);
                }
                sr.Close();
            }
            //rtn.SetFileNameRegexRegular(SystemConfiguration.GetValue("NavigationSourceFileNameRegex"));
            //rtn.SetFileNameRegexReplacement(SystemConfiguration.GetValue("NavigationReplacementFileNameRegex"));
            rtn.SetFileNameRegexRegular(this.txtFileNameSearchReg.Text.Trim());
            rtn.SetFileNameRegexReplacement(this.txtFileNameReplacementReg.Text.Trim());

            rtn.SetHTMLRegexRegular(this.txtHTMSearchReg.Text.Trim());
            rtn.SetHTMLRegexReplacement(this.txtHTMLReplacementReg.Text.Trim());
            rtn.SetEnabledFileNameReplacement(this.ckbReplaceFileName.Checked);
            rtn.SetEnabledHTMLReplacement(this.ckbReplaceHTML.Checked);
            rtn.SetFileExtension(this.txtNavigationPageExtension.Text.Trim());
            return(rtn);
        }
 public ChangeNavigationPage(CancellationTokenSource _tokenSource, ICloneable _threadParameter) : base(_tokenSource, _threadParameter)
 {
     if (this.ThreadParameter != null)
     {
         param = (ChangeNavigationPageParameter)this.ThreadParameter;
     }
     //amount = 0;
 }
        /// <summary>
        /// recursive  call by self
        /// </summary>
        /// <returns></returns>
        public override bool RunSubThread(ICloneable thparam)
        {
            //judage if threading is cancelled.
            this.IsTaskCanceled();
            ChangeNavigationPageParameter param;

            if (thparam != null)
            {
                param = (ChangeNavigationPageParameter)thparam;
            }
            else
            {
                return(default(bool));
            }
            foreach (string OriginalRootPath in param.OriginalRootPath)
            {
                DirectoryInfo originalFold = new DirectoryInfo(OriginalRootPath);
                successamount = 0;
                LoggerHelper.Info("Start to operate the Fold:" + originalFold.FullName + "\r\n");
                if (!System.IO.Directory.Exists(OriginalRootPath))
                {
                    LoggerHelper.Warn("The fold[" + originalFold.FullName + "] is not exists! Ignore this fold.\r\n");
                    continue;
                }
                if (param.EnabledFileNameReplacement)
                {
                    FileInfo[] OriginalFileList = originalFold.GetFiles();

                    foreach (var file in OriginalFileList)
                    {
                        string originalFileName = file.Name;


                        if (RegularExpression.IsMatch(originalFileName, param.FileNameRegexRegular))
                        {
                            //LoggerHelper.Info("Matched" + "\r\n");
                            string NewFileName = RegularExpression.Replace(originalFileName, param.FileNameRegexRegular, param.FileNameRegexReplacement);
                            if (File.Exists(file.Directory + "\\" + NewFileName))
                            {
                                LoggerHelper.Info(file.Directory + "\\" + NewFileName + " is exists. Doesn't rename [" + file.Name + "]\r\n");
                            }
                            else
                            {
                                File.Copy(file.FullName, file.Directory + "\\" + NewFileName, true);
                                if (File.Exists(file.Directory + "\\" + NewFileName))
                                {
                                    File.Delete(file.FullName);
                                }
                                successamount++;
                            }
                        }

                        //SuccessRecorder.Record(FormatHistory(successamount, file.DirectoryName,file.Name, file.Extension));
                    }
                    LoggerHelper.Info("FileName Replacement is finished! Rename " + successamount.ToString() + " files.\r\n");
                }
                else
                {
                    LoggerHelper.Info("ignore FileName Replacement!\r\n");
                }
                if (param.EnabledHTMLReplacement)
                {
                    successamount = 0;
                    FileInfo[] OriginalFileList = originalFold.GetFiles("*" + param.FileExtension);
                    foreach (var file in OriginalFileList)
                    {
                        try
                        {
                            sr = new StreamReader(file.FullName);
                            string buffer            = "";
                            string tempWriteFileName = file.Directory + "\\" + Guid.NewGuid().ToString().Replace('-', '_');

                            sw = new StreamWriter(tempWriteFileName);
                            while ((buffer = sr.ReadLine()) != null)
                            {
                                buffer = RegularExpression.Replace(buffer, param.HTMLRegexRegular, param.HTMLRegexReplacement);
                                sw.WriteLine(buffer);
                            }
                            sr.Close();
                            sw.Close();
                            File.Delete(file.FullName);
                            File.Copy(tempWriteFileName, file.FullName, true);
                            File.Delete(tempWriteFileName);

                            successamount++;
                        }
                        catch
                        {
                        }
                        finally
                        {
                            if (sr != null)
                            {
                                sr.Close();
                            }
                            if (sw != null)
                            {
                                sw.Close();
                            }
                        }
                    }
                    LoggerHelper.Info("HTML Replacement is finished! Replace " + successamount.ToString() + " files.\r\n");
                }
                else
                {
                    LoggerHelper.Info("ignore HTML Replacement!\r\n");
                }
            }
            //recursive Sub Directory
            //foreach (DirectoryInfo dir in originalFold.GetDirectories())
            //{
            //    TraverseParameter paramSub = (TraverseParameter)param.Clone();
            //    paramSub.SetOriginalRootPath(dir.FullName);
            //    RunSubThread(paramSub);
            //}

            return(default(bool));
        }