C# 为什么';t FileStream作为Streamwriter写入文本文件的参数?

C# 为什么';t FileStream作为Streamwriter写入文本文件的参数?,c#,filestream,streamwriter,C#,Filestream,Streamwriter,在下面包含的代码中,当使用以下语句时,我能够将字符串“fullname”的内容写入指定目录中的文本文件: System.IO.File.writealText(路径,全名) 但是,如果我将字符串路径写入FileStream对象(指定了参数),然后将该FileStream对象作为参数传递给StreamWriter对象,则会创建该文件,但不会写入任何内容 第一次尝试:注释掉System.IO.File.WriteAllText(路径,全名)并使用上面的三行。这将创建文件,但不会将任何内容写入文件 第

在下面包含的代码中,当使用以下语句时,我能够将字符串“fullname”的内容写入指定目录中的文本文件:
System.IO.File.writealText(路径,全名)
但是,如果我将字符串路径写入FileStream对象(指定了参数),然后将该FileStream对象作为参数传递给StreamWriter对象,则会创建该文件,但不会写入任何内容

第一次尝试:注释掉
System.IO.File.WriteAllText(路径,全名)并使用上面的三行。这将创建文件,但不会将任何内容写入文件

第二次尝试:取消注释
System.IO.File.WriteAllText(路径,全名)语句并注释上面的三行。这将根据需要执行

以下是完整的代码块:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileInputOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the Split() method of the String Class
            string fullname = " Robert Gordon Orr ";
            fullname = fullname.Trim();
            string[] splitNameArray = fullname.Split(' ');
            Console.WriteLine("First Name is: {0}", splitNameArray[0]);
            Console.WriteLine("Middle Name is: {0}", splitNameArray[1]);
            Console.WriteLine("Last Name is: {0}", splitNameArray[2]);
            Console.WriteLine("Full name is: {0}", fullname);
            string path = @"C:\Programming\C#\C# Practice Folder\Console Applications\FileInputOutput\textfile.txt";

            FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite);
            StreamWriter toFile = new StreamWriter(fs);
            toFile.Write(fullname);

            //System.IO.File.WriteAllText(path, fullname);`enter code here`

            Console.ReadLine();

        }
    }
}

我认为您只是忘记刷新文件流:

fs.Flush();
这是必需的,因为根据msdn,这是使FileStream实际将缓冲区写入文件的原因

刷新:清除此流的缓冲区,并将所有缓冲数据写入文件。(覆盖Stream.Flush()

问候。

您必须调用Close以确保所有数据都正确地写入底层流


所以这里的问题主要是,因为您实际上没有关闭StreamWriter,所以数据会得到备份,但不会推送到文件,即使FileStream立即在其构造函数中创建了文件。永远不要忘记关闭流,否则可能会导致后续的重大问题。

正如其他人所说:流必须在.NET中刷新才能写入磁盘。这可以手动完成,但我只需将代码更改为在流上使用using语句:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace FileInputOutput
{
    class Program
    {
        static void Main(string[] args)
        {
            // Use the Split() method of the String Class
            string fullname = " Robert Gordon Orr ";
            fullname = fullname.Trim();
            string[] splitNameArray = fullname.Split(' ');
            Console.WriteLine("First Name is: {0}", splitNameArray[0]);
            Console.WriteLine("Middle Name is: {0}", splitNameArray[1]);
            Console.WriteLine("Last Name is: {0}", splitNameArray[2]);
            Console.WriteLine("Full name is: {0}", fullname);
            string path = @"C:\textfile.txt";

            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
            {
                using (StreamWriter toFile = new StreamWriter(fs))
                {
                    toFile.Write(fullname);
                }
            }
            //System.IO.File.WriteAllText(path, fullname);`enter code here`

            Console.ReadLine();

        }
    }
}

对流调用Dispose()(与使用隐式操作一样),会导致在using块结束时刷新并关闭流。

您可以改用
File.writealText
。这是因为您在错误的时间查看文件。文件流尚未刷新其缓冲区。它没有任何理由这样做,你没有关闭文件。在这里使用Using语句不是可选的。欢迎!当你不在问题的开头陈述你的经验或研究时,问题通常看起来更好。最好从你的实际问题开始(所有问题都在感叹号之后),然后在结尾包括你的研究中的相关引用和你的来源的链接。谢谢大家。通过在ADO数据库调用中应用“using”语句,我对它有些熟悉。使用上面的“using”代码块后,我的文件被成功写入。我将对此进行进一步调查,以更好地了解执行情况。再次感谢大家。