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