【问题标题】:Why can't I implement a boolean check for a folder? [closed]为什么我不能对文件夹进行布尔检查? [关闭]
【发布时间】:2021-08-11 10:00:47
【问题描述】:

我正在尝试检查特定文件夹是否存在。例如,检查名为“下载”的文件夹是否存在。我正在使用 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:'无法将'System.IO.FileSystemEnumerableIterator`1[System.String]'类型的对象转换为'System.IConvertible'类型。'

我做错了什么?

【问题讨论】:

  • “检查文件夹是否存在” - 不。您的代码中没有任何地方这样做,或者任何接近的地方。你试过Directory.Exists()吗?
  • 您正在尝试使用 Convert.ToBoolean() 将目录迭代器转换为布尔值。那总是会失败的。

标签: c# type-conversion boolean windows-forms-designer


【解决方案1】:

使用

Directory.Exists(pathtodir)

它返回一个布尔值。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    • 2020-01-01
    • 2013-03-10
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多