title: VB.Net 3.5 Check if file is in use
body: <p>I have an update program that is completely independent of my main application. I run the update.exe to update the app.exe. To check to see if the file is in use, I move it to another file and catch an error if it is in use. If no problems occurr I rename it back. At least that was my plan...</p>
  
  <blockquote>
    <p>Main program: app.exe<br/>
    Update program:  update.exe</p>
  </blockquote>
  
  <p>This program is used over the network without any services running. So, the users is quite literally running the exe over the network on thier machine.</p>
  
  <p>I need to update the app.exe when the update.exe has run. To check to see if the app.exe was still in use I was wrapping the following in a try/catch to see if it failed:</p>
  
  <blockquote>
    <p>IO.File.Move(upddir &amp; "\app.exe", upddir &amp; "\app.exe.tst")<br/>
    IO.File.Move(upddir &amp; "\app.exe.tst", upddir &amp; "\app.exe")</p>
  </blockquote>
  
  <p>The funny thing is, even if the app.exe is running, the move can rename it to the app.exe.tst without any error. I can even continue on in the application without any errors. </p>
  
  <p>I thought I was out of my mind, so I had another programmer look at this and he verified what I said above.</p>
  
  <p>So, we tried this wrapped in a try catch:</p>
  
  <blockquote>
    <p>Dim readfile As New FileStream(upddir &amp; "\app.exe", FileMode.Open, FileAccess.Read, FileShare.None)<br/>
    readfile.Dispose()</p>
  </blockquote>
  
  <p>I put the fileshare as none that, at least I thought it would, show that there was someone in the file.</p>
  
  <p>It still continued without any error.</p>
  
  <p>Anyone know why I can rename a file in use?
  Also, is there a better way to do this than what I am doing now?</p>
  
  <p>Thanks!
  Eroc</p>