Programm_Wirl/Program.cs

209 lines
7.3 KiB
C#

using DatenDB;
using Deckungsbeitrag.AA_Forms;
using Deckungsbeitrag.Properties;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Deckungsbeitrag
{
static class Program
{
public static ImageList imagelist = new ImageList();
public static string userid;
private static bool istest = false;
/// <summary>
/// Der Haupteinstiegspunkt für die Anwendung.
/// </summary>
[STAThread]
static void Main()
{
userid = ConfigurationManager.AppSettings["ConnectionString"].Split(';')[0];
userid = userid.Split('=')[1];
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Screen[] screens = Screen.AllScreens;
if (CheckAndInstallUpdate())
{
return;
}
ImageList_Laden();
string readmePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Verwaltung_ReadMe.txt");
// Prüfe, ob ReadMe schon einmal geöffnet wurde
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\KilianWirlGmbH\Verwaltung"))
{
if (key == null || key.GetValue("ReadMeShown") == null)
{
// Erstes Mal nach Installation
if (File.Exists(readmePath))
{
Process.Start(new ProcessStartInfo
{
FileName = readmePath,
UseShellExecute = true
});
// WICHTIG: Warte 2 Sekunden (ReadMe öffnen)
System.Threading.Thread.Sleep(2000);
// Abbruch-Dialog
DialogResult result = MessageBox.Show(
"ReadMe wurde geöffnet.\n\n" +
"Falls INSTALL-REMINDER noch Änderungen enthält,\n" +
"soll die Anwendung jetzt ABGEBROCHEN werden?\n\n" +
"[JA = Abbruch | NEIN = Fortfahren]",
"Nach Installation - Update prüfen",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1 // Standard: Abbruch
);
if (result == DialogResult.Yes)
{
MessageBox.Show("Installation wurde abgebrochen.\nBitte Änderungen vornehmen und neu installieren.",
"Abbruch", MessageBoxButtons.OK, MessageBoxIcon.Information);
return; // App beenden
}
// Markiere als gezeigt
Registry.CurrentUser.CreateSubKey(@"Software\KilianWirlGmbH\Verwaltung")
.SetValue("ReadMeShown", 1);
}
}
}
if (istest) { Application.Run(new FormAufleger()); return; }
if (userid.StartsWith("ms") | userid.StartsWith("ps"))
{
if (userid.Contains("wstrasse")) Application.Run(new FormWSVerfolgung(userid));
else Application.Run(new FormFehlmengeCount(userid));
}
else
{
FormLogin loginForm = new FormLogin();
if (loginForm.ShowDialog() == DialogResult.OK)
{
// Switch on BenutzerRolle für Weiterleitung zum richtigen Screen
switch (loginForm.Person.Rolle)
{
case BenutzerRolle.Verwaltung: //Büropersonal ohne aktive Hallenbeschäftigung
Application.Run(new FormMain(loginForm.Person));
break;
case BenutzerRolle.Fahrer: //Zustellfahrer
Application.Run(new FormMain(loginForm.Person));
break;
case BenutzerRolle.Admin: //Höheres Büropersonal oder Claudia und Kevin
Application.Run(new FormMain(loginForm.Person));
break;
case BenutzerRolle.Waschstrasse: //Bediener einer Waschstraße
Application.Run(new FormNeuerAuftrag(loginForm.Person, AuftragTyp.Standart));
break;
case BenutzerRolle.Master: //Entwickler oder Claudia und Kevin
Application.Run(new FormMain(loginForm.Person));
break;
case BenutzerRolle.Expedit: //Endkontrolle Lieferungen und Lieferscheine (Mirka)
Application.Run(new FormExpedit(loginForm.Person, screens));
break;
case BenutzerRolle.Frottee: //Containerbestückung Frotteewäsche
//BenutzerRolle Frottee = Expedit Frottee da die Maschine keine Anmeldung erfordert.
Application.Run(new FormExpedit(loginForm.Person, screens));
break;
case BenutzerRolle.Flach: //Containerbestückung Flachwäsche
//BenutzerRolle Flach = Expedit Frottee da die Maschine keine Anmeldung erfordert.
Application.Run(new FormExpedit(loginForm.Person, screens));
break;
default:
break;
}
}
}
}
private static bool CheckAndInstallUpdate()
{
string currentVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
string updatePath = @"N:\TECHNIK\Software\Wirl-Verwaltung\Setup"; // Ihr interner UNC-Pfad
try
{
string newVersion = File.ReadAllText(Path.Combine(updatePath, "Version.txt")).Trim();
if (new Version(newVersion).CompareTo(new Version(currentVersion)) > 0)
{
DialogResult result = MessageBox.Show(
$"Update verfügbar: {newVersion}\nInstallieren?",
"Update verfügbar",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
string msiPath = Path.Combine(updatePath, "SetupVerwaltung.msi");
if (result == DialogResult.Yes)
{
try
{
// 1. Laufende Prozesse killen
//foreach (var proc in Process.GetProcessesByName("Deckungsbeitrag"))
// try { proc.Kill(); } catch { }
// 2. MSI lokal kopieren (umgeht UNC-Fehler 2203)
string localMsi = Path.Combine(Path.GetTempPath(), "SetupVerwaltung.msi");
File.Copy(msiPath, localMsi, true);
// 3. MSI starten
var psi = new ProcessStartInfo
{
FileName = "msiexec.exe",
Arguments = $"/i \"{localMsi}\" /qn /norestart ALLUSERS=1 /L*v \"C:\\tmp\\update.log\"",
Verb = "runas",
UseShellExecute = true
};
Process.Start(psi)?.Dispose();
return true;
}
catch (Exception ex)
{
MessageBox.Show($"Start fehlgeschlagen: {ex.Message}");
}
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Update-Prüfung fehlgeschlagen: {ex.Message}\nPfad: {updatePath}",
"Fehler", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
return false;
}
private static void ImageList_Laden()
{
imagelist.Images.Add(Resources.Exclamation_Red);
imagelist.Images.Add(Resources.InformationSymbol_16x);
imagelist.Images.Add("print", Resources.PrintTag1);
imagelist.Images.Add("abschließen", Resources.checkmark);
imagelist.Images.Add("dirt", Resources.dirt);
imagelist.Images.Add("clean", Resources.clean);
}
}
}