2

I Need to find my pictures in my User folder. But I get the runtime error Access Denied

Here is my code

static void Main(string[] args)
{
    string pic = "*.jpg";
    string b = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
    string appdata = Path.Combine(b, "AppData"); // I Dont want search in this folder.
    string data = Path.Combine(b, "Data aplikací"); // Here also not.
    foreach (string d in Directory.GetDirectories(b))
    {
        try
        {
            if ((d == data) || (d == appdata))
            {
                continue;
            }
            else
            {
                foreach (string f in Directory.GetFiles(d, pic))
                {
                   //...
                }
            }
        }
        catch (System.Exception excpt)
        {
            Console.WriteLine(excpt.Message);
        }
    }
}

Running the application as admin doesn't work either. How to avoid this?

stefan
  • 10,215
  • 4
  • 49
  • 90
Lalson
  • 293
  • 1
  • 3
  • 14

2 Answers2

2

check if the folder is read only (in windows) if it is, just clear the read only flag.

if it isn't read only, make sure that the admin user has full rights on that folder. You can check this by right clicking on the folder --> properties --> security

check out this link for more information on how to set it programatically: C# - Set Directory Permissions for All Users in Windows 7

Community
  • 1
  • 1
Thousand
  • 6,562
  • 3
  • 38
  • 46
  • Ou Nice! :) It was just for read.. But is here any possibilities How to unlock it in my Program? :) – Lalson Jul 18 '13 at 19:21
  • i updated my answer, if you're looking on how to do this programatically, there are plenty of blogs/tutorials on how to do it if you just google around abit. – Thousand Jul 18 '13 at 19:25
  • Oh, please don't do this - there are read/write permissions on profile folders for a reason; don't go nuking them when there are alternative approaches. – JerKimball Jul 18 '13 at 19:42
  • You may also want to make sure the Folder is not a System or Hidden folder. Just my 2 cents. –  Jul 18 '13 at 19:43
2

Oh, don't go changing your directory/folder permissions - that's just asking for future pain.

There's no "one-liner" solution here - basically, you need to recursively walk through the folder structure looking for the files you care about, and absorbing/eating the UnauthorizedAccessExceptions along the way (you could avoid the exception altogether by checking DirectoryInfo.GetAccessControl, but that's a whole different question)

Here's a blob o'code:

void Main()
{
    var profilePath = Environment
        .GetFolderPath(Environment.SpecialFolder.UserProfile);
    var imagePattern = "*.jpg";
    var dontLookHere = new[]
    {
        "AppData", "SomeOtherFolder"
    };

    var results = new List<string>();
    var searchStack = new Stack<string>();
    searchStack.Push(profilePath);    
    while(searchStack.Count > 0)
    {    
        var path = searchStack.Pop();
        var folderName = new DirectoryInfo(path).Name;
        if(dontLookHere.Any(verboten => folderName == verboten))
        {
            continue;
        }
        Console.WriteLine("Scanning path {0}", path);
        try
        {
            var images = Directory.EnumerateFiles(
                 path, 
                 imagePattern, 
                 SearchOption.TopDirectoryOnly);
            foreach(var image in images)
            {
                Console.WriteLine("Found an image! {0}", image);
                results.Add(image);
            }
            var subpaths = Directory.EnumerateDirectories(
                  path, 
                  "*.*", 
                  SearchOption.TopDirectoryOnly);
            foreach (var subpath in subpaths)
            {
                searchStack.Push(subpath);
            }
        }
        catch(UnauthorizedAccessException nope)
        {
            Console.WriteLine("Can't access path: {0}", path);
        }
    }
}
JerKimball
  • 16,584
  • 3
  • 43
  • 55