readfilelinebyline_dll_visu.../ReadFileLineByLine/ReadFileLineByLine.cs
Eugen Höglinger 8589f01b02 Änderung
- Assembly name korrigiert
- Namespace korrigiert
2021-08-26 16:15:33 +02:00

161 lines
5.2 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Eugen.ESystem.IO
{
public class ReadFileLineByLine
{
#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();
string icon = Application.StartupPath + "\\Info.bmp";
//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);
}
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 program file
/// </summary>
public static string DllName { set; get; }
/// <summary>
/// Contains the version of the program file
/// </summary>
public static string DllVersion { set; get; }
/// <summary>
/// Contains the copyright notice
/// </summary>
public static string Copyright { set; get; }
private static void SetDllInfo()
{
//Name, Version und Copyright setzen
DllName = dllName;
DllVersion = dllVersion;
Copyright = copyright;
}
#endregion
/// <summary>
/// Read a text file line by line and store each line in a list.
/// </summary>
static ReadFileLineByLine()
{
SetDllInfo(); //Name, Version und Copyright setzen
}
private void FileExist(string fileName)
{
//Wenn der Dateiname nicht existiert, dann eine Exception werfen
if (!File.Exists(fileName))
{
throw new FileNotFoundException(fileName);
}
}
/// <summary>
/// Reads the specified file line by line
/// </summary>
/// <param name="fileName">A valid file name with full path.</param>
/// <returns></returns>
public static List<string> Read(string fileName)
{
List<string> lines = new List<string>();
if (lines != null)
{
lines.Clear(); //Liste leeren
}
try
{
foreach (string line in File.ReadAllLines(fileName, Encoding.Default))
{
lines.Add(line);
}
}
catch
{
//Wenn nicht bis zum Ende gelesen werden kann, dann eine Exception werfen
throw new CannotReadFileToEndException(fileName);
}
return lines;
}
}
#region Exception
public class CannotReadFileToEndException : ApplicationException
{
public CannotReadFileToEndException()
{ }
public CannotReadFileToEndException(string message) : base(message)
{ }
public CannotReadFileToEndException(string message, Exception inner) : base(message, inner)
{ }
}
#endregion
}