Home All Groups Group Topic Archive Search About

Delete files/directories through VB Net

Author
1 Nov 2006 8:28 PM
nospam
I am writing this app in .net 2003 since all machines don't have 2.0
framework.  I am trying to delete old profiles, but I am getting access
denied or file in use errors. A lot of these files I am able to delete
in Windows Explorer.
The app is getting launched using the System account, so there is
plenty of rights to delete the files.

I have tried the following:

Recursive delete of every file using Kill()
fso.DeleteFolder(path,True)
Directory.Delete(path)

Set Owner on directories to System and still can't delete.  Can't
delete after reboot, even though the user has not logged back in.  Most
of the files are index.dat from Cookies or Temp internet folders.

Is there any way to brute force delete these files and folders?  I want
them deleted, and don't want to have to shell out to a third party app.
Thanks for any help

Author
2 Nov 2006 4:40 AM
Cor Ligthert [MVP]
nobody,

I am not sure how you do it on those clients, but you can always try to do
it with the diagonsic process

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDiagnosticsProcessClassTopic.asp

I hope this helps,

Cor

<nospam@meatonconsulting.com> schreef in bericht
Show quoteHide quote
news:1162412934.691029.116290@h48g2000cwc.googlegroups.com...
>I am writing this app in .net 2003 since all machines don't have 2.0
> framework.  I am trying to delete old profiles, but I am getting access
> denied or file in use errors. A lot of these files I am able to delete
> in Windows Explorer.
> The app is getting launched using the System account, so there is
> plenty of rights to delete the files.
>
> I have tried the following:
>
> Recursive delete of every file using Kill()
> fso.DeleteFolder(path,True)
> Directory.Delete(path)
>
> Set Owner on directories to System and still can't delete.  Can't
> delete after reboot, even though the user has not logged back in.  Most
> of the files are index.dat from Cookies or Temp internet folders.
>
> Is there any way to brute force delete these files and folders?  I want
> them deleted, and don't want to have to shell out to a third party app.
> Thanks for any help
>
Author
3 Nov 2006 3:03 PM
nospam
Cor,
Thanks for the reply.  I ended up writing a service, starting as Local
System since that always has full rights to the file system.  I then
dropped down to Kernel32:

Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA"
(ByVal lpFileName As String) As Long

Private Sub DeleteProfileFolder(ByVal UserHomeDir As String)
        Try
            Dim Folder As String
            Dim File As String

            For Each Folder In Directory.GetDirectories(UserHomeDir)
                For Each File In Directory.GetFiles(Folder)
                    DeleteFile(File)
                Next
                DeleteProfileFolder(Folder)
            Next
        Catch Ex As Exception
            WriteLog(Ex.ToString, 2)
        End Try
    End Sub

Unfortunately I had to use FSO.DeleteFolder to delete the main
directory since it has the force option, but all is working well now.
Hopefulle this can help out someone in the future.