Fix STAThread, Dienst-Neustart nach Speichern, Pfad-Fix install-service
This commit is contained in:
parent
d996fd0254
commit
93f5c72ef2
4 changed files with 60 additions and 11 deletions
|
|
@ -10,10 +10,12 @@
|
|||
<AssemblyName>MailPrintConfig</AssemblyName>
|
||||
<SelfContained>true</SelfContained>
|
||||
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="System.ServiceProcess.ServiceController" Version="8.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -491,6 +491,17 @@ public class MainForm : Form
|
|||
?? Path.Combine(AppContext.BaseDirectory, "appsettings.json");
|
||||
}
|
||||
|
||||
private static bool IsServiceRunning()
|
||||
{
|
||||
try
|
||||
{
|
||||
var svc = System.ServiceProcess.ServiceController.GetServices()
|
||||
.FirstOrDefault(s => s.ServiceName == ServiceName);
|
||||
return svc?.Status == System.ServiceProcess.ServiceControllerStatus.Running;
|
||||
}
|
||||
catch { return false; }
|
||||
}
|
||||
|
||||
private void BrowseFolder(int rowIndex)
|
||||
{
|
||||
string? result = null;
|
||||
|
|
@ -714,6 +725,23 @@ public class MainForm : Form
|
|||
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
||||
File.WriteAllText(path, root.ToString(Formatting.Indented));
|
||||
SetStatus($"Gespeichert: {path}", Color.DarkGreen);
|
||||
|
||||
// Dienst-Neustart anbieten wenn Dienst läuft
|
||||
if (IsServiceRunning())
|
||||
{
|
||||
if (MessageBox.Show(
|
||||
"Konfiguration gespeichert.\n\nDienst jetzt neu starten um Änderungen zu übernehmen?",
|
||||
"Dienst neu starten?",
|
||||
MessageBoxButtons.YesNo,
|
||||
MessageBoxIcon.Question) == DialogResult.Yes)
|
||||
{
|
||||
_ = ServiceActionAsync("stop").ContinueWith(async _ =>
|
||||
{
|
||||
await Task.Delay(1500);
|
||||
await ServiceActionAsync("start");
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { SetStatus($"Fehler: {ex.Message}", Color.Red); }
|
||||
}
|
||||
|
|
@ -787,8 +815,8 @@ public class MainForm : Form
|
|||
{
|
||||
var publishDir = Path.GetDirectoryName(txtConfigPath.Text) ?? AppContext.BaseDirectory;
|
||||
var exePath = Path.Combine(publishDir, "MailPrint.exe");
|
||||
var installPs = Path.Combine(publishDir, "..", "install-service.ps1");
|
||||
var uninstallPs= Path.Combine(publishDir, "..", "uninstall-service.ps1");
|
||||
var installPs = Path.Combine(publishDir, "install-service.ps1");
|
||||
var uninstallPs = Path.Combine(publishDir, "uninstall-service.ps1");
|
||||
|
||||
// Bestätigung nur bei Install/Deinstall
|
||||
if (action is "install" or "uninstall")
|
||||
|
|
@ -818,12 +846,13 @@ public class MainForm : Form
|
|||
{
|
||||
FileName = "powershell",
|
||||
Arguments = $"-NoProfile -NonInteractive {cmd}",
|
||||
UseShellExecute = true,
|
||||
Verb = "runas",
|
||||
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
RedirectStandardError = true
|
||||
};
|
||||
|
||||
using var p = System.Diagnostics.Process.Start(psi)!;
|
||||
string err = await p.StandardError.ReadToEndAsync();
|
||||
await p.WaitForExitAsync();
|
||||
|
||||
if (p.ExitCode == 0)
|
||||
|
|
@ -840,7 +869,7 @@ public class MainForm : Form
|
|||
MessageBox.Show(successMsg, "Erfolgreich", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||||
}
|
||||
else
|
||||
SetStatus($"'{action}' fehlgeschlagen (Code {p.ExitCode})", Color.Red);
|
||||
SetStatus($"'{action}' fehlgeschlagen (Code {p.ExitCode}): {err.Trim()}", Color.Red);
|
||||
}
|
||||
catch (Exception ex) { SetStatus($"Fehler: {ex.Message}", Color.Red); }
|
||||
finally { DisableServiceButtons(false); RefreshStartStop(); }
|
||||
|
|
|
|||
|
|
@ -1,5 +1,12 @@
|
|||
using MailPrintConfig;
|
||||
|
||||
class Program
|
||||
{
|
||||
[STAThread]
|
||||
static void Main()
|
||||
{
|
||||
Application.EnableVisualStyles();
|
||||
Application.SetCompatibleTextRenderingDefault(false);
|
||||
Application.Run(new MainForm());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
MailPrintConfig/app.manifest
Normal file
11
MailPrintConfig/app.manifest
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<assemblyIdentity version="1.0.0.0" name="MailPrintConfig.exe"/>
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
|
||||
<security>
|
||||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
|
||||
</requestedPrivileges>
|
||||
</security>
|
||||
</trustInfo>
|
||||
</assembly>
|
||||
Loading…
Add table
Add a link
Reference in a new issue