none
'Access to the path 'F:\System Volume Information' is denied.'? RRS feed

  • Question

  • I get this error message 'Access to the path 'F:\System Volume Information' is denied.' when I run the code below this text. 

    How can I ignore 'System Volume Information'?

     string[] originalFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);
       Array.ForEach(originalFiles, (originalFileLocation) =>
      {
            FileInfo originalFile = new FileInfo(originalFileLocation);
            FileInfo destFile = new FileInfo(originalFileLocation.Replace(sourceFolder, destiniationFolder);                   

              if (destFile.Exists)
                        {
                        if (originalFile.Length > destFile.Length)
                        {
                            originalFile.CopyTo(destFile.FullName, true);
                        }
                        else
                        {
                            Directory.CreateDirectory(destFile.DirectoryName);
                            originalFile.CopyTo(destFile.FullName, false);
                        }

                });

                   
    Saturday, October 19, 2019 6:37 PM

All replies

  • Test for System Volume Information contains into the string using string.Contains.

    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.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, October 19, 2019 6:51 PM
    Moderator
  • I don't think that i can use the string.Contains because this error occurs at this line

    string[] originalFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);

     
    Sunday, October 20, 2019 11:25 AM
  • I don't think that i can use the string.Contains because this error occurs at this line

    string[] originalFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories);

    You can find old threads like for example :

    Directory.getfiles gives me access denied

    • Proposed as answer by simonb549 Sunday, October 20, 2019 2:01 PM
    Sunday, October 20, 2019 12:03 PM
  • I have tried to use LINQ like this but it does work.

    string[] originalFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories).Where(f => !f.Contains("System Volume Information").ToString())

    Is it possible in this case to use LINQ?

    Sunday, October 20, 2019 12:39 PM
  • string[] originalFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories).Where(f => !f.Contains("System Volume Information").ToString())

    Is it possible in this case to use LINQ?

    No, you can't use LINQ here. This would be LINQ-to-objects, not LINQ-to-SQL which is what you probably have in mind.

    With LINQ-to-SQL (or -to-Entities) the lambda expression is converted into a SQL Query that gets sent to the server, and then the server only returns the results that satisfy the expression. If you were doing this then yes, you could use linq to solve the issue.

    But in this case, you are applying LINQ to an array of strings. This results in the execution of the Where extension method in IEnumerable<T>. This method has to enumerate the entries in the array and evaluate each of them using the lambda expression. This means that the collection of elements has to exist before the LINQ is triggered. In other words, the Directory.GetFiles has to return all of the files before the LINQ query starts filtering them out. Since the error happens precisely when Directory.GetFiles does this, then the LINQ query cannot do anything to resolve your problem.

    So, what's the solution? You use Directory.GetFiles without the AllDirectories option. You combine it with .GetDirectories to get the nested directories, compare each of them with "System Volume Information" (or use a try...catch), and for each of the other ones you invoke your search routine recursively to continue descending the directory tree. Yes, this requires half a dozen lines of code, which is more trouble than simply using the AllDirectories option in a single line of code. But it is not that difficult to do and gives you greater control on the search file process (you may find mor forbidden directories apart from System Volume Information, which you can easily skip if you use try...catch in a recursive routine).
    Sunday, October 20, 2019 12:58 PM
    Moderator
  • Thank you for this reply. Unfortunately here my own knowledge ends, but thanks for pointing me out that LINQ can't be used.
    • Edited by EKH10 Sunday, October 20, 2019 2:13 PM
    Sunday, October 20, 2019 2:12 PM
  • The recursive routine that lists the files and skips any folders that produce errors looks more or less like the code below:

    public List<string> GetFilesWithoutErrors(string sourceFolder, string filter)
    {
        var list = new List<string>();
        foreach (string subfolder in Directory.GetDirectories(sourceFolder))
        {
           try
           {
            list.AddRange(GetFilesWithoutErrors(subfolder, filter));
           }
           catch (Exception)
           {
              // We ignore the error here, although you can of course log it instead if you wish
           }
        }
    
        list.AddRange(Directory.GetFiles(sourceFolder, filter));
    }

    Note: This presumes that the starting folder itself does not produce an error; it only skips any subfolders that produce errors.

    Also, it can be optimised by using a single list and passing it to the descendants. However, you would have to initialise it before starting the first recursion, so this code is a bit simpler.

    Sunday, October 20, 2019 3:42 PM
    Moderator
  • Hi EKH10, 

    Thank you for posting here.

    For your question, you get the exception when using Directory.GetFiles() method.

    I find two related references, and you can refer to the suggestions in them.

    1. Issue in accesing the drive using Directory.GetFiles in C#
    2. Access to the path is denied when using Directory.GetFiles(…)

    Hope them can help you.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 22, 2019 9:34 AM
    Moderator