diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..ec0526e --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,42 @@ +name: Build + +on: + push: + branches: + - master + - v2-modernization + pull_request: + branches: + - master + +permissions: + contents: read + +jobs: + build: + runs-on: windows-latest + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup .NET 10 + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 10.0.x + + - name: Restore + run: dotnet restore PrinterManagerNativeTool.sln + + - name: Build + run: dotnet build PrinterManagerNativeTool.sln -c Release --no-restore + + - name: Publish portable x64 + run: dotnet publish PrinterManagerNativeTool/PrinterManagerNativeTool.csproj -c Release -r win-x64 --self-contained true -p:PublishSingleFile=true --no-restore -o artifacts/win-x64 + + - name: Upload portable executable + uses: actions/upload-artifact@v4 + with: + name: PrinterManager-win-x64 + path: artifacts/win-x64/PrinterManagerNativeTool.exe + if-no-files-found: error diff --git a/PrinterManagerNativeTool/App.xaml b/PrinterManagerNativeTool/App.xaml new file mode 100644 index 0000000..5efad71 --- /dev/null +++ b/PrinterManagerNativeTool/App.xaml @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/PrinterManagerNativeTool/App.xaml.cs b/PrinterManagerNativeTool/App.xaml.cs new file mode 100644 index 0000000..27799de --- /dev/null +++ b/PrinterManagerNativeTool/App.xaml.cs @@ -0,0 +1,64 @@ +using System.Windows; +using PrinterManagerNativeTool.Services; + +namespace PrinterManagerNativeTool; + +public partial class App : Application +{ + protected override async void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + if (e.Args.Length >= 2 && + string.Equals(e.Args[0], "--admin", StringComparison.OrdinalIgnoreCase)) + { + await RunAdministrativeActionAsync(e.Args[1]); + Shutdown(); + return; + } + + var window = new MainWindow(); + MainWindow = window; + window.Show(); + } + + private static async Task RunAdministrativeActionAsync(string action) + { + var spooler = new SpoolerService(); + + try + { + switch (action.ToLowerInvariant()) + { + case "restart-spooler": + await spooler.RestartAsync(); + MessageBox.Show( + "Spooler di stampa riavviato correttamente.", + "Printer Manager", + MessageBoxButton.OK, + MessageBoxImage.Information); + break; + + case "clear-spooler": + await spooler.ClearAsync(); + MessageBox.Show( + "Coda spooler pulita correttamente.", + "Printer Manager", + MessageBoxButton.OK, + MessageBoxImage.Information); + break; + + default: + throw new ArgumentException($"Azione amministrativa sconosciuta: {action}"); + } + } + catch (Exception ex) + { + MessageBox.Show( + ex.Message, + "Printer Manager - Errore", + MessageBoxButton.OK, + MessageBoxImage.Error); + } + } +} diff --git a/PrinterManagerNativeTool/Form1.cs b/PrinterManagerNativeTool/Form1.cs deleted file mode 100644 index 203afe2..0000000 --- a/PrinterManagerNativeTool/Form1.cs +++ /dev/null @@ -1,200 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -using System.Drawing; -using System.Linq; -using System.Threading.Tasks; -using System.Windows.Forms; - - -namespace PrinterManagerNativeTool -{ - public partial class Form1 : Form - { - private ListBox lstPrinters; - private Button btnRicarica; - private Button btnRiavviaSpooler; - private Button btnSvuotaSpooler; - private Label lblAdminWarning; - private ContextMenuStrip contextMenu; - - public Form1() - { - InitializeComponent(); - _ = CaricaStampanti(); - } - - private void InitializeComponent() - { - this.lstPrinters = new ListBox(); - this.btnRicarica = new Button(); - this.btnRiavviaSpooler = new Button(); - this.btnSvuotaSpooler = new Button(); - this.lblAdminWarning = new Label(); - this.contextMenu = new ContextMenuStrip(); - - // - // lstPrinters - // - this.lstPrinters.Location = new Point(10, 10); - this.lstPrinters.Size = new Size(380, 250); - this.lstPrinters.ContextMenuStrip = this.contextMenu; - this.lstPrinters.MouseDown += LstPrinters_MouseDown; - - // - // btnRicarica - // - this.btnRicarica.Text = "Ricarica stampanti"; - this.btnRicarica.Location = new Point(10, 270); - this.btnRicarica.Size = new Size(380, 30); - this.btnRicarica.Click += BtnLoad_Click; - - // - // btnRiavviaSpooler - // - this.btnRiavviaSpooler.Text = "Riavvia spooler"; - this.btnRiavviaSpooler.Location = new Point(10, 310); - this.btnRiavviaSpooler.Size = new Size(185, 30); - this.btnRiavviaSpooler.Click += BtnRiavviaSpooler_Click; - - // - // btnSvuotaSpooler - // - this.btnSvuotaSpooler.Text = "Svuota spooler"; - this.btnSvuotaSpooler.Location = new Point(205, 310); - this.btnSvuotaSpooler.Size = new Size(185, 30); - this.btnSvuotaSpooler.Click += BtnSvuotaSpooler_Click; - - // - // lblAdminWarning - // - this.lblAdminWarning.Text = "⚠ Alcune funzioni richiedono i permessi da amministratore."; - this.lblAdminWarning.Font = new Font("Segoe UI", 7F, FontStyle.Italic); - this.lblAdminWarning.ForeColor = Color.Gray; - this.lblAdminWarning.AutoSize = true; - this.lblAdminWarning.Location = new Point(10, 345); - - // - // contextMenu - // - var preferenze = new ToolStripMenuItem("Preferenze di stampa"); - preferenze.Click += (s, e) => ApriPreferenze(); - - var proprieta = new ToolStripMenuItem("Proprietà stampante..."); - proprieta.Click += (s, e) => ApriProprietaStampante(); - - var spooler = new ToolStripMenuItem("Apri spooler"); - spooler.Click += (s, e) => ApriCartellaSpooler(); - - var predefinita = new ToolStripMenuItem("Imposta come predefinita"); - predefinita.Click += (s, e) => ImpostaComePredefinita(); - - contextMenu.Items.Add(preferenze); - contextMenu.Items.Add(proprieta); - contextMenu.Items.Add(spooler); - contextMenu.Items.Add(predefinita); - - // - // Form1 - // - this.Text = "Printer Manager Tool"; - this.ClientSize = new Size(400, 370); - this.Controls.Add(lstPrinters); - this.Controls.Add(btnRicarica); - this.Controls.Add(btnRiavviaSpooler); - this.Controls.Add(btnSvuotaSpooler); - this.Controls.Add(lblAdminWarning); - } - - private async void BtnLoad_Click(object sender, EventArgs e) - { - await CaricaStampanti(); - } - - private async Task CaricaStampanti() - { - lstPrinters.Items.Clear(); - lstPrinters.Items.Add("Caricamento in corso..."); - - List printers = await Task.Run(() => - { - return PrinterHelper.EnumPrintersMultiThread() - .OrderBy(p => p.PrinterName, StringComparer.CurrentCultureIgnoreCase) - .ToList(); - }); - - lstPrinters.Items.Clear(); - foreach (var p in printers) - { - string stato = p.IsOffline ? "[Offline]" : "[Pronta]"; - lstPrinters.Items.Add($"{p.PrinterName} {stato}"); - } - } - - private void BtnRiavviaSpooler_Click(object sender, EventArgs e) - { - EseguiComeAmministratore("riavviaSpooler"); - } - - private void BtnSvuotaSpooler_Click(object sender, EventArgs e) - { - EseguiComeAmministratore("svuotaSpooler"); - } - - private void EseguiComeAmministratore(string argomento) - { - try - { - Process.Start(new ProcessStartInfo - { - FileName = Application.ExecutablePath, - Arguments = argomento, - Verb = "runas", - UseShellExecute = true - }); - } - catch (Exception ex) - { - MessageBox.Show("Azione annullata o errore:\n" + ex.Message); - } - } - - private void LstPrinters_MouseDown(object sender, MouseEventArgs e) - { - if (e.Button == MouseButtons.Right) - { - int index = lstPrinters.IndexFromPoint(e.Location); - if (index != ListBox.NoMatches) - { - lstPrinters.SelectedIndex = index; - } - } - } - - private void ApriPreferenze() - { - if (lstPrinters.SelectedItem == null) return; - string printerName = lstPrinters.SelectedItem.ToString().Split('[')[0].Trim(); - Process.Start("rundll32.exe", $"printui.dll,PrintUIEntry /e /n \"{printerName}\""); - } - - private void ApriProprietaStampante() - { - if (lstPrinters.SelectedItem == null) return; - string printerName = lstPrinters.SelectedItem.ToString().Split('[')[0].Trim(); - Process.Start("rundll32.exe", $"printui.dll,PrintUIEntry /p /n \"{printerName}\""); - } - - private void ApriCartellaSpooler() - { - Process.Start("explorer.exe", "C:\\Windows\\System32\\spool\\PRINTERS"); - } - - private void ImpostaComePredefinita() - { - if (lstPrinters.SelectedItem == null) return; - string printerName = lstPrinters.SelectedItem.ToString().Split('[')[0].Trim(); - Process.Start("RUNDLL32.EXE", $"PRINTUI.DLL,PrintUIEntry /y /n \"{printerName}\""); - } - } -} diff --git a/PrinterManagerNativeTool/Form1.resx b/PrinterManagerNativeTool/Form1.resx deleted file mode 100644 index 29dcb1b..0000000 --- a/PrinterManagerNativeTool/Form1.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/PrinterManagerNativeTool/MainWindow.xaml b/PrinterManagerNativeTool/MainWindow.xaml new file mode 100644 index 0000000..3c68bb7 --- /dev/null +++ b/PrinterManagerNativeTool/MainWindow.xaml @@ -0,0 +1,302 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +