[VB.net] - Access Denied Exception Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zubair1
    New Member
    • Sep 2008
    • 79

    [VB.net] - Access Denied Exception Error

    Hello,

    I have a program which deletes files from cache

    for exampe:-
    Temporary Internet Cache
    History Cache
    Cookie Cache
    and finally TEMP cache

    while i (my program) was deleting the TEMP cache an exception error occured saying that access is denied :(

    Why is this error occuring - i manually browsed to the TEMP folder and tried to delete that file and it gave me an access denied error too :(
    could some one provide a working solution for this

    i'm using VB.net += VB2005

    Thanks in Advance
    Regards
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    If the system is preventing you deleting the file, there will be no workaround with .NET. The o/s is preventing deletion of the file and likely with good reason. The likely cause is that an application is currently using that file. You'll probably say "I don't have any applications open", but I'll bet if you open task manager you'll probably find a bunch of applications in the list that are resident in memory that don't show up on your toolbar. One of these has either got the file locked - or a previous application didn't unlock the file before it closed down. If no application is currently using the file, then eventually the o/s will unlock it - otherwise, the only alternative is to reboot the computer you're running the code on, at which point the file will be unlocked and you can delete it either through Windows Explorer or through your .NET code...

    Hope that helps,

    Ben

    Comment

    • zubair1
      New Member
      • Sep 2008
      • 79

      #3
      Hi,

      Thanks Ben :)

      Actually i wasn't going to say that "Theres no application =P" i had a few like Internet Explorer, MSN, FireFox, 3D StudioMAX :( i'll try and close them all and see if it helps any.. i assume it wont for now..

      anyways, Mr.Expert :D how do i delete the file :( have you used the software Crap Cleaner / cCleaner ? it cleans these files - i'm not sure how :( but i am pretty sure it does it after a reboot (maybe).

      You are also saying the same to reboot and delete but sir could you tell me how i can accomplish this by demostrating a small chunk of code or something i am very new to VB.net :(

      Thanks in Advance
      Regards

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Code:
        Try
          Dim oSearchPath = System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
          Dim oFolder = New System.IO.DirectoryInfo(oSearchPath)
          For Each oFile As System.IO.FileInfo In oFolder.GetFiles()
            oFile.Delete()
          Next
        Catch ex As Exception
          MessageBox.Show("Unable to delete file. " & ex.Message)
        End Try
        Of course, if the file is locked...you will get the message box telling you that the file couldn't be deleted, which could get annoying pretty quickly. Better to write the file name and the reason the file couldn't be deleted to a log that you can review afterwards...

        If you want to have these deleted on reboot, you have to set up a sentinel that can be loaded as a service as windows starts so that it beats all other applications to the folders. You obviously want this to run as the o/s boots rather than after you've logged in. So you'd write some kind of service to run once as the system is booting up. Windows services are relatively straight forward, although I will admit I've never written one for this purpose so I'm not sure exactly how difficult it will be.

        My first thought is that your process for deleting the files would save a log of files to delete at reboot (those which it couldn't delete successfully), when the process is complete, if the log holds any file paths, then it flags the user to tell them that some files will need to be removed after the next restart. The Windows Service you wrote would then be set to autostart which would allow it to start automatically at the next reboot. It could then read the log file and delete each file that occurred before which setting itself back to manual and stopping.

        Comment

        • jg007
          Contributor
          • Mar 2008
          • 283

          #5
          to find out why you cannot delete the file download the sysinternals program ' handle.exe ' when ran against a file it will show you which program is using the file.

          to delete on reboot you will need to find some way of making your program delete the files BEFORE this program starts .

          in general there are a fairly small amount of files locked and most of them are only used until you next restart anyway so I would be inclined to sugest that you just implement some error handling to ignore any locked files and just delete those which you can as it would be much easier.

          Comment

          • zubair1
            New Member
            • Sep 2008
            • 79

            #6
            Originally posted by balabaster
            Code:
            Try
              Dim oSearchPath = System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
              Dim oFolder = New System.IO.DirectoryInfo(oSearchPath)
              For Each oFile As System.IO.FileInfo In oFolder.GetFiles()
                oFile.Delete()
              Next
            Catch ex As Exception
              MessageBox.Show("Unable to delete file. " & ex.Message)
            End Try
            Of course, if the file is locked...you will get the message box telling you that the file couldn't be deleted, which could get annoying pretty quickly. Better to write the file name and the reason the file couldn't be deleted to a log that you can review afterwards...

            If you want to have these deleted on reboot, you have to set up a sentinel that can be loaded as a service as windows starts so that it beats all other applications to the folders. You obviously want this to run as the o/s boots rather than after you've logged in. So you'd write some kind of service to run once as the system is booting up. Windows services are relatively straight forward, although I will admit I've never written one for this purpose so I'm not sure exactly how difficult it will be.

            My first thought is that your process for deleting the files would save a log of files to delete at reboot (those which it couldn't delete successfully), when the process is complete, if the log holds any file paths, then it flags the user to tell them that some files will need to be removed after the next restart. The Windows Service you wrote would then be set to autostart which would allow it to start automatically at the next reboot. It could then read the log file and delete each file that occurred before which setting itself back to manual and stopping.

            Thanks for the prompt response :)

            I tried running the code -

            i copied and pasted the code into my form load event
            and when it loaded nothing happened - no errors no thing :o

            i also checked the cache and its still there too :o

            Really appreciate your Efforts
            :)

            Comment

            Working...