如何在 .net 中调用我的函数

发布于 2024-11-30 15:33:55 字数 2133 浏览 1 评论 0原文

我正在学习 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽月 2024-12-07 15:33:55

本教程可能要求您在服务的设计模式界面中添加一个 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.

溇涏 2024-12-07 15:33:55

确保将观察程序实例“FolderWatcher”的事件注册到代码中的事件处理程序,您可以在服务设计器或服务的构造函数中执行此操作,例如:

public Watcher()
{
    InitializeComponent();

    FolderWatcher.Created += FolderWatcher_Created;//when created FolderWatcher_Created will be called
    FolderWatcher.Deleted += FolderWatcher_Deleted;//when deleted call FolderWatcher_Deleted will be called
    FolderWatcher.Renamed += FolderWatcher_Renamed;//when renamed FolderWatcher_Renamed will be called
}

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:

public Watcher()
{
    InitializeComponent();

    FolderWatcher.Created += FolderWatcher_Created;//when created FolderWatcher_Created will be called
    FolderWatcher.Deleted += FolderWatcher_Deleted;//when deleted call FolderWatcher_Deleted will be called
    FolderWatcher.Renamed += FolderWatcher_Renamed;//when renamed FolderWatcher_Renamed will be called
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文