注意下面这段代码的错误
 
static void Main(string[] args)
                {
                        String txtStr = "test.txt";

                        if (!File.Exists(txtStr))
                        {
                                //创建文件之后没有关闭文件流
                                File.Create(txtStr);
                        }

                        using (StreamWriter writer=new StreamWriter(txtStr))
                        {
                                writer.WriteLine("hello test!");
                        }
                }
 
 
File.Create()方法返回的是当前的FileStream,想下面这样修改就没问题
 
static void Main(string[] args)
                {
                        String txtStr = "test.txt";
                        FileStream fileStream = null;

                        if (!File.Exists(txtStr))
                        {
                                //创建文件之后没有关闭文件流
                                fileStream = File.Create(txtStr);
                        }

                        using (StreamWriter writer = new StreamWriter(fileStream))
                        {
                                writer.WriteLine("hello test!");
                        }
                }
 
或者直接创建完成后进行关闭。
 
File.Create(txtStr).Close()