PEPTool/AV-ToolV3/Reader.cs
2025-10-08 10:11:18 +02:00

146 lines
5.0 KiB
C#

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace PEP_Tool
{
public static class Reader
{
public static string OpenFile(string Dialogname = "Datei")
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.RestoreDirectory = true;
dialog.Title = $"{Dialogname} öffnen";
dialog.Filter = "Excel Arbeitsblatt CSV|*.csv";
dialog.ShowDialog();
var result = dialog.FileName;
if (string.IsNullOrEmpty(result))
return OpenFile(Dialogname);
else
return result;
}
internal static List<string> ReadZuweisungsMoeglichkeiten()
{
var path = "";
if (File.Exists(@"\\mhb00swfs003v.fv-werke.db.de\GLW99\PEP\Zuweisungen.csv")) path = @"\\mhb00swfs003v.fv-werke.db.de\GLW99\PEP\Zuweisungen.csv";
else
path = Properties.Settings.Default.ZuweisungenPath == "" ? OpenFile("Zuweisungen") : Properties.Settings.Default.ZuweisungenPath;
if (!File.Exists(path)) path = OpenFile("Zuweisungen");
if (!File.ReadAllText(path, Encoding.GetEncoding(1250)).Contains(";"))
{
if (path != Properties.Settings.Default.ZuweisungenPath)
{
Properties.Settings.Default.ZuweisungenPath = path;
Properties.Settings.Default.Save();
}
var list = File.ReadAllLines(path, Encoding.GetEncoding(1250)).ToList();
list.Insert(0, "");
return list;
}
else
{
MessageBox.Show("Keine korrekte Zuweisungen-Datei ausgewählt", "Fehler", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
return ReadZuweisungsMoeglichkeiten();
}
}
internal static Dictionary<string, Tuple<string, string>> ReadAbteilungsNamen()
{
var path = "";
if (File.Exists(@"\\mhb00swfs003v.fv-werke.db.de\GLW99\PEP\DKEYS.csv")) path = @"\\mhb00swfs003v.fv-werke.db.de\GLW99\PEP\DKEYS.csv";
else
path = Properties.Settings.Default.AbteilungsnamenPath == "" ? OpenFile("Abteilungsnamen") : Properties.Settings.Default.AbteilungsnamenPath;
if (!File.Exists(path)) path = OpenFile("Abteilungsnamen");
if (File.ReadAllLines(path).First().StartsWith("Abteilung;"))
{
if (path != Properties.Settings.Default.AbteilungsnamenPath)
{
Properties.Settings.Default.AbteilungsnamenPath = path;
Properties.Settings.Default.Save();
}
Dictionary<string, Tuple<string, string>> list = new Dictionary<string, Tuple<string, string>>();
foreach (var line in File.ReadAllLines(path))
{
list.Add(line.Split(';')[0], new Tuple<string, string>(line.Split(';')[1], line.Split(';')[2]));
}
return list;
}
else
{
MessageBox.Show("Keine korrekte Abteilungsnamen-Datei ausgewählt", "Fehler", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
return ReadAbteilungsNamen();
}
}
internal static List<sWork> ReadsWork()
{
var path = "";
//if (File.Exists(@"\\mhb00swfs003v.fv-werke.db.de\GLW99\PEP\sWork.csv")) path = @"\\mhb00swfs003v.fv-werke.db.de\GLW99\PEP\sWork.csv";
//else
// MessageBox.Show("Standardarbeiten nicht gefunden\nBitte an Marcus Bachler wenden", "Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
//if (!File.Exists(path)) path = OpenFile("Abteilungsnamen");
var list = new List<sWork>();
foreach (var line in File.ReadAllLines(path).Skip(1))
{
list.Add(new sWork() { Title = line.Split(';')[0], Work = line.Split(';')[1], Schaltzustand = line.Split(';')[2] });
}
return list;
}
}
public static class Extensions
{
/// <summary>
/// Get the array slice between the two indexes.
/// ... Inclusive for start index, exclusive for end index.
/// </summary>
public static T[] Slice<T>(this T[] source, int start, int end)
{
// Handles negative ends.
if (end < 0)
{
end = source.Length + end;
}
int len = end - start;
// Return new array.
T[] res = new T[len];
for (int i = 0; i < len; i++)
{
res[i] = source[i + start];
}
return res;
}
}
}