C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,858 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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);
}
Check if this example finds all accessible files:
string rootDirectory = System.IO.DriveInfo.GetDrives( )[0].RootDirectory.FullName;
List<string> files = new List<string>( );
void scan( DirectoryInfo di )
{
try
{
files.AddRange( di.EnumerateFiles( "file.exe" ).Select( f => f.FullName ) );
foreach( var sd in di.EnumerateDirectories( ) )
{
scan( sd );
}
}
catch( UnauthorizedAccessException )
{
Console.WriteLine( "Skipped: {0}", di.FullName );
}
}
scan( new DirectoryInfo( rootDirectory ) );
(If local functions are not available, then move the scan function).
Thank you, Viorel-1.
Best regards.