tags:

views:

135

answers:

4

At my company, we are writing a script to take care of simple tasks that we usually would do by hand. I am using c# to delete profiles in c:\documents and settings\, except a few. These will simply be left alone. The problem is that even with code that sets the files to normal and marks the admin user as an owner, they won't delete. They say that the quick launch folder has access denied. I am using a recursive permissions change method and I know that it works. Same thing with file attributes. Why won't it work? How do I fix this?

+3  A: 

It might not be a permissions issue, but a file locking issue. Both will appear to be a permissions issue.

Randolpho
File locking is an attribute that would get set by setting attributes to normal, right?
@user315881: No, I mean the file is locked by a process that has a locking handle against the file (usually that means the file is open for writing). You can't turn it off except via the process that obtained the lock.
Randolpho
You can check whether locking handles cause the deletion problem with a small utility called *Unlocker*. It can be freely downloaded from the 'net.
stakx
A: 

It is very tricky, you will need to use PInvoke to call native APIs (and I'm not sure which ones). Unlocker, a utility to unlock files being used by other processes do this. It is not yet working with Windows 7 and 64-bit versions of some editions of windows.

TheVillageIdiot
Unlocker used to be great, but I think it was in 1.8.6 that a backdoor trojan horse was added (those 'ebay' shortcuts). The website claims that the ebay shortcuts have been removed and replaced with a 'bing toolbar', but I'm still a little leery of using it.
Zach Johnson
yes @Zach Johnson sadly this is the case
TheVillageIdiot
+6  A: 

The problem could be that other processes have open handles to those files or directories. To help you find out which processes are to blame, you could use the Find feature of Process Explorer to find them.

(Just to be clear: this isn't a suggestion on how you can write code to delete the files, but a suggestion on how to find out exactly what's preventing your current system from working.)

RichieHindle
+1 for beating me by seconds on Process Explorer :)
csharptest.net
+1  A: 

You can verify if this is being caused by file locking rather than access control by download the Process Explorer tool from http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

Run as Administrator and use the Find->Handle or Dll to see if it's being locked.

Otherwise you should be able to take ownership and grant access to delete without a problem.

csharptest.net