簡體   English   中英

從C#運行批處理文件

[英]running a batch file from C#

更新 **仍然正在尋找一個正確的答案**我的Windows服務中有以下代碼,我想運行一個批處理文件。 我想要命令提示窗口,以便我可以看到進度

這是我的代碼,但我的批處理文件代碼不起作用

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#的新手,我不知道該怎么做。 謝謝

如何從Windows服務運行控制台應用程序?

您將要設置p.StartInfo與FileName =“cmd.exe”和Arguments =“c:\\\\ thebatfile.bat”我相信

問題是您將UseShellExecute為false,但您沒有傳遞可執行文件的名稱。

當使用ShellExecute ,它類似於雙擊資源管理器中的文件 - 它知道需要使用Word打開.doc文件,並且需要使用cmd.exe打開.bat文件。 如果您已禁用此功能但它不知道這些內容,您需要傳遞可執行文件才能成功運行任何內容。

當您將RedirectStandardOutput設置為true時,您需要通過將FileName設置為cmd.exe並將參數設置為/C "c:\\myFile.bat"來通過cmd.exe運行批處理文件:

p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C \"c:\\myFile.bat\"";

Windows服務在無桌面用戶帳戶下運行。 要查看cmd窗口,您必須模擬當前登錄的用戶並在此用戶的桌面上啟動cmd窗口。 看到這個:

來自C#的Windows模擬

看起來它在首次運行服務時運行批處理腳本然后退出( p.WaitForExit(); ),然后其他函數才能被調用。 這是預期的行為嗎? 這可以解釋為什么你可以看到它執行文件夾操作而不是看到正在運行的腳本。

嘗試使用此代碼調出控制台窗口。 它應該讓您了解批處理腳本何時運行。

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;

        /*
        This is commented out so we can see what the script is doing
        inside the cmd console.
        */
        //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.

        /*
        Since we aren't redirecting the output, we have to comment out
        this line or we get an error
        */
        //string output = p.StandardOutput.ReadToEnd();

        p.WaitForExit();
}

我懷疑你的服務或蝙蝠文件。 修改源代碼打開記事本! 檢查記事本是否顯示!! 如果是,那么我們可以進一步調查!

你的批處理文件在做什么? 假設您已確認此IS正常運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM