9 Replies - 3067 Views - Last Post: 08 June 2011 - 10:29 AM Rate Topic: -----

#1 nelson2391  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 25-May 11

how to check a file is currently using or not?

Posted 07 June 2011 - 01:17 AM

[code]
For Each f1 In diar1
f1.Delete()
'lblnumber.Text = "C:\WINDOWS\Temp\" + dra.Name.ToString()
Next
[code\]

but i got this error message,
"The process cannot access the file 'kls3FBE.tmp' because it is being used by another process."

How can i check the file is currently using or not?

Thanks in advance..:)

Is This A Good Question/Topic? 0
  • +

Replies To: how to check a file is currently using or not?

#2 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 307
  • Joined: 30-January 11

Re: how to check a file is currently using or not?

Posted 07 June 2011 - 01:55 AM

One method is to use a try catch block but to be more specific use A IOException


        Try

        Catch ex As IOException


        End Try



Was This Post Helpful? 0
  • +
  • -

#3 nelson2391  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 25-May 11

Re: how to check a file is currently using or not?

Posted 07 June 2011 - 02:06 AM

okay, but, if any error occur, it just skip. But i need to know is it currently using or not, without a skip.
Was This Post Helpful? 0
  • +
  • -

#4 Jeff H  Icon User is offline

  • D.I.C Regular

Reputation: 112
  • View blog
  • Posts: 307
  • Joined: 30-January 11

Re: how to check a file is currently using or not?

Posted 07 June 2011 - 03:09 AM

If Catch block is empty then it just skips it and if the try catch is inside or contains the for each loop it will change its behavior.


You might want to post your code that fills diar1 and are you sure you are closing all resources?

I like to use a using block because it close it, call dispose if needed, and the compiler will generate a try catch finally block for you and call dispose etc.. for you in finally block.


A simple console app to show try catch

Imports System.IO

Module Module1

    Sub Main()
        Dim fStream As New FileStream("C:\Hpad\Test\TestOpen.txt", FileMode.Open)

        For Each fileName In Directory.GetFiles("C:\Hpad\Test")
            Try
                File.Delete(fileName)
            Catch ex As IOException
                Console.WriteLine(String.Format("{0} {1} created error {1} {2}", fileName, Environment.NewLine, ex.Message))
            End Try

        Next

        Console.ReadLine()

    End Sub

End Module




Same one updated that will not cause an error because of 'using' block

Imports System.IO

Module Module1

    Sub Main()
        Using fStream As New FileStream("C:\Hpad\Test\TestOpen.txt", FileMode.Open)

        End Using

        Try
            For Each fileName In Directory.GetFiles("C:\Hpad\Test")
                File.Delete(fileName)
            Next

        Catch ex As IOException
            Console.WriteLine(ex.Message)
        End Try





        Console.ReadLine()

    End Sub

End Module


This post has been edited by Jeff H: 07 June 2011 - 03:10 AM

Was This Post Helpful? 0
  • +
  • -

#5 nelson2391  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 25-May 11

Re: how to check a file is currently using or not?

Posted 07 June 2011 - 04:30 AM

Thank you. Its working
Was This Post Helpful? 0
  • +
  • -

#6 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: how to check a file is currently using or not?

Posted 07 June 2011 - 03:46 PM

I have never used a "Using" block. Can anyone provide a short explanation of what it does?
Was This Post Helpful? 0
  • +
  • -

#7 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: how to check a file is currently using or not?

Posted 08 June 2011 - 01:27 AM

Basically, a using block ensures that an object's Dispose() method is called once you have finished using it.

In the example in this thread (slightly modified to write "Test" to the .txt file):

Using fStream As New FileStream("C:\Hpad\Test\TestOpen.txt", FileMode.Append)
          Dim b As Byte() = Encoding.ASCII.GetBytes("Test")
          fStream.Write(Encoding.ASCII.GetBytes("Test"), 0, b.Length)
End Using



we are 'using' the FileStream object in the using block. The FileStream object is only available inside the using block.

Once all the code in the using block has executed OR the code in the using block throws an exception, the Dispose() method of the FileStream object is called automatically in order to clean up the resources used by the object. You don't have to call Dispose(), it is done automatically for you by the using block.


Behind the scenes, this is essentially what that using block code gets translated to:

Dim fStream As New FileStream("TestOpen.txt", FileMode.Append)

Try
       Dim b As Byte() = Encoding.ASCII.GetBytes("Test")
       fStream.Write(Encoding.ASCII.GetBytes("Test"), 0, b.Length)
Finally
       If Not fStream Is Nothing Then
          fStream.Dispose()
       End If
End Try



That is all the using block is really... Syntax sugar for a try-finally block. Essentially, no matter what happens*** (see below for a few small exceptions to this) in the using block, Dispose() is always called automatically on the object your 'using' for you when the block has finished executing.

The object your using in the block (FileStream in this case) must implement the IDisposable interface, as this guarantees that the object has a Dispose() method for the using block to call :)

It is advised that for any object that implements IDisposable (and can therefore be 'disposed'), you try and wrap it in a using block so you don't forget to call Dispose() on it when your done with it.

Anyhow, just know that you should always call Dispose() (via a using block, or by calling it directly) on objects that implement IDisposable in order to free the resources used by them.


For anyone interested, here is the IL code that the using block above gets translated to (if you put the using block in the Main method of Module1). You can clearly see the try and the finally blocks:

Spoiler



***Well, finally blocks are NOT always guarenteed to be executed. For example, if the executing thread is killed using the Win32 TerminateThread function, or the process is killed using the Win32 TerminateProcess function or the System.Environment.FailFast() method, whilst in a try block (or a using block, as remember that a using block is just a try-finally block behind the scenes), then the finally block will not be executed, meaning that Dispose() will not be called. It will then be down to the operating system to clean up the resources :)

This post has been edited by CodingSup3rnatur@l-360: 08 June 2011 - 01:44 AM

Was This Post Helpful? 2
  • +
  • -

#8 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,499
  • Joined: 16-March 11

Re: how to check a file is currently using or not?

Posted 08 June 2011 - 07:38 AM

I've always enjoyed using blocks, as they provide for cleaner code. :)
Was This Post Helpful? 0
  • +
  • -

#9 Psyguy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 69
  • View blog
  • Posts: 314
  • Joined: 12-January 11

Re: how to check a file is currently using or not?

Posted 08 June 2011 - 08:06 AM

Thanks, that was a very good explaination!
Was This Post Helpful? 1
  • +
  • -

#10 nelson2391  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 14
  • Joined: 25-May 11

Re: how to check a file is currently using or not?

Posted 08 June 2011 - 10:29 AM

yeah really good explanation.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1