471,356 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

Windows Service & Windows Application?

Is there any way to have my C# executable file be runnable as a Windows
Application AND runnable as a Windows Service? I know I can create a
separate project for the Windows Service, but I am wondering if there is a
way to do this with a single executable?
Jun 27 '08 #1
1 1289
This is how I do it for running as plain old EXE while debugging. What you
need would be similar:

private static void Main(string[] args)
{
string opt = null;
// check for arguments
if (args.Length 0)
{
opt = args[0];
if (opt != null && opt.ToLower() == "/install")
{
TransactedInstaller ti = new TransactedInstaller();
ProjectInstaller pi = new ProjectInstaller();
ti.Installers.Add(pi);
String path = String.Format("/assemblypath={0}",
Assembly.GetExecutingAssembly().Location);
String[] cmdline = {path};
InstallContext ctx = new InstallContext("", cmdline);
ti.Context = ctx;
ti.Install(new Hashtable());
}
else if (opt != null && opt.ToLower() == "/uninstall")
{
TransactedInstaller ti = new TransactedInstaller();
ProjectInstaller mi = new ProjectInstaller();
ti.Installers.Add(mi);
String path = String.Format("/assemblypath={0}",
Assembly.GetExecutingAssembly().Location);
String[] cmdline = {path};
InstallContext ctx = new InstallContext("", cmdline);
ti.Context = ctx;
ti.Uninstall(null);

}
}
if (opt == null) // e.g. ,nothing on the command line
{
#if ( ! DEBUG )
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] {new Service1()};
ServiceBase.Run(ServicesToRun);
#else
// debug code: allows the process to run as a non-service
// will kick off the service start point, but never kill it
// shut down the debugger to exit
Service1 service = new Service1();
service.OnStart(null);
Thread.Sleep(Timeout.Infinite);
#endif

}

Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by ThunderMusic | last post: by
1 post views Thread by Artur Kowalski | last post: by
9 posts views Thread by SP | last post: by
4 posts views Thread by tshad | last post: by
3 posts views Thread by =?Utf-8?B?RGFuZGFuIFpoYW5n?= | last post: by
1 post views Thread by mdhaman | last post: by
6 posts views Thread by AMP | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.