如何在 .net 中调用我的函数
我正在学习 .net 并按照下面的教程编写服务 http:// /www.beansoftware.com/NET-Tutorials/Create-Windows-Services.aspx
Windows 服务已成功安装并启动。但是,当我更改文件名或删除文件或添加文件时,它不会向我的日志文件中添加任何信息 C:\Folder\FolderLog.txt
我看到了 FolderWatcher_** 的 3 个方法,但我没有看到代码中对这些方法的调用。我对 .net 非常陌生,所以我不确定它们是如何被调用的,
这里是我的完整代码,
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();
}
protected override void OnStart(string[] args)
{
}
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();
}
}
}
我遵循了该教程中的每一个步骤。我还没有看到任何描述如何调用这些方法的内容。任何帮助都会很棒
I am learning .net and writing a service by following the tutorial below http://www.beansoftware.com/NET-Tutorials/Create-Windows-Services.aspx
The windows service gets installed successfully and it starts too. But when I change the file name or delete a file or add a file, it doesnt add any information to my log file which is in
C:\Folder\FolderLog.txt
I see 3 methods for FolderWatcher_** but I dont see a call to those in the code. I am very new to .net so I am not sure how they are being called
here is my complete code
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();
}
protected override void OnStart(string[] args)
{
}
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();
}
}
}
I have followed every single step in that tutorial. I have not seen anything that descibes how these methods are called. Any help would be great
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
本教程可能要求您在服务的设计模式界面中添加一个
FileSystemWatcher
。在设计模式下,选择该对象并在其事件处理程序中选择类的适当方法。The tutorial probably has you adding a
FileSystemWatcher
in the design mode surface for your service. In design mode, select that object and in its event handlers, select the appropriate methods of your class.确保将观察程序实例“
FolderWatcher
”的事件注册到代码中的事件处理程序,您可以在服务设计器或服务的构造函数中执行此操作,例如:Make sure you register the events of the watcher instance "
FolderWatcher
" to the events handlers in your code, you can do that at service designer or in the constructor of the service, like: