HR-Collector/HRServer-Exporter/Installer/FRMInstaller.cs
2024-12-31 16:35:06 +01:00

284 lines
11 KiB
C#

using System.Diagnostics;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
namespace Installer
{
public partial class FRMInstaller : Form
{
private string googleDriveFolderPath = string.Empty;
private string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
private string serverExecutableFileName = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\HR-Exporter\HRServer.exe";
public FRMInstaller()
{
InitializeComponent();
}
private void btn_next_Click(object sender, EventArgs e)
{
this.tablessTabControl.SelectedIndex++;
if (this.tablessTabControl.SelectedIndex == 2)
{
if (!checkBox_GoogleDrive.Checked && !checkBox2.Checked)
{
tablessTabControl.SelectedIndex--;
MessageBox.Show("Please select at least one option.");
return;
}
this.tablessTabControl.SelectedIndex++;
btn_next_Click(sender, e);
return;
bool isDotNet8Installed = IsDotNet8InstalledCrossPlatform();
bool isAspNetCore8Installed = IsAspNetCore8InstalledCrossPlatform();
if (!isDotNet8Installed)
{
lbl_dotnet8installed.Text = "❌ .NET 8.0 is not installed!";
}
else
{
lbl_dotnet8installed.Text = "✅ .NET 8.0 is installed!";
}
if (!isAspNetCore8Installed)
{
lbl_aspnet8installed.Text = "❌ ASP.NET Core 8.0 is not installed!";
}
else
{
lbl_aspnet8installed.Text = "✅ ASP.NET Core 8.0 is installed!";
}
if (!isDotNet8Installed || !isAspNetCore8Installed)
{
btn_installRuntimes.Visible = true;
lbl_runtimeinfo.Visible = true;
lbl_runtimeinfo.Text = "To be able to run the application, you're required to update to the newest runtimes.";
}
else if (isDotNet8Installed && isAspNetCore8Installed)
{
btn_next2.Enabled = true;
lbl_runtimeinfo.Visible = true;
lbl_runtimeinfo.Text = "You have the newest runtimes installed! Please continue.";
}
lbl_aspnet8installed.Visible = true;
lbl_dotnet8installed.Visible = true;
}
else if (this.tablessTabControl.SelectedIndex == 3)
{
CopyDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\Server", appDataPath + @"\HR-Exporter", true);
string serviceName = "HRServer";
ExecuteCommand($"create \"{serviceName}\" binPath= \"{serverExecutableFileName}\" start= auto");
ExecuteCommand($"start \"{serviceName}\"");
Console.WriteLine($"The service \"{serviceName}\" was created successfully and started.");
var filePath = "HorseCollection.xlsm";
try
{
// Mit "Streams" den Zone.Identifier löschen
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = $"/c echo. > \"{filePath}:Zone.Identifier\"";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit();
Console.WriteLine("Dateiblock wurde erfolgreich entfernt.");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
if (checkBox_GoogleDrive.Checked)
{
File.WriteAllText("GoogleDrive.dat", googleDriveFolderPath);
}
}
}
static void ExecuteCommand(string arguments)
{
var process = Process.Start(new ProcessStartInfo
{
FileName = "sc",
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
});
process.WaitForExit();
if (process.ExitCode != 0 && process.ExitCode != 1073)
{
throw new Exception(process.StandardError.ReadToEnd());
}
}
public void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
{
MessageBox.Show("There was an issue running the installer. Please make sure that you're running the installer from within the root directory.");
Environment.Exit(-1);
return;
}
// Create the destination directory if it doesn't exist
Directory.CreateDirectory(destinationDir);
// Copy all the files
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath, overwrite: true);
}
// If recursive, copy subdirectories
if (recursive)
{
foreach (DirectoryInfo subDir in dir.GetDirectories())
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, recursive);
}
}
}
public static bool IsDotNet8InstalledCrossPlatform()
{
var versionPattern = "Microsoft.NETCore.App 8.0.";
var output = RunDotnetListRuntimes();
// Wenn Zeile mit "Microsoft.NETCore.App 8.0." vorkommt => .NET 8 ist installiert
return output.Contains(versionPattern);
}
public static bool IsAspNetCore8InstalledCrossPlatform()
{
var versionPattern = "Microsoft.AspNetCore.App 8.0.";
var output = RunDotnetListRuntimes();
return output.Contains(versionPattern);
}
private static string RunDotnetListRuntimes()
{
var psi = new ProcessStartInfo("dotnet", "--list-runtimes")
{
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
using var process = new Process { StartInfo = psi };
process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
private void btn_installRuntimes_Click(object sender, EventArgs e)
{
var psi = new ProcessStartInfo
{
FileName = "winget",
Arguments = "install --id Microsoft.DotNet.SDK.8 --scope machine -e",
UseShellExecute = true,
Verb = "runas" // => UAC-Prompt erscheint
};
using var process = new Process { StartInfo = psi };
try
{
process.Start();
// Hier warten wir, bis winget-Installation abgeschlossen ist
process.WaitForExit();
// Optional: Winget-Exitcode auswerten (0 = Erfolg, != 0 = Fehler)
int exitCode = process.ExitCode;
if (exitCode != 0)
{
MessageBox.Show($"winget-Installation schlug fehl. Exitcode={exitCode}");
return;
}
// Jetzt prüfen, ob .NET 8 installiert ist
if (IsDotNet8InstalledCrossPlatform())
{
MessageBox.Show(".NET 8 wurde erfolgreich installiert!");
}
else
{
MessageBox.Show("Es sieht so aus, als wäre .NET 8 nicht installiert.");
}
}
catch (Exception ex)
{
MessageBox.Show($"Fehler: {ex.Message}");
}
var psi2 = new ProcessStartInfo
{
FileName = "winget",
Arguments = "install --id Microsoft.AspNetCore.App.8 --scope machine -e",
UseShellExecute = true,
Verb = "runas" // => UAC-Prompt erscheint
};
using var process2 = new Process { StartInfo = psi2 };
try
{
process2.Start();
// Hier warten wir, bis winget-Installation abgeschlossen ist
process2.WaitForExit();
// Optional: Winget-Exitcode auswerten (0 = Erfolg, != 0 = Fehler)
int exitCode = process2.ExitCode;
if (exitCode != 0)
{
MessageBox.Show($"winget-Installation schlug fehl. Exitcode={exitCode}");
return;
}
// Jetzt prüfen, ob .NET 8 installiert ist
if (IsAspNetCore8InstalledCrossPlatform())
{
MessageBox.Show("ASP.NET Core 8 wurde erfolgreich installiert!");
}
else
{
MessageBox.Show("Es sieht so aus, als wäre ASP.NET Core 8 nicht installiert.");
}
}
catch (Exception ex)
{
MessageBox.Show($"Fehler: {ex.Message}");
}
btn_next2.Enabled = true;
}
private void checkBox_GoogleDrive_checked(object sender, EventArgs e)
{
if (checkBox_GoogleDrive.Checked)
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.Description = "Select the folder where the Google Drive file is going to be located at.";
if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
{
googleDriveFolderPath = folderBrowserDialog.SelectedPath;
lbl_GoogleDrivePath.Text = googleDriveFolderPath;
lbl_GoogleDrivePath.Visible = true;
}
}
else
{
googleDriveFolderPath = string.Empty;
lbl_GoogleDrivePath.Visible = false;
lbl_GoogleDrivePath.Text = string.Empty;
}
}
private void btnDone_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}