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

ken76

Member
Joined
Nov 15, 2018
Messages
7
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:

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
1,031
Location
Virginia Beach, VA
Programming Experience
10+
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().
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
952
Location
UK
Programming Experience
10+
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.
 

ken76

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

Skydiver

Well-known member
Joined
Apr 6, 2019
Messages
1,031
Location
Virginia Beach, VA
Programming Experience
10+
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.
 

Sheepings

Senior Programmer
Joined
Sep 5, 2018
Messages
952
Location
UK
Programming Experience
10+

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,730
Location
Sydney, Australia
Programming Experience
10+
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.
 
Top Bottom