运行在C#中的批处理文件批处理文件

2023-09-03 00:54:55 作者:夜声带走最后一个我@

更新 **还在寻找一个正确的答案** 我有以下的code。在我的Windows服务,我想运行一个批处理文件。我想在命令提示符窗口,所以我能看到进步

UPDATE ** STILL LOOKING FOR A CORRECT ANSWER ** I have the following code in my windows service and I want to run a batch file. I want the command prompt window up so I can see progress

这是我的code,但我的批处理文件,code不工作

here is my code but my batch file code doesnt work

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;

    namespace Watcher
    {
        public partial class Watcher : ServiceBase
        {
            public Watcher()
            {
                InitializeComponent();
            FolderWatcher.Created += FolderWatcher_Created;
            FolderWatcher.Deleted += FolderWatcher_Deleted;
            FolderWatcher.Renamed += FolderWatcher_Renamed;
            }

            protected override void OnStart(string[] args)
            {

                          // Start the child process.
            Process p = new Process();
            // Redirect the output stream of the child process.
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.FileName = "C:\\myFile.bat";
            p.Start();
            // Do not wait for the child process to exit before
            // reading to the end of its redirected stream.
            // p.WaitForExit();
            // Read the output stream first and then wait.
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();


            }

            protected override void OnStop()
            {
            }

            private void FolderWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
            {
                TextWriter writer = new StreamWriter("C:\\folder\\FolderLog.txt", true);
                writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been created. ");
                writer.Close();
            }

            private void FolderWatcher_Deleted(object sender, System.IO.FileSystemEventArgs e)
            {
                TextWriter writer = new StreamWriter("C:\\folder\\FolderLog.txt", true);
                writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been deleted. ");
                writer.Close();
            }

            private void FolderWatcher_Renamed(object sender, System.IO.RenamedEventArgs e)
            {
                TextWriter writer = new StreamWriter("C:\\folder\\log.txt", true);
                writer.WriteLine(DateTime.Now + " A new folder/file with name " + e.Name + " has been renamed. ");
                writer.Close();
            }


        }
    }

它不执行批处理文件。我在.NET和C#的新手,我不知道该怎么在这里做。 谢谢

It does not execute the batch file. I am a newbie in .net and C# and I am not sure what to do from here. thanks

推荐答案

How从Windows服务运行控制台程序?

您想要的设置p.StartInfo与文件名=cmd.exe的和参数=C:\\ thebatfile.bat我相信

You will want to the set the p.StartInfo with FileName="cmd.exe" and Arguments="c:\\thebatfile.bat" i believe