简体   繁体   English

如何通过C#跟踪和停止cmd中正在运行的应用程序(Windows Services)

[英]How to keep track and stop a running application in cmd from C# (Windows Services)

I'm developing a windows service using Topshelf library... I want to start cmd and map a folder from my network and run an console application from it. 我正在使用Topshelf库开发Windows服务...我想启动cmd并从我的网络映射一个文件夹,然后从中运行控制台应用程序。

This is my whole class: 这是我的全班:

class ClientService
{
    private Scheduler ServiceScheduler { get; set; }
    private Process MyApp{ get; set; }
    private Config ConfigFile { get; set; }
    public void Start()
    {
        ServiceScheduler = new Scheduler(6000) { Enabled = true };
        ServiceScheduler.Elapsed += Scheduler_Elapsed;
        ConfigFile =Config.get();
        ConfigMyApp();
    }
    private void ConfigMyApp() => ConfigMyApp= new Process
    {
        StartInfo =
        {
            CreateNoWindow = false,
            ErrorDialog = false,
            UseShellExecute = false,
            RedirectStandardInput = true,
            FileName = "CMD.exe",
        }
    };
    private void Scheduler_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        if (Network.CurrentHourMin == ConfigFile.StartTime)
        {
            MyApp.Start();
            using (var sw = MyApp.StandardInput)
            {
                if (sw.BaseStream.CanWrite)
                {
                    //Map My Folder Network
                    sw.WriteLine("pushd \\\\MyNetwork\\General\\");
                    //Run MyApp with Args
                    sw.WriteLine(MyApp -run -args -others);
                }
            }
        }
        else if (Network.CurrentHourMin == ConfigFile.EndTime)
        {
         //////////
         How To stop running MyApp.exe
         //////////
        }
    }
    public void Stop()
    {

    }
}

The problem is I can't close MyApp.exe that previously ran from cmd.exe and doing MyApp.Close won't stop running the application and doesn't any affect at all. 问题是我无法关闭以前从cmd.exe运行的MyApp.exe并执行MyApp.Close不会停止运行该应用程序,也没有任何影响。

Even Calling Stop method from Topshelf service (or stopping the service) doesn't stop running MyApp What is the best practice for Exit or kill an Console application in this scenario? 即使从Topshelf服务调用Stop方法(或停止服务)也不会停止运行MyApp在这种情况下退出或终止控制台应用程序的最佳实践是什么?

Don't create processes unless you have to. 除非必须,否则不要创建流程。 Here's some code to map a network drive: 这是一些映射网络驱动器的代码:

public void mapDrive(string driveLetter, string networkPath, bool isPersistent)
{

    private WshNetwork _networkShell = new WshNetwork();
    object persistent = isPersistent;
    _networkShell.MapNetworkDrive(driveLetter, networkPath, ref persistent) ; 

}

You'll need a reference to Windows Script Host Object Model. 您需要对Windows脚本宿主对象模型的引用。

You'll have better control over the exception handling if you keep it in process. 如果您继续处理异常,则可以更好地控制异常处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM