C# 如何正确使用 StreamWriter 类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11467240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:57:32  来源:igfitidea点击:

how to use StreamWriter class properly?

c#streamwriter

提问by vettori

I am using StreamWriterclass for file operations, are there any problems in this code that I am not seeing?

我正在使用StreamWriter类进行文件操作,这段代码中是否有我没有看到的问题?

Such as do I need to put it into a try catch finallyblock?

比如我需要把它放到一个try catch finally块中吗?

StreamWriter sr = new StreamWriter(streamFolder);
sr.Write(details);

File.SetAttributes(streamFolder, FileAttributes.Hidden);
sr.Close();

采纳答案by Sergey Berezovskiy

Whats wrong with your code? If some exception will occur before you close stream, then stream will stay open, and system resources will not be released:

你的代码有什么问题?如果在关闭流之前发生异常,则流将保持打开状态,并且不会释放系统资源:

StreamWriter sr = new StreamWriter(streamFolder);
sr.Write(details);
// some exception occurs here 
File.SetAttributes(streamFolder, FileAttributes.Hidden);
sr.Close();

So, you need to be sure, that stream will be closed. This could be achieved by try...finallyblock:

因此,您需要确保该流将被关闭。这可以通过try...finally块来实现:

StreamWriter sr = new StreamWriter(streamFolder);

try 
{
   sr.Write(details);
   // some exception occurs here 
   File.SetAttributes(streamFolder, FileAttributes.Hidden);
}
finally
{
   sr.Close();
}

But StreamWriter implements IDisposableinterface, so you can let C# compiler do it automatically for you by wrapping writer usage into usingblock:

但是 StreamWriter 实现了IDisposable接口,因此您可以让 C# 编译器通过将编写器用法包装到using块中来自动为您执行此操作:

using(StreamWriter sr = new StreamWriter(streamFolder)) 
{
   sr.Write(details);
   // some exception occurs here 
   File.SetAttributes(streamFolder, FileAttributes.Hidden);
}

This code will be compiled as:

这段代码将被编译为:

StreamWriter sr = new StreamWriter(streamFolder);

try 
{
   sr.Write(details);
   // some exception occurs here 
   File.SetAttributes(streamFolder, FileAttributes.Hidden);
}
finally
{
   if (sr != null)
      sr.Dispose();
}

The only difference between manual implementation is null-check and method Disposeis called instead of Close. But actually when you call Close()or Dispose()same code will be executed:

手动实现之间的唯一区别是空检查和方法Dispose被调用而不是Close. 但实际上当你调用Close()Dispose()相同的代码将被执行:

this.Dispose(true);
GC.SuppressFinalize(this);

You can read more on Dispose method implementation.

您可以阅读有关Dispose 方法实现的更多信息。

回答by John Saunders

You want to use:

你想使用:

  using (StreamWriter sr = new StreamWriter(streamFolder))
  {
      sr.Write(details); 

      File.SetAttributes(streamFolder, FileAttributes.Hidden); 
  }

You don't need the Close.

你不需要Close.

回答by Daniel Sklenitzka

You should probably use a using statement:

您可能应该使用 using 语句:

using (StreamWriter sr = new StreamWriter(streamFolder))
{
    sr.Write(details);
    File.SetAttributes(streamFolder, FileAttributes.Hidden);
}

At the end of the using block, the StreamWriter.Dispose will be called, whether there was an exception or the code ran successfully.

在 using 块结束时, StreamWriter.Dispose 将被调用,无论是否有异常或代码运行成功。

回答by user1485585

System.IO.StreamWriter is a System.IDisposable

System.IO.StreamWriter 是一个 System.IDisposable

You have to dispose it. And why do you close something you don't open ?

你必须处理它。为什么你关闭一些你没有打开的东西?

回答by oleksii

Wrap it in a usingblock

将其包裹在一个using块中

using(StreamWriter sr = new StreamWriter(streamFolder))
{
      sr.Write(details);
      File.SetAttributes(streamFolder, FileAttributes.Hidden);
}

Make sure your naming is good, so streamFoldershould probably be an fName. You can also put this code into try-catch-finally, if you feel you can handle some IO (or other) exceptions:

确保你的命名是好的,所以streamFolder应该是一个fName. 如果您觉得可以处理一些 IO(或其他)异常,您也可以将此代码放入 try-catch-finally 中:

StreamWriter sr;
try
{
    sr = new StreamWriter(streamFolder);
    sr.Write(details);
    File.SetAttributes(streamFolder, FileAttributes.Hidden);
}
catch(IOException ex)
{
    //handling IO
}
finally
{
    if (sr != null)
        sr.Dispose();
}