简体   繁体   English

目录检查不起作用

[英]Directory Check Won't Work

I tried creating a program that tells you if a directory exists or not, but no matter what I input, it always comes up as not existing. 我尝试创建一个程序来告诉您目录是否存在,但是无论我输入什么内容,它始终会以不存在的形式出现。

My Code: 我的代码:

using System;
using System.IO;

class TestFileAndDirectory
{
    public static void Main()
    {
        string input;
        input = Console.ReadLine();
        if ( Directory.Exists(input))
        {
            Console.WriteLine("Exists");
        }
        else
        {
            Console.WriteLine("Doesn't Exist");
        }
        Console.ReadLine();
    }
}

At first I just thought maybe it was my logic, so I tried this code from the book: Microsoft Visual C# 2010: Comprehensive Ch.14: 起初我只是以为这可能是我的逻辑,所以我尝试了本书中的以下代码:Microsoft Visual C#2010:全面的第14章:

using System;
using System.IO;
public class DirectoryInformation
{
   public static void Main()
   {
      string directoryName;
      string[] listOfFiles;
      Console.Write("Enter a folder >> ");
      directoryName = Console.ReadLine();
      if(Directory.Exists(directoryName))
      {
         Console.WriteLine("Directory exists, " +
            "and it contains the following:");
         listOfFiles = Directory.GetFiles(directoryName);
         for(int x = 0; x < listOfFiles.Length; ++x)
           Console.WriteLine("   {0}", listOfFiles[x]);
      }
      else
      {
         Console.WriteLine("Directory does not exist");
      }
   }
}

When I tried this code it did not work either not even if I put it into the same base folder as the directory I'm trying to find. 当我尝试使用此代码时,即使将其放置在与我要查找的目录相同的基本文件夹中,它也不起作用。

Path in question: C:\\C#\\Chapter.14\\Cat Haikus 问题路径:C:\\ C#\\ Chapter.14 \\ Cat Haikus

Path of Program: C:\\C#\\Chapter.14\\TestFilesAndDirectories.cs 程序路径:C:\\ C#\\ Chapter.14 \\ TestFilesAndDirectories.cs

The path parameter is permitted to specify relative or absolute path information. 允许path参数指定相对或绝对路径信息。 Relative path information is interpreted as relative to the current working directory. 相对路径信息被解释为相对于当前工作目录。

Source: https://msdn.microsoft.com/en-us/library/system.io.directory.exists(v=vs.110).aspx 来源: https : //msdn.microsoft.com/zh-cn/library/system.io.directory.exists(v=vs.110).aspx

If your input string is only a folder name like "Chapter. 14" (relative path), then this folder must exist in the path of your executable file. 如果您的input字符串只是一个文件夹名称,例如“ Chapter。14”(相对路径),则此文件夹必须存在于可执行文件的路径中。 Like PathOfTheExecutableFile\\Chapter. 14 就像PathOfTheExecutableFile\\Chapter. 14 PathOfTheExecutableFile\\Chapter. 14 . PathOfTheExecutableFile\\Chapter. 14

If the folder is in a completely different place, use absolute paths. 如果文件夹位于完全不同的位置,请使用绝对路径。 Like C:\\Users\\theuser\\Desktop\\Chapter. 14 C:\\Users\\theuser\\Desktop\\Chapter. 14 C:\\Users\\theuser\\Desktop\\Chapter. 14 . C:\\Users\\theuser\\Desktop\\Chapter. 14

Update: 更新:

Since you want to check C:\\C#\\Chapter.14\\Cat Haikus folder, you could check if it exists using 由于要检查C:\\C#\\Chapter.14\\Cat Haikus文件夹,因此可以使用以下命令检查它是否存在

if (Directory.Exists(@"C:\C#\Chapter.14\Cat Haikus")){
    Console.WriteLine("Exists");
}

I don't know your exact folder tree structure, but if your executable file is in a subfolder of C:\\C#\\Chapter.14\\ , you could also use Directoy.GetParent() method. 我不知道您的确切文件夹树结构,但是如果您的可执行文件在C:\\C#\\Chapter.14\\的子文件夹中,则也可以使用Directoy.GetParent()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM