我想在扫描后用C#创建一个程序来扫描文件夹我想查看所选根文件夹中有多少个文件和文件夹。但是,如果我采取像C:\或程序文件orso文件夹我得到访问错误,像文件夹c:\ $ Recycle.Bin \ S-1-5-18访问被拒绝,当我想要扫描文件夹C:\时,我在我的桌面上创建一个包含一些文件和文件夹的文件夹,然后我没有收到任何错误,并且可以正常运行程序。
我使用的代码就像这样
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");
}
}