CẬP NHẬT ** vẫn đang tìm kiếm một câu trả lời ĐÚNG ** Tôi có đoạn code sau trong dịch vụ cửa sổ của tôi và tôi muốn chạy một tập tin thực thi. Tôi muốn các cửa sổ command prompt lên vì vậy tôi có thể nhìn thấy sự tiến bộchạy một tập tin thực thi từ C#
đây là mã của tôi nhưng batch file đang doesnt làm việc của tôi
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();
}
}
}
Nó không chạy file batch. Tôi là một newbie trong .net và C# và tôi không chắc chắn phải làm gì từ đây. nhờ
Bạn có thể xác nhận rằng dịch vụ đang chạy không? Dịch vụ –
đang chạy tốt. Tôi có thể bắt đầu nó từ dòng lệnh. Tôi có thể đổi tên/xóa/thêm thư mục và ghi nhật ký thông tin. Vì vậy, tôi chắc chắn 100% rằng các dịch vụ đang chạy tốt – Autolycus
Cũng giống như một lưu ý phụ: Bạn nên quấn 'StreamWriter' của bạn trong 'bằng cách sử dụng' nếu không bạn có thể để mở xử lý tập tin xung quanh. – ChrisWue