Tôi đang cố gắng tạo một phần mềm đơn giản lưu trữ dữ liệu trong một tệp nhật ký TXT. Đây là mã của tôiFileStream.WriteLine() không ghi vào tập tin
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();
}
Điều tôi đạt được là: tệp được tạo nhưng không có gì được ghi trong đó. Tôi đã kiểm tra rất nhiều bài đăng nhưng tôi không thể tìm thấy bất kỳ trợ giúp cụ thể nào.
Trước hết, try/cuối cùng khối không cần thiết, bạn có thể sử dụng một ' sử dụng 'khối thay thế. – antonijn
bạn có thể cần phải tuôn ra StreamWriter: 'fw.Flush()' –
StreamWriter có một hàm tạo một chuỗi, đường dẫn tới tệp và một bool, được đặt thành true để nối thêm. Nó sẽ giúp bạn rơi vào hố thành công. –