• 0

[VB.NET] Delete File in process


Question

Ok gang,

I've got an interesting problem. I have a .net 2.0 app that goes off to a server to get a file and unzip it locally. This process is threaded. It's possible that during the first run through the process the end-user may need to stop the original process and go get a different file.

So, I abort the initial thread and start over. The issue? (Glad you asked) Seems that one or more files (the zip and possibly files from within the zip file, depending on how far along the initial process is) will not delete (File.Delete(path) as these files are in use my another process.

Exact error:

Description: The process cannot access the file 'c:\TestExtract\75872-01-1.ado' because it is being used by another process.

The code that is pulling the file from the zip is here:

--- BEGIN CODE CHUNK --------

If (sFileName <> String.Empty) Then

Dim StreamWriter As FileStream = File.Create(sDirectory & theEntry.Name)

Dim Size As Integer = 2048

Dim data(2048) As Byte

While (True)

Size = stmZipFile.Read(data, 0, data.Length)

If (Size > 0) Then

StreamWriter.Write(data, 0, Size)

Else

Exit While

End If

End While

StreamWriter.Close()

End If

--- END OF CODE CHUNK --------

Ive tried watching for the thread's abort request in that loop, but it never gets it. So I can't close the stream (arg)

I've looked to see if I can get the handle of the process that is using the file so I can kill the process and then the file... but so far no luck. I'm heading home now, but I'll check back in a few hours (it's 5pm est)

Thanks for any ideas.

Link to comment
https://www.neowin.net/forum/topic/578013-vbnet-delete-file-in-process/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

The quickest thing that I would offer would be to have some type of boolean variable that the code chunk continuously checks within it's while(true) loop. such as

While(_cancelZip = false)
   Size = stmZipFile.Read(data, 0, data.Length)
   If (Size &gt; 0) Then
	  StreamWriter.Write(data, 0, Size)
   Else
	  Exit While
   End If
End While

_cancelZip should be a private member variable that can be accessed externally through an Exposed Property.

After this loop, check to see if _cancelZip = true. If it is, you must close this StreamWriter and then delete the file.

Does this make sense?

  • 0

There is a FileSystemWatcher component that you may be able to use. Have you looked into that?

EDIT: Re-read your question and the FileSystemWatcher probably isn't what you're looking for.

Anyway, you are right in looking for the Abort Thread request, but the fact that you aren't getting that means that you are most likely not threading properly. Although bleepjay's solution will probably work, it is clumsy and not the way it should be done with the advent of Threading in .NET. I recommend you look at some of the many threading tutorials online.

Here are a few links I Googled quickly, hope it's useful:

http://msdn.microsoft.com/msdnmag/issues/01/07/vbnet/

http://www.devx.com/getHelpOn/10MinuteSolution/20365

http://www.stardeveloper.com/articles/disp...0404&page=1

http://blogs.msdn.com/dougturn/archive/200...and-vb-net.aspx

Edited by magik
  • 0
  bleepjay said:
The quickest thing that I would offer would be to have some type of boolean variable that the code chunk continuously checks within it's while(true) loop. such as

While(_cancelZip = false)
   Size = stmZipFile.Read(data, 0, data.Length)
   If (Size &gt; 0) Then
	  StreamWriter.Write(data, 0, Size)
   Else
	  Exit While
   End If
End While

_cancelZip should be a private member variable that can be accessed externally through an Exposed Property.

After this loop, check to see if _cancelZip = true. If it is, you must close this StreamWriter and then delete the file.

Does this make sense?

Hummm... the boolean is an intesting idea. I can set that, wait a moment and then kill the process. I'll give it a try in the morning.

magik,

As for the file system watcher... how would that help? I need to get a specific file at a specific time, not all the files (there are thousands of them!). But maybe you meant something else that I'm not getting.

  • 0

^ Yea, sorry, I didn't read your question fully the first time. I hope my edited post is of more use to you. :)

To clarify: Instead of looking for the Abort Request within your loop, you should have the loop within a Thread Process and then a separate Event Hander that Handles the Thread Abort Request. When that event is fired, it should halt the Thread Process.

Does that make more sense?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Ew... Not going to download that crap, even if it's free!
    • They can say whatever they want about the delay, it was paid for. No way it just happens to release exactly 1 year later, gtfo.
    • There's a similar 2025 model that's even cheaper (£474)
    • Black Myth: Wukong hits Xbox this August, exactly a year after PlayStation launch by Pulasthi Ariyasinghe Developer Game Science delivered one of the biggest action games of all time last year, with Black Myth: Wukong going on to sell tens of millions in its launch month and winning a multitude of awards. However, the title skipped out on a platform it was originally announced to launch in: Xbox. Following a long string of reasons and delays, it seems the port is finally coming to the platform. Today, Game Science announced that the Xbox Series X|S version of Black Myth: Wukong now has a release date, with it landing on August 20, 2025. Pre-orders for the title will become available starting June 18. Interestingly, this Xbox release date is set exactly one year after the original release on PlayStation 5 and PC. Game Science originally stated that the delay was due to technical issues with Xbox consoles. However, Microsoft refuted that claim quickly and alluded that the delay was due to some sort of console exclusivity deal between Game Science and Sony. In today's announcement, Game Science once again says that the delay was due to its work on porting the game to Xbox and meeting its quality standards. "Bringing Black Myth: Wukong to Xbox Series X|S—and ensuring the experience met our internal quality standards—was no easy feat," said the studio today regarding the long delay on Xbox platforms. "Fortunately, we were able to complete this challenging task smoothly within the first year of the game's official release." "If you're an Xbox player who hasn't yet experienced the game on another platform, we're confident it will stand among the finest ARPGs available on Xbox," added Game Science. For those looking to jump into the game a little cheaper than usual, Game Science will be hosting Black Myth: Wukong's first-ever sale on June 18. The 20% off discount will be available across PC, PlayStation, and even the Microsoft Store for the Xbox Series X|S pre-order.
  • Recent Achievements

    • Week One Done
      theevergreentree earned a badge
      Week One Done
    • Dedicated
      Fryer Tuck earned a badge
      Dedicated
    • Week One Done
      luxoxfurniture earned a badge
      Week One Done
    • First Post
      Uranus_enjoyer earned a badge
      First Post
    • Week One Done
      Uranus_enjoyer earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      441
    2. 2
      +FloatingFatMan
      248
    3. 3
      snowy owl
      228
    4. 4
      ATLien_0
      213
    5. 5
      Xenon
      152
  • Tell a friend

    Love Neowin? Tell a friend!