Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- namespace BloodOfEvil.Helpers
- {
- public static class FileSystemHelper
- {
- /// <summary>
- /// Renvoie le contenu d'un fichier de manière sécurisée.
- /// </summary>
- public static string SafeGetFileContent(string path)
- {
- return File.Exists(@path) ?
- File.ReadAllText(@path) :
- "";
- }
- /// <summary>
- /// Affiche le contenu du fichier au chemin "path".
- /// </summary>
- public static void ShowFileContent(string path)
- {
- Console.WriteLine(SafeGetFileContent(@path));
- }
- /// <summary>
- /// Détermine si le "path" est un répertoire.
- /// </summary>
- public static bool IsDirectory(string path)
- {
- return (File.GetAttributes(@path) & FileAttributes.Directory) == FileAttributes.Directory;
- }
- /// <summary>
- /// Renvoit si le fichier ou le répertoire éxiste.
- /// </summary>
- public static bool DoesPathExists(string path)
- {
- return File.Exists(@path) ||
- Directory.Exists(@path);
- }
- /// <summary>
- /// Renvoit si un chemin "path" est d'une des extensions "extensions".
- /// </summary>
- public static bool IsOfExtension(
- string path,
- string[] extensions)
- {
- return Array.Exists(extensions, extension => extension.Equals(Path.GetExtension(@path)));
- }
- /// <summary>
- /// 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.
- /// </summary>
- public static void SafeCreateFile(string path)
- {
- string pathDirectory = Path.GetDirectoryName(@path);
- // Créer le répertoire parent du chemin "Path" si il n'éxiste pas.
- if (!string.IsNullOrEmpty(pathDirectory) &&
- Directory.Exists(pathDirectory))
- Directory.CreateDirectory(pathDirectory);
- using (FileStream fs = File.Create(@path)) { }
- }
- /// <summary>
- /// Créer le chemin si il n'éxiste pas.
- /// </summary>
- public static void CreatePathIfDontExists(string path)
- {
- if (!FileSystemHelper.DoesPathExists(@path))
- {
- // Je considère qu'un chemin est un fichier si il possède une extension.
- if (string.IsNullOrEmpty(Path.GetExtension(@path)))
- Directory.CreateDirectory(@path);
- else
- FileSystemHelper.SafeCreateFile(@path);
- }
- }
- /// <summary>
- /// Combine 2 chemins.
- /// </summary>
- public static string CombinePath(string path, string subPath)
- {
- ///Devrait utilise "params string subPath".
- return Path.Combine(@path, @subPath);
- }
- /// <summary>
- /// Supprime un répertoire de façon sécurisée et récursive.
- /// </summary>
- public static void SafeDeleteDirectoryRecursively(string directoryPath)
- {
- if (Directory.Exists(@directoryPath))
- Directory.Delete(@directoryPath, true);
- }
- /// <summary>
- /// Récupère tous les fichiers d'un répertoire ayant une des extensions notifié par "extensions" et
- /// ne contenant pas un des filtres de "exclusiveIgnoreFilters".
- /// </summary>
- public static string[] GetAllDirectoryFilesRecursively(
- string directoryPath,
- string[] extensions,
- string[] exclusiveIgnoreFilters)
- {
- return extensions.SelectMany(extension => Directory.GetFiles(@directoryPath, "*" + extension, SearchOption.AllDirectories))
- .Where(fileName => !fileName.ContainsAnArrayElement(exclusiveIgnoreFilters))
- .ToArray();
- }
- /// <summary>
- /// Affiche tous les fichiers d'un répertoire "directoryPath" de manière récursive et d'une des extensions de "extensions" et
- /// ne contenant pas un des filtres de "exclusiveIgnoreFilters".
- /// </summary>
- public static void ListAllFilesRecursivelyInADirectory(
- string directoryPath,
- string[] extensions,
- string[] exclusiveIgnoreFilters)
- {
- Array.ForEach(GetAllDirectoryFilesRecursively(directoryPath, extensions, exclusiveIgnoreFilters), Console.WriteLine);
- }
- /// <summary>
- /// Renvoie le nombre de sous répertoires du répertoire "rootDirectory".
- /// </summary>
- public static int GetTheNumberOfSubDirectories(string rootDirectory)
- {
- return Directory.GetDirectories(@rootDirectory).Length;
- }
- /// <summary>
- /// Renvoie si le fichier est vide.
- /// </summary>
- public static bool IsEmptyFile(string path)
- {
- return new FileInfo(@path).Length == 0;
- }
- /// <summary>
- /// Enlève l'extension d'un nom de fichier.
- /// </summary>
- public static string TakeOutExtension(string fileName)
- {
- return Path.GetFileNameWithoutExtension(@fileName);
- }
- /// <summary>
- /// Renvoie le nom de tous les sous répertoires du répertoire "repositoryPath".
- /// </summary>
- public static string[] GetSubDirectories(string repositoryPath)
- {
- return Directory.GetDirectories(repositoryPath);
- }
- /// <summary>
- /// Renvoit le chemin pour accéder au bureau de l'utilisateur.
- /// </summary>
- /// <returns></returns>
- public static string GetDesktopPath()
- {
- return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
- }
- /// <summary>
- /// Renvoit la taille d'un répertoire en octets.
- /// </summary>
- public static long GetDirectorySize(string directoryPath)
- {
- return Directory.GetFiles(@directoryPath, "*", SearchOption.AllDirectories)
- .Sum(subFile => (new FileInfo(@subFile).Length));
- }
- /// <summary>
- /// Exemple pour path = "Hello/Toto" -> créer le répertoire Hello si il n'éxiste pas et renvoit si il éxiste.
- /// </summary>
- public static void CreateFileDirectoryIfDontExists(string path)
- {
- CreatePathIfDontExists(Path.GetDirectoryName(@path));
- }
- }
- }
- using System;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- namespace BloodOfEvil.Extensions
- {
- public static class StringExtension
- {
- /// <summary>
- /// Renvoie si une châine est égale à une châine des châines d'un tableau de châine.
- /// Il y a beaucoup de fois le mot châine ̿' ̿'\̵͇̿̿\з=(◕_◕)=ε/̵͇̿̿/'̿'̿ ̿
- /// </summary>
- public static bool EqualToAnArrayElement(
- this string text,
- string[] researchedTexts)
- {
- if (string.IsNullOrEmpty(text) ||
- null == researchedTexts ||
- 0 == researchedTexts.Length)
- return false;
- return Array.Exists(researchedTexts, researchedText => !string.IsNullOrEmpty(researchedText) && text.Equals(researchedText));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement