I want make a program with C# for scan a folder after the scan i want see how many files and folders in the selected rootfolder. But if i take a folder like C:\ or program files orso i get a access error, Like access denied for folder c:\$Recycle.Bin\S-1-5-18 when i want scan folder C:\ when i create a folder on my desktop with some files and folders then i get no error and works the program fine.
The code that i used is like this
public partial class MainWindow : Window
{
System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
}
public MainWindow()
{
InitializeComponent();
}
private void btnScan_Click(object sender, RoutedEventArgs e)
{
if (Directory.Exists(TbLocatie.Text))
{
// @"C:\Users\Alexander\Desktop\test"
DirectoryInfo di = new DirectoryInfo(TbLocatie.Text);
System.IO.FileInfo[] files = di.GetFiles("*.*", SearchOption.AllDirectories);
int countfiles = files.Length;
lblFiles.Content = countfiles;
System.IO.DirectoryInfo[] bestanden = di.GetDirectories("*.*", SearchOption.AllDirectories);
int countbestanden = bestanden.Length;
lblBestanden.Content = countbestanden;
FileSystemInfo[] filelist = di.GetFileSystemInfos();
FileInfo[] fileInfo;
fileInfo = di.GetFiles("*", SearchOption.AllDirectories);
FileSizeFormatProvider sizeconv = new FileSizeFormatProvider();
long size = Convert.ToInt64(fileInfo.Length);
// double sizeMB = Convert.ToDouble(lblGroote.Content);
for (int i = 0; i < fileInfo.Length; i++)
{
size += fileInfo[i].Length;
lblGroote.Content = size;
}
}
else
{
System.Windows.MessageBox.Show("niet gevonden");
}
}