1

Need to wait until:

  1. File downloads completely,
  2. Downloaded file is unlocked.

Till now I am successful in identifying that file is created on specific location. Code is below

FileSystemWatcher FileSystemWatcher = new FileSystemWatcher(downloadpath);
FileSystemWatcher.WaitForChanged(WatcherChangeTypes.Created, 5000);

I want to wait till any attached process detaches its self from the downloaded file so that I can use it again.

Vaibhav Dhasmana
  • 151
  • 2
  • 13
  • 2
    how are you downloading? – Vivek Nuna Aug 23 '19 at 06:35
  • [Maybe related?](https://stackoverflow.com/questions/50744/wait-until-file-is-unlocked-in-net) – ProgrammingLlama Aug 23 '19 at 06:39
  • @vivek nuna by clicking a download hyperlink on webpage – Vaibhav Dhasmana Aug 23 '19 at 06:45
  • @John while creating this post I didn't find any related quesion in c# – Vaibhav Dhasmana Aug 23 '19 at 06:46
  • 1
    Then is that question related or not? It seems that you're asking how to find out when another application stops having a lock on the file. Whether it's a download, a spreadsheet being opened by Excel, etc. seems to be neither here nor there. Feel free to clarify why this is important if it is. – ProgrammingLlama Aug 23 '19 at 06:53
  • 3
    there are some simple answers .. a) some browsers dont actually name the file to its actual final name until its finished, b) the file is usually locked by the browser, and so just try opening the file with a lock to see if its done or not – BugFinder Aug 23 '19 at 06:57
  • @John I misunderstood your comment. Sorry. Basicaly I am trying to download a file and I want to read it again throug code. Before reading the same downloaded file I want to make sure that required file is not locked otherwise I will get an exception. – Vaibhav Dhasmana Aug 23 '19 at 06:58
  • @BugFinder Thanks. I will try it. – Vaibhav Dhasmana Aug 23 '19 at 07:01
  • 1
    Just some thoughts about you using a FileWatcher: You get notified if the file size changes. So you could take that as a hint: Did the filesize change during last X seconds? Yes = wait anther turn, No = try to open, catch exception and wait another turn if one is caught (of course, break if the exception is "not found" or anything that hints to that waiting won't help). – Fildor Aug 23 '19 at 07:24
  • @Fildor That is a very good idea. Thanks. – Vaibhav Dhasmana Aug 23 '19 at 08:12
  • Strange Question, just don't let the calling thread exit, and it will be blocked or use a busy indicator if its Ui, you need to provide much more details to get a convincing answer for this question – Mrinal Kamboj Aug 23 '19 at 08:40

0 Answers0