I have the following code:
List<String> suma = new List<String>();
if (File.Exists(Application.StartupPath + "/totalsold" + username))
suma = new List<String>(File.ReadAllLines(Application.StartupPath + "/totalsold" + username));
List<String> actual = new List<String>();
if (File.Exists(Application.StartupPath + "/totalsold" + username))
actual = new List<String>(File.ReadAllLines(Application.StartupPath + "/soldproducts" + username));
List<String> sumatotal = new List<String>();
if (File.Exists(Application.StartupPath + "/totalsoldadmin"))
sumatotal = new List<String>(File.ReadAllLines(Application.StartupPath + "/totalsoldadmin"));
StreamWriter vanzare = new StreamWriter(Application.StartupPath + "/soldproducts" + username);
StreamWriter total = new StreamWriter(Application.StartupPath + "/totalsold" + username);
StreamWriter totall = new StreamWriter(Application.StartupPath + "/totalsoldadmin");
Why the files vanzare,total and totall are not created after the code below is executed ?
vanzare.WriteLine("Hello World");
total.WriteLine("Helle World again!");
totall.WriteLine("Hello World again and again!");
Problem solved!
Do you close the file? Writes to files may not immediately occur, since probably both .NET and the OS are caching and thus delaying writes. The file should appear immediately when you open the
StreamWriter, though.For short-term usage of a file (e.g. for writing stuff and then closing it) you should definitely use the
usingstatement:which will make sure to properly close the file immediately afterwards and not leave any unmanaged resources around longer than they are needed.
If you need to leave the file open for longer than a single method, then of course you have to do that manually. Make sure that when you no longer need the
StreamWriter(and otherIDisposables) you call theDispose()method on them.