为什么我不能对文件夹实施布尔检查?

Why can't I implement a boolean check for a folder?

我正在尝试检查特定文件夹是否存在。例如,检查名为“下载”的文件夹是否存在。我正在使用 C# Windows Forms 应用编写,这是代码:

      //key = Folder Name
     {
      var pathtu = Convert.ToBoolean(Directory.EnumerateDirectories(main,key,SearchOption.AllDirectories));
      if (pathtu == true)
       {
         path[0] = @"c:\Users\"+key;
         File.Move(file.SelectedPath, path[0]);
         label2.Visible = true;
       }

当我 运行 此代码时,出现以下错误:

System.InvalidCastException: 'Unable to cast object of type 'System.IO.FileSystemEnumerableIterator`1[System.String]' to type 'System.IConvertible'.'

我做错了什么?

使用

Directory.Exists(pathtodir)

它 returns 一个布尔值。

https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.exists