2

我在方法之外编写了这段代码,因为我需要在 OnStop() 方法中关闭并处理流写入器和文件流:

private Timer timer = new Timer();
private FileStream fs = new FileStream(@"D:\Hello.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
private StreamWriter sw = new StreamWriter(fs);
//The fs is in red line, having error

但是新的StreamWriter里面的fs提示错误“A field initializer cannot reference the non-static field, method, or property..”。所以我将 FileStream 设为静态:

private static FileStream fs = new FileStream(@"D:\Hello.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);

当我运行程序时,它没有写入文本文件并得出结论,这是因为静态。搜索并阅读:“一个类可以声明为静态,表明它只包含静态成员。使用 new 关键字无法创建静态类的实例。静态类由 .NET Framework 自动加载加载包含该类的程序或命名空间时的公共语言运行时 (CLR)。”

这是我的示例代码:

public partial class Service1 : ServiceBase
{
    private Timer timer = new Timer();
    private FileStream fs = new FileStream(@"D:\Hello.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
    private StreamWriter sw = new StreamWriter(fs);
//The fs is in red line, having error. Look in my description for error's details
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        timer.Elapsed += new ElapsedEventHandler(WriteText);
        timer.Interval = 5000; //5 seconds
        timer.Start();
    }

    public void WriteText(object source, ElapsedEventArgs e)
    {
        sw.WriteLine(DateTime.Now + " Windows Service");
    }
    protected override void OnStop()
    {
        sw.Close();
        sw.Dispose();
        fs.Close();
        fs.Dispose();
        timer.Stop();
    }
}

你觉得我应该怎么做?

4

3 回答 3

4

您所做的应该可以正常工作,但这不是一个好方法,因此不值得调查为什么它不起作用。在类中拥有静态变量不会使类成为静态的。

只需声明不带初始化程序的变量,然后在要打开文件时设置变量:

public partial class Service1 : ServiceBase
{
    private Timer timer;
    private FileStream fs;
    private StreamWriter sw

    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        fs = new FileStream(@"D:\Hello.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
        sw = new StreamWriter(fs);
        timer = new Timer();
        timer.Elapsed += new ElapsedEventHandler(WriteText);
        timer.Interval = 5000; //5 seconds
        timer.Start();
    }

    public void WriteText(object source, ElapsedEventArgs e)
    {
        sw.WriteLine(DateTime.Now + " Windows Service");
    }
    protected override void OnStop()
    {
        timer.Stop(); // Stop the timer before closing the file
        sw.Close();
        sw.Dispose();
        fs.Close();
        fs.Dispose();
    }
}
于 2012-08-24T08:21:27.630 回答
3

不要让它们成为静态的。只需将初始化代码从字段初始化程序移动到构造函数即可。并且不要忘记添加一个Dispose处理它们的方法。

于 2012-08-24T08:18:00.560 回答
2

您不需要静态字段,只需将初始化移动到您的构造函数:

private FileStream fs;
private StreamWriter sw;

public MyClass()
{
    fs = new FileStream(@"D:\Hello.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite); 
    sw = new StreamWriter(fs);
}

为了确保您的OnStop()方法真正被调用并且流被处理,IDisposable在您的类中实现并OnStop()在您的方法结束时调用Dispose()

于 2012-08-24T08:19:10.250 回答