Hello everyone,
My all codes are below. This codes are creating "mac.txt" file but I cant write any data in this file. I dont know what is the problem?
using System.Text;
using System.Windows.Forms;
using System.IO;
private void button1_Click(object sender, EventArgs e)
{
StreamWriter dosya = new StreamWriter("mac.txt");
dosya.WriteLine("1231231231");
dosya.Write("123123123");
}
Loading
VulpesPosted May 10, 2011, 5:08 AM
StreamWriter dosya = new StreamWriter("mac.txt");
dosya.WriteLine("1231231231");
dosya.Write("123123123");
dosya.Close();
Another way to automatically close the stream is to use the 'using' statement:
using (StreamWriter dosya = new StreamWriter("mac.txt"))
{
dosya.WriteLine("1231231231");
dosya.Write("123123123");
}
Sam HobbsPosted May 11, 2011, 12:27 PM
I have in the past had a problem with data being lost (not written) until I called Flush. Perhaps I did not call Close but if so then I am disappointed that Dispose or whatever it is that de-allocates the object does not do the Close.
VulpesPosted May 11, 2011, 9:44 AM
Sam HobbsPosted May 11, 2011, 1:32 AM
omkar mhaiskarPosted May 10, 2011, 5:30 AM
just go through with your following code:-
string strFileName ="mac.txt";
FileStream fileStream = new FileStream(strFileName, FileMode.OpenOrCreate);
StreamWriter streamWriter = new StreamWriter(fileStream);
streamWriter.BaseStream.Seek(0, SeekOrigin.End);
//contents = contents.Replace("@date", DateTime.Today.ToString("dd/MM/yyyy"));
streamWriter.WriteLine("1231231231");
streamWriter.Write("1231231231");
streamWriter.Flush();
streamWriter.Close();