Generate Barcode from Service - C#
BarCode SDK sample in C# demonstrating ‘Generate Barcode from Service’
readme.txt
This is simple .NET service created by Microsoft guidelines ( http://msdn.microsoft.com/en-us/library/d56de412(v=vs.80).aspx )
"ServiceController" - simple GUI application that starts and stops the service;
"WindowsService1" - service itself.
This sample service generates PDF files with random GS1-128 barcode every five seconds
and puts them to "c:\barcodes" folder.
Check the Service1.vb file.
There is barcode generation and email sending example code.
ServiceController requries Administrator rights to run, so you should run the Visual Studio as Administrator to be able to run the ServiceController from Debug.
Or run the compiled executable directly. It will ask for Administrative rights on start.
Form1.cs
using System;
using System.IO;
using System.Management;
using System.Windows.Forms;
using System.ServiceProcess;
namespace ServiceController
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnInstallService_Click(object sender, EventArgs e)
{
try
{
// Install the service
ServiceInstaller.Install("Service1", "Service1", Path.GetFullPath("WindowsService1.exe"));
// Set the service option "Allow desktop interaction".
ConnectionOptions co = new ConnectionOptions();
co.Impersonation = ImpersonationLevel.Impersonate;
ManagementScope mgmtScope = new ManagementScope(@"root\CIMV2", co);
mgmtScope.Connect();
ManagementObject wmiService = new ManagementObject("Win32_Service.Name='Service1'");
ManagementBaseObject InParam = wmiService.GetMethodParameters("Change");
InParam["DesktopInteract"] = true;
ManagementBaseObject OutParam = wmiService.InvokeMethod("Change", InParam, null);
// Start the service
ServiceInstaller.StartService("Service1");
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
private void btnUninstallService_Click(object sender, EventArgs e)
{
try
{
ServiceInstaller.Uninstall("Service1");
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Form1.Designer.cs
��n a m e s p a c e S e r v i c e C o n t r o l l e r
{
p a r t i a l c l a s s F o r m 1
{
/ / / <