3

I'm trying to find out a way to check if there is any copy job in progress using the C# language since I'll need to restart the explorer.exe process to refresh some settings changed by my application. So I want to avoid to corrupt any copy process in progress.

Someone here has a good tip to go around this issue?

Thanks in advance

Liam
  • 27,717
  • 28
  • 128
  • 190
user2964193
  • 53
  • 1
  • 7
  • 2
    @zey that's what the OP is trying to find out. Not every question starts with "why is this code I wrote not working?". – Wim Ombelets Nov 07 '13 at 10:06
  • Hi. That's right. I don't have any piece of code for it. I'm just trying to understand if that is possible and how to do it. Thanks. – user2964193 Nov 07 '13 at 10:09
  • 2
    If you need to restart explorer.exe I would suggest that rather then just checking for copy/move-jobs are in progress you ask the user if it's ok to do it. People can have a lot of folders opened in the background, they can have some settings dialog or controlpanel running which would be closed by your application and thats just not fair. Better to ask nicely and inform about what is about to happen. – Karl-Johan Sjögren Nov 07 '13 at 10:15
  • 1
    Ok , I hope this link will help you :) > http://stackoverflow.com/questions/1576813/detect-file-copy-operation-in-windows – zey Nov 07 '13 at 10:23
  • Hi Karl-Johan Sjögren. Well I agree with you. That should be the best approach indeed. Nevertheless this application will be raised even when the user isn't present through of an agent installed on the client, so I wouldn't want to have the application hanged waiting for some user action. – user2964193 Nov 07 '13 at 10:25
  • Hi Zey. Actually I already had looked at that article but I'm not sure if it "solves" my issue, because when my application is launched the copy job could be already in progress and if I understood it correctly the filesystemwatcher will only notify you if the copy job is started when the application with the filesystemwatcher is already running. Am I right or am I missing the point here? – user2964193 Nov 07 '13 at 10:32

2 Answers2

1

I'm going out on a limb here, but afaik this is not within the (notoriously limited) possibilities of the Windows copy command.
RoboCopy however (and perhaps other, similar tools) supports returning values on completion.
This would allow you to easily prevent an (automated) kill of the explorer.exe process (manual kill notwithstanding, that would remain possible of course) as long as the return value has not been received.

Since you say the copy isn't initiated by you, it's worth using FileSystemWatcher, although that too has its limitations you need to be aware of

Community
  • 1
  • 1
Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
  • Hi Wim. The issue here is that the copy job wasn't raised for me. So what I want to know is if there is a copy job running even before I launch my application to postpone the restart explorer process. I believe this is a hard task... – user2964193 Nov 07 '13 at 10:22
  • @user2964193 `FileSystemWatcher` might have some features you could use (caveat: link to limitations in edited answer) – Wim Ombelets Nov 07 '13 at 10:35
  • thanks for you reply. So my next question is if I will get notifications from FileSystemWatcher if I launch the application with the FileSystemLauncher implemented only after the copy job been started, so when it is already in progress. Do you know if it will work in that case as well? Thanks. – user2964193 Nov 07 '13 at 10:54
  • I cannot say by heart but it is worth testing. The big mess is, that checking for open file handles on an OS level forces you to leave the managed code comfort zone and dive into the proverbial abyss... not my cup of tea, I'm afraid. – Wim Ombelets Nov 07 '13 at 12:14
  • Okay. It will be a tough task certainly :) Thanks a lot for your help. – user2964193 Nov 07 '13 at 14:33
1

You could try to get page faults delta counter for explorer.exe. See this link. If the number is high (not exact science, I know), there is something happening (perhaps a copy?).

That won't give you a definitive answer, but rather a hint. The copy may still be in progress despite of page faults being low or even zero - for example when copy displays prompt to user, waits for network, etc. You could monitor the process for few seconds and if page faults is low, ask user if it is ok to restart the process.

Untested idea, but may be worth exploring.

ya23
  • 14,226
  • 9
  • 46
  • 43
  • I've tried to check this method but I cant get a clear conclusion, so I will prefer to used another method more precise than this one. thanks anyway. – user2964193 Nov 07 '13 at 11:18