755 lines
31 KiB
C#
755 lines
31 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Eugen.IO
|
|
{
|
|
/// <summary>
|
|
/// Deletes files and/or directories
|
|
/// </summary>
|
|
class Delete
|
|
{
|
|
#region Variablen
|
|
List<string> undeletableFiles = new List<string>(); //Liste der unlöschbaren Dateien
|
|
List<string> undeletableDirectories = new List<string>(); //Liste der unlöschbaren Verzeichnisse
|
|
|
|
public static long FileCounter { set; get; } //Anzahl der abgearbeiteten Dateien
|
|
public static long DirectoryCounter { set; get; } //Anzahl der abgearbeiteten Verzeichnisse
|
|
|
|
public static long NumberOfFiles { set; get; } //Anzahl der Dateien
|
|
public static long NumberOfDirectories { set; get; } //Anzahl der Verzeichnisse
|
|
|
|
public static List<string> UndeletableFiles { set; get; } //Liste aller unlöschbaren Dateien
|
|
public static List<string> UndeletableDirectories { set; get; } //Liste aller unlöschbaren Verzeichnisse
|
|
|
|
public static int PercentDone { set; get; } //Prozent abgeschlossen
|
|
public static string Status { set; get; } //Zeigt an welche Datei gerade gelöscht wird
|
|
#endregion
|
|
|
|
#region Konstruktor
|
|
/// <summary>
|
|
/// Delete files or directories in a directory.
|
|
/// </summary>
|
|
public Delete()
|
|
{
|
|
UndeletableFiles = undeletableFiles;
|
|
UndeletableDirectories = undeletableDirectories;
|
|
}
|
|
#endregion
|
|
|
|
#region File
|
|
/// <summary>
|
|
/// Deletes a singel file.
|
|
/// </summary>
|
|
/// <param name="fileName">A valid filename with path.</param>
|
|
/// <returns></returns>
|
|
public string File(string fileName)
|
|
{
|
|
//Löscht eine Datei
|
|
try
|
|
{
|
|
//Wert für ProgressBar berechnen
|
|
FileCounter++; //Zähler hochzählen
|
|
if ((NumberOfFiles + NumberOfDirectories) == 0)
|
|
{
|
|
PercentDone = 0;
|
|
}
|
|
else
|
|
{
|
|
PercentDone = Convert.ToInt32(100 / Convert.ToDouble(NumberOfFiles + NumberOfDirectories) * Convert.ToDouble(FileCounter + DirectoryCounter));
|
|
}
|
|
if (PercentDone > 100)
|
|
{
|
|
PercentDone = 100; //Auf max 100% begrenzen
|
|
}
|
|
Status = String.Concat("Delete ", fileName);
|
|
OnEvent(PercentDone, Status);
|
|
|
|
System.IO.File.SetAttributes(fileName, FileAttributes.Normal); //Attribute zurücksetzen
|
|
System.IO.File.Delete(fileName); //Datei löschen
|
|
|
|
return null; //Wenn löschen möglich war
|
|
}
|
|
catch (Exception)
|
|
{
|
|
UndeletableFiles.Add(fileName); //Dateiname in die Liste der unlöschbaren Dateien aufnehmen
|
|
return fileName; //Wenn löschen nicht möglich war
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a file in a directory that correspond to a particular pattern sequence.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", ".ext", "*.ext", "*xyz.ext", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All files.</param>
|
|
/// <returns>If the file was successfully deleted, 'null' is returned, otherwise the filename.</returns>
|
|
public List<string> File(string directoryName, string searchPattern)
|
|
{
|
|
//Löscht alle Dateien im angegebenen Verzeichnis die dem Suchkriterium entsprechen
|
|
//Gültige Suchpattern: "", ".txt", "*.txt", "*xyz*", "Xyz*", "*xyz", "wx*yz"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
List<string> undeletableFiles = new List<string>(); //Liste der unlöschbaren Dateien
|
|
string tempUndelFile = null;
|
|
|
|
try
|
|
{
|
|
foreach (string fileName in System.IO.Directory.GetFiles(directoryName))
|
|
{
|
|
if (SearchPatternFile(fileName, searchPattern))
|
|
{
|
|
//Wenn Datei dem Suchpattern entspricht, dann löschen
|
|
tempUndelFile = File(fileName);
|
|
}
|
|
|
|
//Wenn ein Dateiname zurückgeliefert wird, dann in die Liste aufnehmen
|
|
if (tempUndelFile != null)
|
|
{
|
|
undeletableFiles.Add(tempUndelFile);
|
|
tempUndelFile = null; //Variable wieder zurücksetzen
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return undeletableFiles;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a file in a directory that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", ".ext", "*.ext", "*xyz.ext", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All files.</param>
|
|
/// <param name="daysOld">Days the file must be old. 0=All files are counted.</param>
|
|
/// <returns>If the file was successfully deleted, 'null' is returned, otherwise the filename.</returns>
|
|
public List<string> File(string directoryName, string searchPattern, int daysOld)
|
|
{
|
|
//Löscht alle Dateien im angegebenen Verzeichnis die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", ".txt", "*.txt", "*xyz*", "Xyz*", "*xyz", "wx*yz"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
List<string> undeletableFiles = new List<string>(); //Liste der unlöschbaren Dateien
|
|
string tempUndelFile = null;
|
|
|
|
try
|
|
{
|
|
foreach (string fileName in System.IO.Directory.GetFiles(directoryName))
|
|
{
|
|
if (SearchPatternFile(fileName, searchPattern) && DaysOld(fileName, daysOld))
|
|
{
|
|
//Wenn Datei dem Suchpattern entspricht, dann löschen
|
|
tempUndelFile = File(fileName);
|
|
}
|
|
|
|
//Wenn ein Dateiname zurückgeliefert wird, dann in die Liste aufnehmen
|
|
if (tempUndelFile != null)
|
|
{
|
|
undeletableFiles.Add(tempUndelFile);
|
|
tempUndelFile = null; //Variable wieder zurücksetzen
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return undeletableFiles;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes a file in a directory and all subdirectories that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", ".ext", "*.ext", "*xyz.ext", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All files.</param>
|
|
/// <param name="daysOld">Days the file must be old. 0=All files are counted.</param>
|
|
/// <param name="recursive">True=Search in all sudirectories recursive. False=Search only in root directory.</param>
|
|
/// <returns>If the file was successfully deleted, 'null' is returned, otherwise the filename.</returns>
|
|
public List<string> File(string directoryName, string searchPattern, int daysOld, bool recursive)
|
|
{
|
|
//Löscht alle Dateien im angegebenen Verzeichnis und allen Unterverzeichnissen die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", ".txt", "*.txt", "*xyz*", "Xyz*", "*xyz", "wx*yz"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
List<string> undeletableFiles = new List<string>(); //Liste der unlöschbaren Dateien
|
|
List<string> tempUndeletableFiles = new List<string>();
|
|
string tempUndelFile = null;
|
|
|
|
try
|
|
{
|
|
foreach (string fileName in System.IO.Directory.GetFiles(directoryName))
|
|
{
|
|
if (SearchPatternFile(fileName, searchPattern) && DaysOld(fileName, daysOld))
|
|
{
|
|
//Wenn Datei dem Suchpattern entspricht, dann löschen
|
|
tempUndelFile = File(fileName);
|
|
}
|
|
|
|
//Wenn ein Dateiname zurückgeliefert wird, dann in die Liste aufnehmen
|
|
if (tempUndelFile != null)
|
|
{
|
|
undeletableFiles.Add(tempUndelFile);
|
|
tempUndelFile = null; //Variable wieder zurücksetzen
|
|
}
|
|
}
|
|
|
|
if (recursive)
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
tempUndeletableFiles = File(dirName, searchPattern, daysOld, recursive);
|
|
|
|
if (tempUndeletableFiles.Count != 0)
|
|
{
|
|
undeletableFiles.AddRange(tempUndeletableFiles); //Unlöschbare Dateien zur Liste hinzufügen
|
|
tempUndeletableFiles.Clear(); //Liste wieder leeren
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return undeletableFiles;
|
|
}
|
|
#endregion
|
|
|
|
#region Directory
|
|
/// <summary>
|
|
/// Deletes the spicified directory. It is not checked whether subdirectories are included.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <returns>If the directory was successfully deleted, 'null' is returned, otherwise the directoryname.</returns>
|
|
public string Directory(string directoryName)
|
|
{
|
|
//Löscht das angegebene Verzeichnis
|
|
try
|
|
{
|
|
//Wert für ProgressBar berechnen
|
|
DirectoryCounter++; //Zähler hochzählen
|
|
if ((NumberOfFiles + NumberOfDirectories) == 0)
|
|
{
|
|
PercentDone = 0;
|
|
}
|
|
else
|
|
{
|
|
PercentDone = Convert.ToInt32(100 / Convert.ToDouble(NumberOfFiles + NumberOfDirectories) * Convert.ToDouble(FileCounter + DirectoryCounter));
|
|
}
|
|
if (PercentDone > 100)
|
|
{
|
|
PercentDone = 100; //Auf max 100% begrenzen
|
|
}
|
|
Status = String.Concat("Delete ", directoryName);
|
|
OnEvent(PercentDone, Status);
|
|
|
|
if (System.IO.Directory.Exists(directoryName))
|
|
{
|
|
//Damit das Verzeichnis gelöscht werden kann, erst alle Dateien löschen
|
|
File(directoryName, "");
|
|
|
|
//Schreibschutz aufheben
|
|
DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
|
|
dirInfo.Attributes = FileAttributes.Normal;
|
|
|
|
System.IO.Directory.Delete(directoryName, false);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
UndeletableDirectories.Add(directoryName); //Verzeichnis in die Liste der unlöschbaren Verzeichnisse aufnehmen
|
|
return directoryName; //Wenn löschen nicht möglich war
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes all directories in a directory that correspond to a particular pattern sequence.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "xyz*", "*xyz", "wx*yz", "*xyz*". ""=All files.</param>
|
|
/// <returns>If the directory was successfully deleted, 'null' is returned, otherwise the directoryname.</returns>
|
|
public List<string> Directory(string directoryName, string searchPattern)
|
|
{
|
|
//Löscht alle Verzeichnisse und Dateien im angegebenen Verzeichnis die dem Suchkriterium entsprechen
|
|
//Gültige Suchpattern: "", "xyz*", "*xyz", "wx*yz", "*xyz*"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
List<string> undeletableDirectories = new List<string>(); //Liste der unlöschbaren Verzeichnisse
|
|
string tempUndelDirectory = null;
|
|
|
|
try
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
if (SearchPatternDirectory(dirName, searchPattern))
|
|
{
|
|
//Wenn Verzeichnis dem Suchpattern entspricht, dann löschen
|
|
tempUndelDirectory = Directory(dirName);
|
|
}
|
|
|
|
//Wenn ein Verzeichnsname zurückgeliefert wird, dann in die Liste aufnehmen
|
|
if (tempUndelDirectory != null)
|
|
{
|
|
undeletableDirectories.Add(tempUndelDirectory);
|
|
tempUndelDirectory = null; //Variable wieder zurücksetzen
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return undeletableDirectories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes all directories in a directory that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "xyz*", "*xyz", "wx*yz", "*xyz*". ""=All files.</param>
|
|
/// <param name="daysOld">Days the directory must be old. 0=All directories are deleted.</param>
|
|
/// <returns>If the directory was successfully deleted, 'null' is returned, otherwise the directoryname.</returns>
|
|
public List<string> Directory(string directoryName, string searchPattern, int daysOld)
|
|
{
|
|
//Löscht alle Verzeichnisse und Dateien im angegebenen Verzeichnis die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", "xyz*", "*xyz", "wx*yz", "*xyz*"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
List<string> undeletableDirectories = new List<string>(); //Liste der unlöschbaren Verzeichnisse
|
|
string tempUndelDirectory = null;
|
|
|
|
try
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
if (SearchPatternDirectory(dirName, searchPattern) && DaysOld(dirName, daysOld))
|
|
{
|
|
//Wenn Verzeichnis dem Suchpattern entspricht, dann löschen
|
|
tempUndelDirectory = Directory(dirName);
|
|
}
|
|
|
|
//Wenn ein Verzeichnsname zurückgeliefert wird, dann in die Liste aufnehmen
|
|
if (tempUndelDirectory != null)
|
|
{
|
|
undeletableDirectories.Add(tempUndelDirectory);
|
|
tempUndelDirectory = null; //Variable wieder zurücksetzen
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return undeletableDirectories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes all directories in a directory and all subdirectories that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "xyz*", "*xyz", "wx*yz", "*xyz*". ""=All files.</param>
|
|
/// <param name="daysOld">Days the directory must be old. 0=All directories are deleted.</param>
|
|
/// <param name="recursive">True=Search in all sudirectories recursive. False=Search only in root directory.</param>
|
|
/// <returns></returns>
|
|
public List<string> Directory(string directoryName, string searchPattern, int daysOld, bool recursive)
|
|
{
|
|
//Löscht alle Verzeichnisse und Dateien im angegebenen Verzeichnis und allen Unterverzeichnissen
|
|
//die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", "xyz*", "*xyz", "wx*yz", "*xyz*"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
List<string> undeletableDirectories = new List<string>(); //Liste der unlöschbaren Verzeichnisse
|
|
List<string> tempUndeletableDirectories = new List<string>();
|
|
string tempUndelDirectory = null;
|
|
|
|
try
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
if (SearchPatternDirectory(dirName, searchPattern) && DaysOld(dirName, daysOld))
|
|
{
|
|
//Wenn Verzeichnis dem Suchpattern entspricht, dann löschen
|
|
tempUndelDirectory = Directory(dirName);
|
|
}
|
|
|
|
//Wenn ein Verzeichnsname zurückgeliefert wird, dann in die Liste aufnehmen
|
|
if (tempUndelDirectory != null)
|
|
{
|
|
undeletableDirectories.Add(tempUndelDirectory);
|
|
tempUndelDirectory = null; //Variable wieder zurücksetzen
|
|
}
|
|
}
|
|
|
|
if (recursive)
|
|
{
|
|
foreach (string dirName in System.IO.Directory.GetDirectories(directoryName))
|
|
{
|
|
tempUndeletableDirectories = Directory(dirName, searchPattern, daysOld, recursive);
|
|
|
|
if (tempUndeletableDirectories.Count != 0)
|
|
{
|
|
undeletableDirectories.AddRange(tempUndeletableDirectories); //Unlöschbare Verzeichnisse zur Liste hinzufügen
|
|
tempUndeletableDirectories.Clear(); //Liste wieder leeren
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
//Nichts tun, weil nicht notwendig
|
|
}
|
|
|
|
return undeletableDirectories;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Deletes all directories in a directory and all subdirectories and the directory itself that correspond to a particular pattern sequence and older than 'n'-days.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "xyz*", "*xyz", "wx*yz", "*xyz*". ""=All files.</param>
|
|
/// <param name="daysOld">Days the directory must be old. 0=All directories are deleted.</param>
|
|
/// <param name="recursive">True=Search in all sudirectories recursive. False=Search only in root directory.</param>
|
|
/// <param name="deleteItself">True=Delete also the root directory itself, False=Delete not the root directory itself.</param>
|
|
/// <returns></returns>
|
|
public List<string> Directory(string directoryName, string searchPattern, int daysOld, bool recursive, bool deleteItself)
|
|
{
|
|
//Löscht alle Verzeichnisse und Dateien im angegebenen Verzeichnis und allen Unterverzeichnissen
|
|
//die dem Suchkriterium entsprechen und älter als n-Tage sind
|
|
//Gültige Suchpattern: "", "xyz*", "*xyz", "wx*yz", "*xyz*"
|
|
//Groß-/Kleinschreibung wird ignoriert
|
|
|
|
List<string> undeletableDirectories = new List<string>(); //Liste der unlöschbaren Verzeichnisse
|
|
List<string> tempUndeletableDirectories = new List<string>();
|
|
string tempUndelDirectory = null;
|
|
|
|
tempUndeletableDirectories = Directory(directoryName, searchPattern, daysOld, recursive);
|
|
|
|
if (tempUndeletableDirectories.Count != 0)
|
|
{
|
|
undeletableDirectories.AddRange(tempUndeletableDirectories); //Unlöschbare Verzeichnisse zur Liste hinzufügen
|
|
tempUndeletableDirectories.Clear(); //Liste wieder leeren
|
|
}
|
|
|
|
if (deleteItself)
|
|
{
|
|
if (SearchPatternDirectory(directoryName, searchPattern) && DaysOld(directoryName, daysOld))
|
|
{
|
|
//Wenn Verzeichnis dem Suchpattern entspricht, dann löschen
|
|
tempUndelDirectory = Directory(directoryName);
|
|
}
|
|
|
|
if (tempUndelDirectory != null)
|
|
{
|
|
undeletableDirectories.Add(tempUndelDirectory); //Unlöschbares Verzeichnis zur Liste hinzufügen
|
|
tempUndelDirectory = null; //Zurücksetzen
|
|
}
|
|
}
|
|
|
|
return undeletableDirectories;
|
|
}
|
|
#endregion
|
|
|
|
#region Reset Counters
|
|
/// <summary>
|
|
/// Resets the file counter to 0.
|
|
/// </summary>
|
|
public static void ResetFileCounter()
|
|
{
|
|
FileCounter = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the directory counter to 0.
|
|
/// </summary>
|
|
public static void ResetDirectoryCounter()
|
|
{
|
|
DirectoryCounter = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the number of files to 0.
|
|
/// </summary>
|
|
public static void ResetNumberOfFiles()
|
|
{
|
|
NumberOfFiles = 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Resets the number of directories to 0.
|
|
/// </summary>
|
|
public static void ResetNumberOfDirectories()
|
|
{
|
|
NumberOfDirectories = 0;
|
|
}
|
|
#endregion
|
|
|
|
#region Clear
|
|
/// <summary>
|
|
/// Clears the undeletabele file list.
|
|
/// </summary>
|
|
public void ClearUndeletableFiles()
|
|
{
|
|
UndeletableFiles.Clear();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears the undeletabele directory list.
|
|
/// </summary>
|
|
public void ClearUndeletableDirectories()
|
|
{
|
|
UndeletableDirectories.Clear();
|
|
}
|
|
#endregion
|
|
|
|
#region Subroutines
|
|
/// <summary>
|
|
/// Checks whether the search pattern is included in the file name.
|
|
/// </summary>
|
|
/// <param name="fileName">A valid file name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", ".ext", "*.ext", "*xyz.ext", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All files.</param>
|
|
/// <returns>The search pattern is included in the file name=true, otherwise=false.</returns>
|
|
private bool SearchPatternFile(string fileName, string searchPattern)
|
|
{
|
|
string tempFileName = "";
|
|
string tempExtension = "";
|
|
string tempPattern = "";
|
|
string tempPattern1 = "";
|
|
bool match = false;
|
|
try
|
|
{
|
|
if (searchPattern == "")
|
|
{
|
|
match = true;
|
|
}
|
|
else if (searchPattern.StartsWith(".") | searchPattern.StartsWith("*."))
|
|
{
|
|
//Wenn mit '*', dann '*' entfernen
|
|
if (searchPattern.StartsWith("*"))
|
|
{
|
|
searchPattern = searchPattern.Replace("*", "");
|
|
}
|
|
|
|
//Nach Datei Erweiterung suchen
|
|
if (Path.GetExtension(fileName).ToLower() == searchPattern.ToLower())
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.StartsWith("*") & searchPattern.EndsWith("*"))
|
|
{
|
|
//Nach Teilstück suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempFileName.Contains(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.EndsWith("*"))
|
|
{
|
|
//Nach Anfang Suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempFileName.StartsWith(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.StartsWith("*") & searchPattern.Contains("."))
|
|
{
|
|
//Nach Teilstück suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
tempPattern = tempPattern.Remove(tempPattern.IndexOf("."));
|
|
tempExtension = Path.GetExtension(fileName).ToLower();
|
|
string test = searchPattern.Remove(0, searchPattern.IndexOf("."));
|
|
|
|
if (tempFileName.EndsWith(tempPattern) & searchPattern.Remove(0, searchPattern.IndexOf(".")) == tempExtension)
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.StartsWith("*"))
|
|
{
|
|
//Nach Ende Suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempFileName.EndsWith(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.Contains("*"))
|
|
{
|
|
//Nach Anfang und Ende Suchen
|
|
tempFileName = Path.GetFileNameWithoutExtension(fileName).ToLower();
|
|
tempPattern = searchPattern.Remove(searchPattern.IndexOf("*"));
|
|
tempPattern1 = searchPattern.Remove(0, searchPattern.IndexOf("*") + 1);
|
|
|
|
if (tempFileName.StartsWith(tempPattern) & tempFileName.EndsWith(tempPattern1))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
match = false;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
match = false;
|
|
}
|
|
|
|
return match;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks whether the search pattern is included in the directory name.
|
|
/// </summary>
|
|
/// <param name="directoryName">A valid directory name.</param>
|
|
/// <param name="searchPattern">Valid pattern sequence: "", "*xyz*"; "Xyz*", "*xyz", "wx*yz". ""=All directories.</param>
|
|
/// <returns>The search pattern is included in the directory name=true, otherwise=false.</returns>
|
|
private bool SearchPatternDirectory(string directoryName, string searchPattern)
|
|
{
|
|
string tempDirectoryName = "";
|
|
string tempPattern = "";
|
|
string tempPattern1 = "";
|
|
bool match = false;
|
|
try
|
|
{
|
|
if (searchPattern == "")
|
|
{
|
|
match = true;
|
|
}
|
|
else if (searchPattern.StartsWith("*") & searchPattern.EndsWith("*"))
|
|
{
|
|
//Nach Teilstück suchen
|
|
tempDirectoryName = Path.GetFileNameWithoutExtension(directoryName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempDirectoryName.Contains(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.EndsWith("*"))
|
|
{
|
|
//Nach Anfang Suchen
|
|
tempDirectoryName = Path.GetFileNameWithoutExtension(directoryName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempDirectoryName.StartsWith(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.StartsWith("*"))
|
|
{
|
|
//Nach Ende Suchen
|
|
tempDirectoryName = Path.GetFileNameWithoutExtension(directoryName).ToLower();
|
|
tempPattern = searchPattern.Replace("*", "").ToLower();
|
|
|
|
if (tempDirectoryName.EndsWith(tempPattern))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else if (searchPattern.Contains("*"))
|
|
{
|
|
//Nach Anfang und Ende Suchen
|
|
tempDirectoryName = Path.GetFileNameWithoutExtension(directoryName).ToLower();
|
|
tempPattern = searchPattern.Remove(searchPattern.IndexOf("*"));
|
|
tempPattern1 = searchPattern.Remove(0, searchPattern.IndexOf("*") + 1);
|
|
|
|
if (tempDirectoryName.StartsWith(tempPattern) & tempDirectoryName.EndsWith(tempPattern1))
|
|
{
|
|
match = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
match = false;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
match = false;
|
|
}
|
|
|
|
return match;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks how old the file is.
|
|
/// </summary>
|
|
/// <param name="fileName">A valid file name.</param>
|
|
/// <param name="daysOld">Days how old the element must be. 0=All.</param>
|
|
/// <returns>True=Is older as 'daysOld', otherwies=false.</returns>
|
|
private bool DaysOld(string fileName, int daysOld)
|
|
{
|
|
int calcDays = 0;
|
|
bool ok = false;
|
|
|
|
if (daysOld <= 0)
|
|
{
|
|
ok = true;
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
//Das Alter (letzter Zugriff) der Datei errechnen
|
|
DateTime dt = System.IO.File.GetLastAccessTime(fileName);
|
|
TimeSpan sp = DateTime.Now - dt;
|
|
calcDays = sp.Days;
|
|
|
|
if (calcDays > daysOld)
|
|
{
|
|
//Wenn die Datei älter als 'daysOld'-Tage ist
|
|
ok = true;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
ok = false;
|
|
}
|
|
}
|
|
|
|
return ok;
|
|
}
|
|
#endregion
|
|
|
|
#region Event Handler
|
|
// Der Delegat muß die gleiche Signatur aufweisen wie die Eventhandler-Methode.
|
|
public delegate void EventDelegate(int result, string status);
|
|
|
|
// Das Event-Objekt ist vom Typ dieses Delegaten.
|
|
public event EventDelegate DelStatus;
|
|
|
|
public void OnEvent(int result, string status)
|
|
{
|
|
// Prüft ob das Event überhaupt einen Abonnenten hat.
|
|
if (DelStatus != null)
|
|
{
|
|
DelStatus(result, status);
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
}
|