Click to See Complete Forum and Search --> : How to open a file when it is being writted.


raulbolanos
April 20th, 2009, 03:34 AM
Im constantly saving in one file but it takes a while to finish the process and I want to check sometimes how is everything going in this file, but it does not allow me to take a look because its being used at the moment. I heard, that there is not problem if I open the file with notepad, even thought I still get a message like this; The Process cannot access the file because it is being used by the process.

Here is an example code, tell me if the IO functions are wrong, if should I use other functions.


using System;
using System.Threading;
using System.Collections.Generic;
using IO = System.IO;

public class Test
{
public static void Main()
{
int count = 0;

while (true)
{
IO.FileStream fs = null;
IO.BinaryWriter bw = null;
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();

string path = "C:\\Documents and Settings\\Administrator\\Desktop\\Test.tab";
fs = IO.File.Create(path);
bw = new System.IO.BinaryWriter(fs);

Console.WriteLine("{0} second", count);
Thread.Sleep(1000);
bw.Write(count);
bw.Close();
count++;
}
}
}

Thank you.

dannystommen
April 20th, 2009, 04:03 AM
I think you have this problem because you don't close the FileStream fs. Both the FileStream fs and BinaryWriter bw should also be disposed.

When you only want to write a number to a textfile, you can also use StreamWriter.


int count = 0;
string path = "C:\\Documents and Settings\\Administrator\\Desktop\\Test.tab";
while (true) {
count++;
StreamWriter SW = new StreamWriter(path, true); //the count is appended, if you want to overwrite use 'false'
SW.WriteLine(count);
SW.Flush();
SW.Close();
SW.Dispose();
Thread.Sleep(1000);
}


Or you can use the 'using' syntax. In that case the 'flush', 'close' and 'dispose' methods are executed automatically.

while (true) {
count++;
using (StreamWriter SW = new StreamWriter(path, true)) { //the count is appended, if you want to overwrite use 'false'
SW.WriteLine(count);
Thread.Sleep(1000);
}
}

raulbolanos
April 21st, 2009, 06:16 AM
I think you have this problem because you don't close the FileStream fs. Both the FileStream fs and BinaryWriter bw should also be disposed.

When you only want to write a number to a textfile, you can also use StreamWriter.


int count = 0;
string path = "C:\\Documents and Settings\\Administrator\\Desktop\\Test.tab";
while (true) {
count++;
StreamWriter SW = new StreamWriter(path, true); //the count is appended, if you want to overwrite use 'false'
SW.WriteLine(count);
SW.Flush();
SW.Close();
SW.Dispose();
Thread.Sleep(1000);
}


Or you can use the 'using' syntax. In that case the 'flush', 'close' and 'dispose' methods are executed automatically.

while (true) {
count++;
using (StreamWriter SW = new StreamWriter(path, true)) { //the count is appended, if you want to overwrite use 'false'
SW.WriteLine(count);
Thread.Sleep(1000);
}
}


Thank you very much.. it was very very helpful the using sintax, because of it I have advanced so much today.