locked
C:\Documents and Settings' is denied RRS feed

  • Question

  • Hello.

    I used the code below. If i it run "C:\Documents and Settings' is denied" appears. How can I skip denied folders?

    Thank You.

                string rootDirectory = System.IO.DriveInfo.GetDrives()[0].RootDirectory.FullName;
                string[] files = System.IO.Directory.GetFiles(rootDirectory,
                            "file.exe", System.IO.SearchOption.AllDirectories);

                foreach (var str in files)
                {
                    Console.WriteLine(files);
                }

    Wednesday, November 25, 2020 5:57 PM

All replies

  • I don't think there is any folder actually named that. What you have run into is like kind of a folder short-cut acting as a folder. 
    Wednesday, November 25, 2020 6:10 PM
  • Thanks for your answer.

    Yes you are right it is a link to a folder that cannot be deleted because it is part of Windows 10. But there are other forbidden folders on which the application falls for example System Volume Information.

    • Edited by Blader Wednesday, November 25, 2020 6:24 PM
    Wednesday, November 25, 2020 6:21 PM
  • Hello,

    Here is a class that might help, the RecursiveFolders method which as is does nothing but the catch is triggered on not enough permissions for the folder. Since the code is in a class there are events the caller can subscribe too.

    using System;
    using System.IO;
    
    namespace TraverseHelper
    {
        public class Operations
        {
    
            public delegate void OnException(Exception exception);
            /// <summary>
            /// Callback for subscribers to know about a problem
            /// </summary>
            public static event OnException OnExceptionEvent;
    
            public delegate void OnTraverseFolder(string item);
            /// <summary>
            /// Callback for when a folder is being processed
            /// </summary>
            public static event OnTraverseFolder OnTraverseEvent;
    
    
            /// <summary>
            /// Example that will run freely and the app will be unresponsive which
            /// is how many developers approach reading folders and only try with a smaller
            /// folder structure but larger structures will freeze the app thus we
            /// need to consider an asynchronous method.
            /// </summary>
            /// <param name="path">Folder to work with</param>
            /// <param name="indentLevel">Indent level for display with Console.WriteLine</param>
            public static void RecursiveFolders(string path, int indentLevel)
            {
    
                try
                {
    
                    if ((File.GetAttributes(path) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint)
                    {
    
                        foreach (var folder in Directory.GetDirectories(path))
                        {
                            OnTraverseEvent?.Invoke(folder);
                            Console.WriteLine($"{new string(' ', indentLevel)}{Path.GetFileName(folder)}");
                            RecursiveFolders(folder, indentLevel + 2);
                        }
    
                    }
    
                }
                catch (UnauthorizedAccessException unauthorized)
                {
                    OnExceptionEvent?.Invoke(unauthorized);
                }
            }
    
        }
    }
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    • Proposed as answer by KHURRAM RAHIM Thursday, November 26, 2020 9:54 AM
    Wednesday, November 25, 2020 6:53 PM
  • Thank you,

    but i am  not looking for folder I am looking for exe file.

    Best regards
    Wednesday, November 25, 2020 7:29 PM
  • Thank you,

    but i am  not looking for folder I am looking for exe file.

    Best regards

    The take-a-way is no matter if it's folders or files is to wrap code in a try/catch using the following as shown in my code sample.

    UnauthorizedAccessException unauthorized


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    My GitHub code samples
    GitHub page

    Check out:  the new Microsoft Q&A forums

    • Proposed as answer by KHURRAM RAHIM Thursday, November 26, 2020 12:12 PM
    Wednesday, November 25, 2020 9:26 PM
  • Thank you, Kareninstructor

    I need to study it.

    Best regards.

    Thursday, November 26, 2020 4:20 PM
  • Thank you,

    but i am  not looking for folder I am looking for exe file.

    Best regards
    You're looking for a file on your machine?
    If so, can you find this file manually?

    BP-LP 2016/2020 @ll rights reserved



    Monday, November 30, 2020 12:20 PM