|
-
April 20th, 2009, 03:34 AM
#1
How to open a file when it is being writted.
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.
Code:
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.
Last edited by raulbolanos; April 20th, 2009 at 05:01 AM.
-
April 20th, 2009, 04:03 AM
#2
Re: How to open a file when it is being writted.
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.
Code:
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.
Code:
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);
}
}
-
April 21st, 2009, 06:16 AM
#3
Re: How to open a file when it is being writted.
 Originally Posted by dannystommen
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.
Code:
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.
Code:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|