AbstractFileParser.cs
Description du code
AbstractFileParser.cs est un fichier du projet BrolDev.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.
Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.
Code source ou contenu du fichier
Code c# (AbstractFileParser.cs) (238 lignes)
using System; using System.ComponentModel; using System.IO; using be.gaudry.model.config; using be.gaudry.model.enums; namespace be.gaudry.model.file { /// <summary> /// Provides template to parse files and force to implement something to do on files /// </summary> public abstract class AbstractFileParser { #region declarations and constructors protected internal float progressMax, progressToDo; protected internal String startPath; protected internal bool includeSubFolders, includeHidden, preCalcProgress, validPreviousProgress; public AbstractFileParser() { progressToDo = 0F; progressMax = 0F; validPreviousProgress = false; includeSubFolders = true; includeHidden = false; preCalcProgress = true; "TargetInvocationException", "AbstractFileParser", "http://www.thescripts.com/forum/thread519073.html", "TargetInvocationException et utilisation pour e.Results" ) ); } #endregion #region properties /// <summary> /// True to search on the subfolders /// </summary> /// <remarks> /// Modifying this forces total size calculation if progress needed /// </remarks> public bool IncludeSubFolders { get { return includeSubFolders; } set { if (includeSubFolders != value) { //forces size calculation if progress needed validPreviousProgress = false; } includeSubFolders = value; } } /// <summary> /// True to search on the hidden files /// </summary> /// <remarks> /// Modifying this forces total size calculation if progress needed /// </remarks> public bool IncludeHiddenFiles { get { return includeHidden; } set { if (includeHidden != value) { //forces size calculation if progress needed validPreviousProgress = false; } includeHidden = value; } } public bool PreCalcProgress { get { return preCalcProgress; } set { preCalcProgress = value; } } /// <summary> /// Path of the root directory where to search /// </summary> /// <remarks> /// Modifying this forces total size calculation if progress needed /// </remarks> public String StartPath { get { return this.startPath; } set { if (startPath != value) { //forces size calculation if progress needed validPreviousProgress = false; } startPath = value; } } #endregion #region public methods public virtual long parse(BackgroundWorker bgw, DoWorkEventArgs e) { if (!validPreviousProgress) { // reset the progressMax if not valid because the searchMedia asynchroneous method will // add sizes along the search progressMax = 0F; if (preCalcProgress) { // Get progressMax before the first search, // or if a search parameter having a signification for the progressMax value has been modified bgw.ReportProgress((int)BGWORKER.currentInfo, "Lecture de la taille totale du répertoire"); progressMax = (float)FileHelper.getDirectoryLength(bgw, e, dirsInf, includeSubFolders); validPreviousProgress = true; } } //job may have been cancelled during the getDirectoryLength asynchroneous method if (!bgw.CancellationPending) { // Set the progressMax with the result of the search // the second search with the same basics search parameters // will able to display the progress //progressMax = browse(bgw, e, progressMax); progressToDo = progressMax; if (validPreviousProgress) { bgw.ReportProgress((int)BGWORKER.progressBarStyle_Block); } parseFiles(bgw, e, dirsInf, 0); } //job may have been cancelled during the searchMedia asynchroneous method if (!bgw.CancellationPending) { validPreviousProgress = true; bgw.ReportProgress((int)BGWORKER.currentInfo, "Lecture de la taille totale du répertoire"); } return (long)progressMax; } #endregion #region methods to implement /// <summary> /// Do something on each file /// </summary> /// <param name="bgw">BackgroundWorker to perform cancel if needed, and report progress</param> /// <param name="e">DoWorkEventArgs to perform cancel if needed, and report progress</param> /// <param name="fi">FileInfo to check</param> /// <param name="percent">(int) Progress percentage</param> /// <param name="subDirDeep">Deep of the directory in comparison with start directory</param> /// <returns> /// true if we must stop the parsing of current directory (ie. if we have a vob file, we don't check the other files in this directory) /// false otherwise ///</returns> protected abstract bool performOnFile(BackgroundWorker bgw, DoWorkEventArgs e, FileInfo fi, int percent, int subDirDeep); #endregion #region protected internal methods /// <summary> /// /// </summary> /// <param name="bgw">BackgroundWorker to perform cancel if needed, and report progress</param> /// <param name="e">DoWorkEventArgs to perform cancel if needed, and report progress</param> /// <param name="dirsInf">DirectoryInfo to get size</param> /// <param name="subDirDeep">Deep of the directory in comparison with start directory</param> protected internal void parseFiles(BackgroundWorker bgw, DoWorkEventArgs e, DirectoryInfo dirsInf, int subDirDeep) { if (bgw.CancellationPending) { e.Cancel = true; } else { bgw.ReportProgress((int)BGWORKER.currentInfo, dirsInf.Name); try { FileInfo[] filesInf = dirsInf.GetFiles(); foreach (FileInfo fi in filesInf) { progressToDo -= fi.Length; // add length to avoid calculate progressMax each time if (!validPreviousProgress) { progressMax += fi.Length; } if (perform(bgw, e, fi, subDirDeep)) { break; } } } catch (Exception) { } if (includeSubFolders) { try { subDirDeep++; foreach (DirectoryInfo dir in dirsInf.GetDirectories()) { parseFiles(bgw, e, dir, subDirDeep); } } catch (Exception) { } } } } /// <summary> /// /// </summary> /// <param name="bgw">BackgroundWorker to perform cancel if needed, and report progress</param> /// <param name="e">DoWorkEventArgs to perform cancel if needed, and report progress</param> /// <param name="fi">FileInfo</param> /// <param name="subDirDeep">Deep of the directory in comparison with start directory</param> /// <returns></returns> protected internal bool perform(BackgroundWorker bgw, DoWorkEventArgs e, FileInfo fi, int subDirDeep) { if (bgw.CancellationPending) { e.Cancel = true; return true; } else { int progressPercent = (validPreviousProgress) ? ((progressToDo <= progressMax) ? (int)((progressMax - progressToDo) / progressMax * 100) : 100) : 1; if (!IncludeHiddenFiles && ((fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)) { bgw.ReportProgress(progressPercent, null); return false; } return performOnFile(bgw, e, fi, progressPercent, subDirDeep); } } #endregion } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732357770 23/11/2024 11:29:30 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 16/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-broldev-source-rf-model/file//AbstractFileParser.cs.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.