C#在使用StreamWriter时获取empty.txt

C#在使用StreamWriter时获取empty.txt,c#,file,save,.net,C#,File,Save,.net,我对c#system.IO命名空间的StreamWriter方法有问题。我想把几行数据保存到一个.txt文件中,但是我得到的文件总是空的 我的方法: public class Statystyki { public void save(string path, Central cent) { StreamWriter stream = new StreamWriter(path); stream.WriteLine("Somethi

我对c#system.IO命名空间的StreamWriter方法有问题。我想把几行数据保存到一个.txt文件中,但是我得到的文件总是空的

我的方法:

public class Statystyki
{
public void save(string path, Central cent)
        {
            StreamWriter stream = new StreamWriter(path);
            stream.WriteLine("Something one: "+cent.number1);
            stream.WriteLine("Something two: "+cent.number2);
        }
}
我的主要意见是:

Central newcentral = new Central();
Statystyki stats = new Statystyki();
savepath="c:\projects\central.txt";
statsy.save(savepath, newcentral);
我尝试过许多解决方案,但没有一个奏效。有什么问题吗


非常感谢。

使用语句将其封装在
中,以便刷新流并处理对象:

using(StreamWriter stream = new StreamWriter(path))
{
    stream.WriteLine("Something one: "+cent.number1);
    stream.WriteLine("Something two: "+cent.number2);

}
正如前面指出的,您的字符串可能需要转义。您可以通过以下语句来实现这一点:

savepath = @"c:\projects\central.txt";


在主程序中。

使用
语句将其封装在
中,以便刷新流并处理对象:

using(StreamWriter stream = new StreamWriter(path))
{
    stream.WriteLine("Something one: "+cent.number1);
    stream.WriteLine("Something two: "+cent.number2);

}
正如前面指出的,您的字符串可能需要转义。您可以通过以下语句来实现这一点:

savepath = @"c:\projects\central.txt";

在主程序中。

使用以下命令:

stream.AutoFlush = true;
使用以下命令:

stream.AutoFlush = true;

使用
File.writeAllines
方法会更容易(在静态类
File
中,也在
System.IO
中):


它为您管理所有可能的错误和处理。

使用
File.writeAllines
方法会更容易(在静态类
File
中,也在
System.IO
中):


它为您管理所有可能的错误和处理。

字符串变量很可能不正确。在C#中,除非使用逐字记录,否则必须提供双斜杠。下面是我写的一个有效的例子

using (StreamWriter writer = new StreamWriter(@"C:\Users\Brandon\Desktop\text111.txt"))
        {
            writer.WriteLine("something 1");
            writer.WriteLine("something 2");
            writer.Flush();
            writer.Dispose();
        }

很可能是字符串变量不正确。在C#中,除非使用逐字记录,否则必须提供双斜杠。下面是我写的一个有效的例子

using (StreamWriter writer = new StreamWriter(@"C:\Users\Brandon\Desktop\text111.txt"))
        {
            writer.WriteLine("something 1");
            writer.WriteLine("something 2");
            writer.Flush();
            writer.Dispose();
        }

这是一个完整的程序。工作100%,但控制台版本。试试这个,并根据需要修改代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace book_StreamWriter
{
    class FileWrite
    {
        public void writeData()
        {
           using(FileStream fs =
                new FileStream(@"D:\myfile2.txt",
                                FileMode.Append, FileAccess.Write))
           {
              StreamWriter sw = new StreamWriter(fs);
              Console.WriteLine("Enter a string :");
              string str = Console.ReadLine();
              sw.Write(str);
              sw.Flush();
              sw.Close();
           }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            FileWrite fw = new FileWrite();
            fw.writeData();
            Console.ReadLine();
        }
    }
}

这是一个完整的程序。工作100%,但控制台版本。试试这个,并根据需要修改代码

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace book_StreamWriter
{
    class FileWrite
    {
        public void writeData()
        {
           using(FileStream fs =
                new FileStream(@"D:\myfile2.txt",
                                FileMode.Append, FileAccess.Write))
           {
              StreamWriter sw = new StreamWriter(fs);
              Console.WriteLine("Enter a string :");
              string str = Console.ReadLine();
              sw.Write(str);
              sw.Flush();
              sw.Close();
           }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            FileWrite fw = new FileWrite();
            fw.writeData();
            Console.ReadLine();
        }
    }
}
在main()中,您将
stats
声明为
类Statystyki
的对象。但是,您正在使用
statsy
访问您的
save()
。所以,我不确定你的代码是否编译过。 另外,使用
语句添加

在main()中,您将
stats
声明为
类Statystyki
的对象。但是,您正在使用
statsy
访问您的
save()
。所以,我不确定你的代码是否编译过。
另外,使用
语句添加

您的FileStream类在哪里?显示完整的代码。FileStream类在哪里?显示完整的代码。此处不需要使用
FileStream
,因为您使用
using
将其包装起来,
fs.Close()
是多余的
StreamWriter
实现了
IDisposable
,应该使用它自己的
语句包装在
中。这里不需要使用
FileStream
,正如您使用
using
包装它一样,
fs.Close()
是多余的
StreamWriter
实现了
IDisposable
,并且应该使用它自己的
语句包装在
中。