Accessing a file by more than one process

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaleel
    New Member
    • Sep 2007
    • 38

    Accessing a file by more than one process

    Hi
    I am appending two files using following code.


    Code:
    Try
                   
    	   'appending
    
                    Using fs As New FileStream(sourceFile, FileMode.Append, FileAccess.Write)
    
                        Dim buffer() As Byte = File.ReadAllBytes(appendFile)
    
                        fs.Write(buffer, 0, buffer.Length)
                        fs.Flush()
                    End Using
    
                    'Delete the second  file 
                    If (File.Exists(appendFile)) Then
                        File.Delete(appendFile)
                    End If
    
             
                Catch ex As Exception
    
          
    
                End Try
    When I am running this code by using F11 , this works fine. But when running without breakpoints, it throws an exception by saying "appendfile is being used by another process ". I think this occurs at deletion of appendfile before it is appended to sourcefile. What can I use to prevent this from happening? Any help will be grateful...Will Applicatio.Doev ents() or sleep the current thread is a feasibile solution..?

    Thanks
  • jaleel
    New Member
    • Sep 2007
    • 38

    #2
    I was really waiting 4 a reply for this question. But nobody responded. Is there any confusion in my question?This clarification is bcoz I really need an answer to go ahead.


    Hi here sourcefile is the file to which appending other file(appendFile) is done.
    Using fs As New FileStream(sour ceFile, FileMode.Append , FileAccess.Writ e)
    Dim buffer() As Byte = File.ReadAllByt es(appendFile)

    After copying the appendFile , it is deleted here.


    'Delete the second file
    If (File.Exists(ap pendFile)) Then
    File.Delete(app endFile)
    End If

    This code works when we run with breakpoint at the place where this method is called.That is here framework got some time for execution. But without breakpoints , it is not working and tells the appendFile is being used by some other process.

    Pls help me ...

    Comment

    • bala2it4u
      New Member
      • Jun 2007
      • 30

      #3
      Originally posted by jaleel
      I was really waiting 4 a reply for this question. But nobody responded. Is there any confusion in my question?This clarification is bcoz I really need an answer to go ahead.


      Hi here sourcefile is the file to which appending other file(appendFile) is done.

      Dim buffer() As Byte = File.ReadAllByt es(appendFile)

      After copying the appendFile , it is deleted here.





      This code works when we run with breakpoint at the place where this method is called.That is here framework got some time for execution. But without breakpoints , it is not working and tells the appendFile is being used by some other process.

      Pls help me ...
      In c# they use
      fs.clear();
      and
      fs.close();
      after flash statement
      try this any see

      Comment

      • jaleel
        New Member
        • Sep 2007
        • 38

        #4
        thanks 4 atleast one reply.

        Here we are using the keyword Using with Filestram object. So there is no need of closing it explicitly. The dispose method of the filestream will be automatically called when filestram object is no longer used.


        thanks

        Comment

        • Shashi Sadasivan
          Recognized Expert Top Contributor
          • Aug 2007
          • 1435

          #5
          Hi,
          If you take this example like a library, While you are reading a book off the shelf, others cant. They have to wait for you to close it (assuming the book returns to its original place)
          The same applies for files being used.

          Now, what you do need to clarify is if you are doing a web based application or windows based.

          what do you mean by different processes. (it could mean completely different in terms or service process, web app, windows form)

          most of the time you can get away with it by using the singleton pattern.

          cheers

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Your timezone is in the wee early hours of monday morning (Your post came at 2am on monday for me)

            You never closed the fs (use fs.Close()), so the file handle is still open.
            You should be sure to explictly close any and all filehandles and streams when you're done using them.

            Comment

            • jaleel
              New Member
              • Sep 2007
              • 38

              #7
              Hi shashi,
              Thanks 4 ur reply

              Now, what you do need to clarify is if you are doing a web based application or windows based.
              This is a windows application

              what do you mean by different processes. (it could mean completely different in terms or service process, web app, windows form)
              actually no two processes (explicitly) accessing the file. Here let me explain things in a better way:
              1. I have a main file to which i want to append a second file(used the code in the first post)
              2. After the appending process, I want to delete the second file.

              that is, these actions shud be happened in a sequential way.But here what happens is, appending may not be completed when delete method on the second file is getting called.( Refer the code is in the first post).. That is , the reading process of second file may be still continuing inorder to write to the first file.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by jaleel
                But here what happens is, appending may not be completed when delete method on the second file is getting called.
                I would say it's actually most defenitly because you still have the "fs" file handle open on the file.

                Comment

                • jaleel
                  New Member
                  • Sep 2007
                  • 38

                  #9
                  Thanks Plater,

                  Actually if the second file(to be appended ) is a .txt file , this code works fine even without fs.close().

                  But when the file types are complex with some images inside or a large file , File.ReadAllByt es(appendFile) will return a huge Byte() array . A considerable amount of time is needed to complete the writing of this array to first file.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Edit: I have the objects backwards in my head.

                    Comment

                    Working...