CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Location
    Stuttgart, Germany
    Posts
    56

    Question 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.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    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);
                }
             }

  3. #3
    Join Date
    Mar 2009
    Location
    Stuttgart, Germany
    Posts
    56

    Re: How to open a file when it is being writted.

    Quote Originally Posted by dannystommen View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured