Advertisement
BLooDBuRNiNG

FileSystemHelper

Nov 13th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. namespace BloodOfEvil.Helpers
  6. {
  7.     public static class FileSystemHelper
  8.     {
  9.         /// <summary>
  10.         /// Renvoie le contenu d'un fichier de manière sécurisée.
  11.         /// </summary>
  12.         public static string SafeGetFileContent(string path)
  13.         {
  14.             return File.Exists(@path) ?
  15.                     File.ReadAllText(@path) :
  16.                     "";
  17.         }
  18.  
  19.         /// <summary>
  20.         /// Affiche le contenu du fichier au chemin "path".
  21.         /// </summary>
  22.         public static void ShowFileContent(string path)
  23.         {
  24.             Console.WriteLine(SafeGetFileContent(@path));
  25.         }
  26.  
  27.         /// <summary>
  28.         /// Détermine si le "path" est un répertoire.
  29.         /// </summary>
  30.         public static bool IsDirectory(string path)
  31.         {
  32.             return (File.GetAttributes(@path) & FileAttributes.Directory) == FileAttributes.Directory;
  33.         }
  34.  
  35.         /// <summary>
  36.         /// Renvoit si le fichier ou le répertoire éxiste.
  37.         /// </summary>
  38.         public static bool DoesPathExists(string path)
  39.         {
  40.             return File.Exists(@path) ||
  41.                    Directory.Exists(@path);
  42.         }
  43.  
  44.         /// <summary>
  45.         /// Renvoit si un chemin "path" est d'une des extensions "extensions".
  46.         /// </summary>
  47.         public static bool IsOfExtension(
  48.             string path,
  49.             string[] extensions)
  50.         {
  51.             return Array.Exists(extensions, extension => extension.Equals(Path.GetExtension(@path)));
  52.         }
  53.  
  54.         /// <summary>
  55.         /// Permet de créer un fichier de manière sécurisée en créant le répertoire parent du fichier si il n'éxiste pas encore.
  56.         /// </summary>
  57.         public static void SafeCreateFile(string path)
  58.         {
  59.             string pathDirectory = Path.GetDirectoryName(@path);
  60.  
  61.             // Créer le répertoire parent du chemin "Path" si il n'éxiste pas.
  62.             if (!string.IsNullOrEmpty(pathDirectory) &&
  63.                 Directory.Exists(pathDirectory))
  64.                 Directory.CreateDirectory(pathDirectory);
  65.  
  66.             using (FileStream fs = File.Create(@path)) { }
  67.         }
  68.  
  69.         /// <summary>
  70.         /// Créer le chemin si il n'éxiste pas.
  71.         /// </summary>
  72.         public static void CreatePathIfDontExists(string path)
  73.         {
  74.             if (!FileSystemHelper.DoesPathExists(@path))
  75.             {
  76.                 // Je considère qu'un chemin est un fichier si il possède une extension.
  77.                 if (string.IsNullOrEmpty(Path.GetExtension(@path)))
  78.                     Directory.CreateDirectory(@path);
  79.                 else
  80.                     FileSystemHelper.SafeCreateFile(@path);
  81.             }
  82.         }
  83.  
  84.         /// <summary>
  85.         /// Combine 2 chemins.
  86.         /// </summary>
  87.         public static string CombinePath(string path, string subPath)
  88.         {
  89.             ///Devrait utilise "params string subPath".
  90.             return Path.Combine(@path, @subPath);
  91.         }
  92.  
  93.         /// <summary>
  94.         /// Supprime un répertoire de façon sécurisée et récursive.
  95.         /// </summary>
  96.         public static void SafeDeleteDirectoryRecursively(string directoryPath)
  97.         {
  98.             if (Directory.Exists(@directoryPath))
  99.                 Directory.Delete(@directoryPath, true);
  100.         }
  101.  
  102.         /// <summary>
  103.         /// Récupère tous les fichiers d'un répertoire ayant une des extensions notifié par "extensions" et
  104.         /// ne contenant pas un des filtres de "exclusiveIgnoreFilters".
  105.         /// </summary>
  106.         public static string[] GetAllDirectoryFilesRecursively(
  107.             string directoryPath,
  108.             string[] extensions,
  109.             string[] exclusiveIgnoreFilters)
  110.         {
  111.             return extensions.SelectMany(extension => Directory.GetFiles(@directoryPath, "*" + extension, SearchOption.AllDirectories))
  112.                              .Where(fileName => !fileName.ContainsAnArrayElement(exclusiveIgnoreFilters))
  113.                              .ToArray();
  114.         }
  115.  
  116.         /// <summary>
  117.         /// Affiche tous les fichiers d'un répertoire "directoryPath" de manière récursive et d'une des extensions de "extensions"  et
  118.         /// ne contenant pas un des filtres de "exclusiveIgnoreFilters".
  119.         /// </summary>
  120.         public static void ListAllFilesRecursivelyInADirectory(
  121.             string directoryPath,
  122.             string[] extensions,
  123.             string[] exclusiveIgnoreFilters)
  124.         {
  125.             Array.ForEach(GetAllDirectoryFilesRecursively(directoryPath, extensions, exclusiveIgnoreFilters), Console.WriteLine);
  126.         }
  127.  
  128.         /// <summary>
  129.         /// Renvoie le nombre de sous répertoires du répertoire "rootDirectory".
  130.         /// </summary>
  131.         public static int GetTheNumberOfSubDirectories(string rootDirectory)
  132.         {
  133.             return Directory.GetDirectories(@rootDirectory).Length;
  134.         }
  135.  
  136.         /// <summary>
  137.         /// Renvoie si le fichier est vide.
  138.         /// </summary>
  139.         public static bool IsEmptyFile(string path)
  140.         {
  141.             return new FileInfo(@path).Length == 0;
  142.         }
  143.  
  144.         /// <summary>
  145.         /// Enlève l'extension d'un nom de fichier.
  146.         /// </summary>
  147.         public static string TakeOutExtension(string fileName)
  148.         {
  149.             return Path.GetFileNameWithoutExtension(@fileName);
  150.         }
  151.  
  152.         /// <summary>
  153.         /// Renvoie le nom de tous les sous répertoires du répertoire "repositoryPath".
  154.         /// </summary>
  155.         public static string[] GetSubDirectories(string repositoryPath)
  156.         {
  157.             return Directory.GetDirectories(repositoryPath);
  158.         }
  159.  
  160.         /// <summary>
  161.         /// Renvoit le chemin pour accéder au bureau de l'utilisateur.
  162.         /// </summary>
  163.         /// <returns></returns>
  164.         public static string GetDesktopPath()
  165.         {
  166.             return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
  167.         }
  168.  
  169.         /// <summary>
  170.         /// Renvoit la taille d'un répertoire en octets.
  171.         /// </summary>
  172.         public static long GetDirectorySize(string directoryPath)
  173.         {
  174.             return Directory.GetFiles(@directoryPath, "*", SearchOption.AllDirectories)
  175.                             .Sum(subFile => (new FileInfo(@subFile).Length));
  176.         }
  177.  
  178.         /// <summary>
  179.         /// Exemple pour path = "Hello/Toto" -> créer le répertoire Hello si il n'éxiste pas et renvoit si il éxiste.
  180.         /// </summary>
  181.         public static void CreateFileDirectoryIfDontExists(string path)
  182.         {
  183.             CreatePathIfDontExists(Path.GetDirectoryName(@path));
  184.         }
  185.     }
  186. }
  187.  
  188. using System;
  189. using System.Globalization;
  190. using System.Linq;
  191. using System.Text;
  192.  
  193. namespace BloodOfEvil.Extensions
  194. {
  195.     public static class StringExtension
  196.     {
  197.         /// <summary>
  198.         /// Renvoie si une châine est égale à une châine des châines d'un tableau de châine.
  199.         /// Il y a beaucoup de fois le mot châine ̿' ̿'\̵͇̿̿\з=(◕_◕)=ε/̵͇̿̿/'̿'̿ ̿
  200.         /// </summary>
  201.         public static bool EqualToAnArrayElement(
  202.             this string text,
  203.             string[] researchedTexts)
  204.         {
  205.             if (string.IsNullOrEmpty(text) ||
  206.                 null == researchedTexts ||
  207.                 0 == researchedTexts.Length)
  208.                 return false;
  209.  
  210.             return Array.Exists(researchedTexts, researchedText => !string.IsNullOrEmpty(researchedText) && text.Equals(researchedText));
  211.         }
  212.     }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement