Initializing FileStream and StreamWriter fields in

2019-04-12 20:58发布

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

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

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."

This is my sample code:

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?

标签: c# static
3条回答
放荡不羁爱自由
2楼-- · 2019-04-12 21:31

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.

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();
    }
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-04-12 21:43

You don't need a static field, just move the initialization to your constructor:

private FileStream fs;
private StreamWriter sw;

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

To make sure, your OnStop() method is really called and the stream is disposed, implement IDisposable in your class and call OnStop() at the end of your Dispose() method.

查看更多
孤傲高冷的网名
4楼-- · 2019-04-12 21:44

Don't make them static. Just move the initialization code from field-initializers to the constructor. And don't forget to add a Dispose method that disposes them.

查看更多
登录 后发表回答