You must Sign In to post a response.
  • Category: ASP.NET

    How to open cmd from windows service?

    I am creating windowsservice and using C#.I want open cmd/.exe file from windows service.My operating system is windows8.Does it possible from windows service?or any alternative for that.I want want open cmd/exe after some interval that's why I choosed windows service.
  • #763403
    You can use Process to run the DOS Command

    System.Diagnostics.Process.Start("CMD.exe","CD <FolderName>");

    By Nathan
    Direction is important than speed

  • #763404
    Hi Nathan,thanks for ur help.I used process.start() that open cmd/exe/file but when I creating schedular and running that schedular unable to open that cmd/exe.How to handle that?

  • #763407
    Hello Pinky,

    Try this code :

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Configuration;
    using System.Timers;
    using System.Runtime.InteropServices;
    using System.Security;
    using System.ServiceProcess;

    namespace YourServiceName
    {
    class Program : ServiceBase
    {
    private static System.Timers.Timer mytimer;

    public void OnTimedEvent(object source, ElapsedEventArgs e)
    {
    Process.Start("cmd","/c cls"); // cls is the command to clear the screen. You can use another commands.
    }

    protected override void OnStart(string[] args)
    {
    base.OnStart(args);
    mytimer = new System.Timers.Timer();
    mytimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    mytimer.Interval = (1 * 60 * 1000); // 1 minute.. Interval can be define in seconds only. But here you can change the minutes instead of 1 as per your requirement.
    mytimer.Enabled = true;
    }

    protected override void OnStop()
    {
    base.OnStop();
    }

    static void Main(string[] args)
    {
    ServiceBase.Run(new Program());
    }

    public Program()
    {
    this.ServiceName = "YourServiceName";
    }
    }
    }

    Create one installer.cs and write the below code :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.ServiceProcess;

    namespace YourServiceName
    {
    [RunInstaller(true)]
    public class YourServiceNameInstaller : Installer
    {
    string strServiceName = "YourServiceName";
    public YourServiceNameInstaller()
    {
    var processInstaller = new ServiceProcessInstaller();
    var serviceInstaller = new ServiceInstaller();

    //set the privileges
    processInstaller.Account = ServiceAccount.LocalSystem;

    serviceInstaller.DisplayName = strServiceName;
    serviceInstaller.StartType = ServiceStartMode.Automatic;

    //must be the same as what was set in Program's constructor
    serviceInstaller.ServiceName = strServiceName;

    this.Installers.Add(processInstaller);
    this.Installers.Add(serviceInstaller);

    this.AfterInstall += new InstallEventHandler(YourServiceNameInstaller_AfterInstall);
    }

    public void YourServiceNameInstaller_AfterInstall(object sender, InstallEventArgs e)
    {
    var controller = new ServiceController(strServiceName);
    controller.Start();
    }
    }
    }

    Refer my another article for windows service :

    http://www.dotnetspider.com/resources/46148-Windows-service-to-take-backup-of-MySQL-database-at-every-interval.aspx


    Hope this will be helpful for you.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #764747
    Hi, sorry to say that I have no idea about this. I use windows hosting service from Mywindowshosting.com, which is a good and trustworthy online hosting site. Thanks for the discussion. I must try these.


Sign In to post your comments