अद्यतन एक बैच फ़ाइल चल ** अभी भी एक सही जवाब ** मैं अपने विंडोज़ सेवा में निम्न कोड है की तलाश और मैं एक बैच फ़ाइल चलाना चाहते हैं। मैं कमांड प्रॉम्प्ट विंडो ऊपर चाहते हैं तो मैं प्रगतिसे सी #
यहाँमेरी कोड है, लेकिन मेरे बैच फ़ाइल कोड does not काम
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# में नौसिखिया हूं और मुझे यकीन नहीं है कि यहां से क्या करना है। धन्यवाद
किसी भी तरह से आपको लगता है कि सेवा चल रही है इस बात की पुष्टि कर सकते हैं? –
सेवा ठीक चल रही है। मैं इसे कमांड लाइन से शुरू कर सकता हूं। मैं फ़ोल्डर का नाम बदल/हटा/जोड़ सकता हूं और यह जानकारी लॉग करता है। तो मुझे 100% यकीन है कि सेवा ठीक चल रही है – Autolycus
बस एक साइड नोट के रूप में: आपको अपने 'स्ट्रीमवाइटर' का उपयोग करके 'लपेटना' चाहिए अन्यथा आप खुले फ़ाइल हैंडल को छोड़ सकते हैं। – ChrisWue