Could not find a part of the path and access denied

Posted on 2008-11-09
Last Modified: 2013-11-26
I have 2 problems which I think are related and need some help with.  I am writing a small program to clean some temp directories.

1) I get the following error when I run this code anywhere in my program
System.IO.Directory.GetFiles("C:\Users\xxx\AppData\Local\Microsoft\Windows\Temporary Internet Files\")

DirectoryNotFoundException was unhandled
Could not find a part of the path 'C:\Users\xxx\AppData\Local\Local\Microsoft\Windows\Temporary Internet Files'.

Basically I am trying to clean the temporary internet files folder.  I am assuming that because it is a hidden folder the program cannot see it.

2) My program is getting the following error when attempting to delete the files
Access to the path 'C:\Users\xxx\AppData\Local\Temp\test.txt' is denied.

Wierd thing is that 98% of the files in this directory it will delete but has a problem with a few. (It is not because they are in use).  The current user can manual delete this file with no problems but the program cannot.  It seems as though it is a permission problem but I am not sure why.  I have tried adding everyone with full control to the file and the file still cannot be deleted. I am currently running on vista.

Thanks for the help.
Question by:inimicaljords
    LVL 1

    Assisted Solution

    Hi Jordan,

    I would think a better way to do that would to instead of hard-coding the Directory, get the current user directory, then work from there.

    Or else you could just run the program as an Administrator. If not it may be the fact that it is a hidden folder, or the program is open, either of those.

    Hope that helps

    LVL 1

    Author Comment

    hard coding or using environment variables didn't change anything.  Same result.

    I know cleaning temp internet files for a user can be done from that user account and not the administrator.  I have used many programs that can accomplish this.  I just can't figure out why the program cannot see the folder but the user can.

    Assisted Solution

    Try this to get the path (VB.NET 2005):


    See if this gives you a different path than the ones you tried or the environment variables you had.
    LVL 1

    Accepted Solution

    ok guys, i haven't abandon this question but have been extremely busy.  I tried the internet cache and it doesn't give me the error anymore so it sees that directory but it doesn't see any directories in it.

    FYI this is coding for Vista:

    When I run this it scans:
    C:\Users\xxx\AppData\Local\Microsoft\Windows\Temporary Internet Files\Low\Content.IE5\

    which on my current machine I have 4 folders with random names like 5REDNR1R
    The program cannot see the 5REDNR1R folder now.  If I hard code it like this:
                For Each d In Directory.GetDirectories(internetTemp & "\Low\Content.IE5\5REDNR1R\")
    I get the same error as before, path cannot be found

    Any clues?

    Dim internetTemp As String = System.Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)            
    Dim s, d As String
    For Each s In System.IO.Directory.GetFiles(internetTemp & "\Low\Content.IE5\")
                        fcount += FileLen(s)
                    Catch ex As Exception
                        fcount -= FileLen(s)
                    End Try
                Next s
                For Each d In Directory.GetDirectories(internetTemp & "\Low\Content.IE5\")
                        System.IO.Directory.Delete(d, True)
                    Catch ex As Exception
                    End Try
                Next d

    Open in new window

    LVL 1

    Author Comment

    no solution found

    Expert Comment

    inimicaljords,  Thanks for the points. I don't have any experience with Vista security yet, but FWIW, have you seen these links about the internet cache in Vista and IE protected mode?  

    Code Project - A Developer's Survival Guide to IE Protected Mode
    This one is in C++, but informative to me:  

    IEBlog - Protected Mode in Vista IE7

    Understanding and Working in Protected Mode Internet Explorer

    Best o' luck,

    LVL 1

    Expert Comment

    Change your Programs app.manifest file to look something like the code snippet. Change your project settings to compile a user created/modified Manifest, this will request Admin Rights before running.

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
            <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
                            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                                    <requestedExecutionLevel level="requireAdministrator"/>

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now