初始化的FileStream和StreamWriter领域中的服务类 [英] Initializing FileStream and StreamWriter fields in a service class

查看:79
本文介绍了初始化的FileStream和StreamWriter领域中的服务类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的方法以外的代码,因为我需要关闭和处置的StreamWriter和FILESTREAM在调用OnStop()方法:

I made this code outside the methods because I need to close and dispose the streamwriter and filestream in the OnStop() method:

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



不过,里面的FS新的StreamWriter提示错误字段初始不能引用非静态字段,方法或属性..。所以我做了静态的FileStream:

However, the fs inside the new StreamWriter prompts the error "A field initializer cannot reference the non-static field, method, or property..". So I made the FileStream static:

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

当我运行此程序,它不会写入文本文件,并认为这是因为静态的。我搜索一下,阅读 :一个类可以声明为static,这表明它仅包含静态成员是无法创建使用new关键字静态类的实例静态类由.NET Framework公共语言运行时(CLR)自动加载时。含有类节目或命名空间被载入

When I ran the program, it doesn't write to the textfile and concluded that it's because of the static. I searched about it and read this: "A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded."

这是我的示例代码:

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();
    }
}



你觉得我应该怎么办?

What do you think should I do?

推荐答案

您已经做了我应该做工精细,但它不这样做的好方法,所以它不是值得探讨,为什么它不工作。在一类具有静态变量不会使类的静态

What you have done should work fine, but it's not a good way to do it, so it's not worth investigating why it's not working. Having static variables in a class doesn't make the class static.

只是声明变量没有initialisers,然后设置变量的时候要打开文件:

Just declare the variables without initialisers, then set the variables when you want to open the file:

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();
    }
}

这篇关于初始化的FileStream和StreamWriter领域中的服务类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆