C# FileStream.WriteLine()未写入文件
我正在尝试制作一个简单的软件,将数据存储在TXT日志文件中。 这是我的密码C# FileStream.WriteLine()未写入文件,c#,file-io,stream,text-files,C#,File Io,Stream,Text Files,我正在尝试制作一个简单的软件,将数据存储在TXT日志文件中。 这是我的密码 FileStream fs = null; StreamWriter fw = null; try { fs= new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite); fw = new St
FileStream fs = null;
StreamWriter fw = null;
try
{
fs= new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
fw = new StreamWriter(fs);
fw.Write("sadadasdsadsadsadas");
for (int i = 0; i < AnimalShelter.AnimalList.Count; i++)
{
fw.WriteLine("<chipNr>" + AnimalShelter.AnimalList[i].ChipRegistrationNumber + "<chipNr>");
Console.WriteLine("<chipNr>" + AnimalShelter.AnimalList[i].ChipRegistrationNumber + "<chipNr>");
}
}
catch(IOException)
{
MessageBox.Show("ERROR THROWN");
}
finally
{
if (fs!= null) fs.Close();
// if (fw != null) fw.Close();
}
FileStream fs=null;
StreamWriter fw=null;
尝试
{
fs=新文件流(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+“/textme.txt”、FileMode.OpenOrCreate、FileAccess.ReadWrite);
fw=新的StreamWriter(fs);
fw.写作(“萨达斯”);
for(int i=0;i
我实现的是:创建了文件,但没有写入任何内容。
我查看了很多帖子,但找不到任何特别的帮助 添加对
Flush
流的调用。这是因为您正在包装文件流
StreamWriter
将写入FileStream
,但您需要指示何时将流
发送到实际文件。此外,您还可以使用将您的try finally
与交换:
try
{
using (var fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+"/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (var fw = new StreamWriter(fs))
{
fw.Write("sadadasdsadsadsadas");
for (int i = 0; i < AnimalShelter.AnimalList.Count; i++)
{
fw.WriteLine("<chipNr>" + AnimalShelter.AnimalList[i].ChipRegistrationNumber + "<chipNr>");
Console.WriteLine("<chipNr>" + AnimalShelter.AnimalList[i].ChipRegistrationNumber + "<chipNr>");
}
fw.Flush(); // Added
}
}
}
catch(IOException)
{
MessageBox.Show("ERROR THROWN");
}
试试看
{
使用(var fs=new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+“/textme.txt”、FileMode.OpenOrCreate、FileAccess.ReadWrite))
{
使用(var fw=新StreamWriter(fs))
{
fw.写作(“萨达斯”);
for(int i=0;i
将您的StreamWriter
放在一个using块中,以确保在文件使用结束时所有内容都正确关闭,而且我认为您不需要创建文件流
,这样才能工作
try
{
string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "textme.txt")
using(fw = new StreamWriter(fileName, true))
{
......
}
}
catch(IOException)
{
MessageBox.Show("ERROR THROWN");
}
请注意,StreamWriter
有一个构造函数,它接受两个参数,即要创建/打开的文件的名称和一个标志,指示该文件应在附加模式下打开或覆盖
事实上,Flush()就是答案;但是,我会使用
试试看
{
var fileName=Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+“/textme.txt”;
var lines=AnimalShelter.AnimalList.Select(o=>“”+o.ChipRegistrationNumber+);
File.writeAllines(文件名,行);
foreach(行中的var行)
控制台写入线(行);
}
捕获(IOException)
{
Show(“抛出错误”);
}
尝试使用此方法-只需更换阵列即可:
try
{
using (Stream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs))
{
int[] test = new int[] { 0, 12, 23, 46 };
sw.Write("sadadasdsadsadsadas");
for (int i = 0; i < test.Length; i++)
{
sw.WriteLine("<chipNr>" + test[i] + "<chipNr>");
Console.WriteLine("<chipNr>" + test[i] + "<chipNr>");
}
sw.Close();
}
fs.Close();
}
}
catch (IOException)
{
MessageBox.Show("ERROR THROWN");
}
试试看
{
使用(Stream fs=new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+“/textme.txt”、FileMode.OpenOrCreate、FileAccess.ReadWrite))
{
使用(StreamWriter sw=新StreamWriter(fs))
{
int[]test=新的int[]{0,12,23,46};
书写(“萨达斯”);
for(int i=0;i
始终使用使用
(如前所述),您将不会遇到问题(或不得不考虑问题)
(此外,您还可以关闭编写器,而不是应该可以工作的filestream)
问题就我所知
FileStream.Close
实际上是Stream.Close
——这会调用Dispose
,但它不是虚拟的,所以会进行一些常规清理
FileStream.Dispose
当您使用using
时会隐式调用它-执行特定的刷新
,然后关闭
/Dispose-正确的特定清理也会这样做
您可以通过使用
来避免任何这种情况,因为这是通常推荐的模式(坦率地说,从来没有让我接触过这些模式)首先,try/finally块是不需要的,您可以使用使用
块来代替。您可能需要刷新StreamWriter:fw.flush()
StreamWriter有一个构造函数,它接受字符串、文件路径和bool,并将其设置为true以追加。它会帮助您落入成功的陷阱。您需要刷新并关闭流,以确保所有数据都已写入。看这里:
try
{
using (Stream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs))
{
int[] test = new int[] { 0, 12, 23, 46 };
sw.Write("sadadasdsadsadsadas");
for (int i = 0; i < test.Length; i++)
{
sw.WriteLine("<chipNr>" + test[i] + "<chipNr>");
Console.WriteLine("<chipNr>" + test[i] + "<chipNr>");
}
sw.Close();
}
fs.Close();
}
}
catch (IOException)
{
MessageBox.Show("ERROR THROWN");
}
using (FileStream fs = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/textme.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite))
using (StreamWriter fw = new StreamWriter(fs))
{
fw2.Write("sadadasdsadsadsadas");
}