HR-Collector/HRServer-Exporter/Installer/FRMInstaller.cs
2025-01-19 12:57:31 +01:00

145 lines
5.8 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)
{
if (!checkBox_GoogleDrive.Checked && !checkBox_MicrosoftExcel.Checked && tablessTabControl.SelectedTab.Text == "tabPage_Selection")
{
MessageBox.Show("Please select at least one option.");
return;
}
this.tablessTabControl.SelectedIndex++;
}
private void tablessTabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.tablessTabControl.SelectedTab.Text == "tabPage_Finish")
{
if (checkBox_GoogleDrive.Checked)
{
File.WriteAllText("GoogleDrive.dat", googleDriveFolderPath);
}
if (checkBox_MicrosoftExcel.Checked)
{
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}");
return;
}
}
CopyDirectory(AppDomain.CurrentDomain.BaseDirectory + @"\Server", appDataPath + @"\HR-Exporter", true);
// Create the service
string serviceName = "HRServer";
ExecuteCommand($"create \"{serviceName}\" binPath= \"{serverExecutableFileName}\" start= auto");
ExecuteCommand($"start \"{serviceName}\"");
Console.WriteLine($"The service \"{serviceName}\" was created successfully and started.");
}
}
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);
}
}
}
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
{
checkBox_GoogleDrive.Checked = false;
}
}
else
{
googleDriveFolderPath = string.Empty;
lbl_GoogleDrivePath.Visible = false;
lbl_GoogleDrivePath.Text = string.Empty;
}
}
private void btnDone_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}