'Access to the path 'F:\System Volume Information' is denied.'

ken76

Member
Joined
Nov 15, 2018
Messages
23
Programming Experience
5-10
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'?

C#:
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);
                      }
                  });
 
Last edited by a moderator:
You obviously know about if statements. What's to prevent you from simply adding another condition to check for that name and skipping it?

Or if you are a believer in LINQ, then you can use the Where() extension method to filter away that specific name before passing on to ForEach().
 
Please do not bold your text like that. Instead, click the three dots with the drop down arrow and click code, paste your code into the popup box and post. Bolding the text like that is an eyesore.
 
I have also tried to use LINQ like this but this line does work.

string[] originalFiles = Directory.GetFiles(sourceFolder, "*", SearchOption.AllDirectories).Where(f => !f.Contains("System Volume Information").ToString())
Can someone help me?
 
Last edited:
f.Contains() returns a boolean, but calling ToString() on that boolean will convert it to a string. The Where() extension method expects to get back a boolean.
 
That folder is always going to be inaccessible. If you use a method that searches every subfolder from the top level then you'll always encounter that issue. You can use such methods at a lower level, e.g. a Documents folder, but not from the drive root folder. If you want to be able to search an entire drive then you need to write your own recursive method that will catch exceptions on inaccessible folders and carry on. There should be loads around. I've written such code myself many times for the same reason.
 

Latest posts

Back
Top Bottom