250 lines
8.7 KiB
C#
250 lines
8.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Eugen.ESystem.IO
|
|
{
|
|
public class ReadFoldersFromFolder
|
|
{
|
|
#region Version und Copyright
|
|
// Version und Copyright
|
|
static string dllName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
|
|
static string dllVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
|
|
static string copyright = GenerateCopyright();
|
|
//Assembly Datum und Zeit
|
|
static DateTime value = AssemblyDateTime();
|
|
string date = value.ToShortDateString();
|
|
string time = value.ToLongTimeString();
|
|
private static DateTime AssemblyDateTime()
|
|
{
|
|
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
|
|
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
|
|
return buildDateTime;
|
|
}
|
|
|
|
private static string CurrentYear()
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
|
|
private static string StartYear()
|
|
{
|
|
string startYear = "";
|
|
if (((AssemblyCopyrightAttribute)attributes[0]).Copyright.Contains("Copyright © "))
|
|
{
|
|
startYear = ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace("Copyright © ", "");
|
|
while (startYear.StartsWith(" "))
|
|
{
|
|
startYear = startYear.Remove(0, 1);
|
|
}
|
|
|
|
if (startYear.Contains(" "))
|
|
{
|
|
startYear = startYear.Remove(startYear.IndexOf(" "));
|
|
}
|
|
|
|
return startYear;
|
|
}
|
|
else
|
|
{
|
|
DateTime dtn = DateTime.Now;
|
|
return dtn.Year.ToString();
|
|
}
|
|
}
|
|
|
|
private static string GenerateCopyright()
|
|
{
|
|
string startYear = StartYear();
|
|
string currentYear = CurrentYear();
|
|
|
|
if (startYear == currentYear)
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
}
|
|
else
|
|
{
|
|
return ((AssemblyCopyrightAttribute)attributes[0]).Copyright.Replace(startYear, String.Concat(startYear, "-", currentYear));
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region DLL-Info
|
|
/// <summary>
|
|
/// Contains the name of the dll file.
|
|
/// </summary>
|
|
public static string DllName { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the dll file.
|
|
/// </summary>
|
|
public static string DllVersion { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice.
|
|
/// </summary>
|
|
public static string Copyright { private set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the name-, version- and copyright-information of the dll file.
|
|
/// </summary>
|
|
public static string[] DllInfo { private set; get; }
|
|
|
|
private static void SetDllInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
DllName = dllName;
|
|
DllVersion = dllVersion;
|
|
Copyright = copyright;
|
|
|
|
string[] dllInfo = new string[3];
|
|
dllInfo[0] = dllName;
|
|
dllInfo[1] = dllVersion;
|
|
dllInfo[2] = copyright;
|
|
DllInfo = dllInfo;
|
|
}
|
|
|
|
protected string ProgramName { private set; get; }
|
|
protected string ProgramVersion { private set; get; }
|
|
protected string ProgramCopyright { private set; get; }
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// Reads all directories from a directory. Serves only to set the DLL information.
|
|
/// </summary>
|
|
static ReadFoldersFromFolder()
|
|
{
|
|
SetDllInfo();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads all directories from a directory.
|
|
/// </summary>
|
|
/// <param name="folder">A valid directory which is to be read out.</param>
|
|
public ReadFoldersFromFolder(string folder)
|
|
{
|
|
SetDllInfo();
|
|
|
|
ClearFolderFound();
|
|
|
|
try
|
|
{
|
|
FoldersFound = ReadFolder(folder, "", false, false);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads all directories from a directory.
|
|
/// </summary>
|
|
/// <param name="folder">A valid directory which is to be read out.</param>
|
|
/// <param name="pattern">Only directories containing the specified pattern will be returned.</param>
|
|
public ReadFoldersFromFolder(string folder, string pattern)
|
|
{
|
|
SetDllInfo();
|
|
|
|
ClearFolderFound();
|
|
|
|
try
|
|
{
|
|
FoldersFound = ReadFolder(folder, pattern, false, false);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads all directories from a directory.
|
|
/// </summary>
|
|
/// <param name="folder">A valid directory which is to be read out.</param>
|
|
/// <param name="pattern">Only directories containing the specified pattern will be returned.</param>
|
|
/// <param name="startsWithPattern">If the value is true, then the pattern is searched at the beginning of the directory name. Only 'startsWithPattern' or 'endsWithPattern' may be true, never both values.</param>
|
|
/// <param name="endsWithPattern">If the value is true, then the pattern is searched at the end of the directory name.</param>
|
|
public ReadFoldersFromFolder(string folder, string pattern, bool startsWithPattern, bool endsWithPattern)
|
|
{
|
|
SetDllInfo();
|
|
|
|
ClearFolderFound();
|
|
|
|
try
|
|
{
|
|
FoldersFound = ReadFolder(folder, pattern, startsWithPattern, endsWithPattern);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// List of all found directories.
|
|
/// </summary>
|
|
public List<string> FoldersFound { private set; get; }
|
|
|
|
private void ClearFolderFound()
|
|
{
|
|
if (FoldersFound != null)
|
|
{
|
|
FoldersFound.Clear(); //Liste löschen
|
|
}
|
|
}
|
|
|
|
private List<string> ReadFolder(string folder, string pattern, bool startsWithPattern, bool endsWithPattern)
|
|
{
|
|
if (startsWithPattern & endsWithPattern)
|
|
{
|
|
throw new ArgumentException();
|
|
}
|
|
|
|
List<string> folders = new List<string>();
|
|
|
|
if (!Directory.Exists(folder))
|
|
{
|
|
throw new DirectoryNotFoundException();
|
|
}
|
|
else
|
|
{
|
|
//Alle Verzeichnisse einlesen
|
|
foreach (var item in Directory.GetDirectories(folder))
|
|
{
|
|
//Prüfen ob 'pattern' gesetzt ist
|
|
if (String.IsNullOrEmpty(pattern))
|
|
{
|
|
folders.Add(item); //Wenn kein 'pattern' gesetzt ist, dann zur Liste hinzufügen
|
|
}
|
|
else
|
|
{
|
|
if (startsWithPattern && item.Remove(0, item.LastIndexOf("\\") + 1).StartsWith(pattern))
|
|
{
|
|
folders.Add(item); //Wenn ein 'pattern' am Anfang gesetzt ist, dann zur List hinzufügen, wenn das Verzeichnis das 'pattern' enthält
|
|
}
|
|
|
|
if (endsWithPattern && item.Remove(0, item.LastIndexOf("\\")).EndsWith(pattern))
|
|
{
|
|
folders.Add(item); //Wenn ein 'pattern' am Ende gesetzt ist, dann zur List hinzufügen, wenn das Verzeichnis das 'pattern' enthält
|
|
}
|
|
|
|
if ((!startsWithPattern & !endsWithPattern) & item.Remove(0, item.LastIndexOf("\\") + 1).Contains(pattern))
|
|
{
|
|
folders.Add(item); //Wenn ein 'pattern' gesetzt ist, dann zur List hinzufügen, wenn das Verzeichnis das 'pattern' enthält
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return folders;
|
|
}
|
|
}
|
|
}
|