检查给定路径是否存在文件或文件夹

时间:2014-10-09 05:21:24

标签: c# .net visual-studio

我让用户将路径作为字符串传递。

路径可能类似于

C:\someFolder

C:\someFolder\someFile

C:\someFolder\someFile.jpg

我想检查给定路径是文件还是文件夹,如果是文件,我想检查它是否实际退出。

我一直在使用FileAttributes fileRoot = File.GetAttributes(@path);来检查它是文件还是文件夹,但它无法正常工作。

5 个答案:

答案 0 :(得分:3)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\";
            FileAttributes attributes = File.GetAttributes(path);

            switch (attributes)
            {
                case FileAttributes.Directory:
                    if (Directory.Exists(path))
                        Console.WriteLine("This directory exists.");
                    else
                        Console.WriteLine("This directory does not exist.");
                    break;
                default:
                    if (File.Exists(path))
                        Console.WriteLine("This file exists.");
                    else
                        Console.WriteLine("This file does not exist.");
                    break;
            }
        }
    }
}

这是我为你写的一份工作样本。它获取path变量,确定它是dir还是文件,然后检查它是否存在。只需确保正确处理FileAttributes attributes = File.GetAttributes(path);行,例如将其放在try / catch块中,因为如果文件或文件夹不存在,则会抛出异常。

答案 1 :(得分:2)

您可以使用File.Exists检查文件是否存在。

您可以使用Directory.Exists检查文件夹是否存在

然后你可以用它来检查它是文件还是文件夹

private bool CheckIfExists(string path)
{
    // get the file attributes for file or directory
    FileAttributes attr = File.GetAttributes(path);

    //detect whether its a directory or file
    if((attr & FileAttributes.Directory) == FileAttributes.Directory)
        return Directory.Exists(path);
    else
        return File.Exists(path);
}

答案 2 :(得分:2)

    static void Main(string[] args)
    {
        string Path = @"C:\Abhishek\Documents";
        string filePath = @"C:\Abhishek\Documents.txt";
        bool isDirExists = Directory.Exists(Path);
        bool isFileExists = File.Exists(filePath);

        if (isDirExists)
        {
            Console.WriteLine("Directory Exists");
        }
        else {
            Console.WriteLine("Directory does not exists");
        }
        if (isFileExists)
        {
            Console.WriteLine("File Exists");
        }
        else
        {
            Console.WriteLine("File does not exists");
        }
        Console.ReadKey();
    }

答案 3 :(得分:0)

请您澄清一下这句话:

  

但它没有正常工作

"不正确"

的情况如何?

关于问题:

您的任务是否需要知道其文件或目录?

如果没有(即你只想拥有" true"如果文件存在),你可以使用File.Exists并获得所需的结果。 如果你担心,不会抛出异常。

var filePath = @"d:\Storage\Repo\Detrack\Detrack.bak";
var dirPath = @"d:\Storage\Repo\Detrack\";
var dirPathWithoutTrailingSlash = @"d:\Storage\Repo\Detrack";

Console.WriteLine("Path ({0}) exists = {1}", filePath, new FileInfo(filePath).Exists);
Console.WriteLine("Path ({0}) exists = {1}", dirPath, new FileInfo(dirPath).Exists);
Console.WriteLine("Path ({0}) exists = {1}", dirPathWithoutTrailingSlash, new FileInfo(dirPathWithoutTrailingSlash).Exists);
Console.ReadLine();

结果是:

Path (d:\Storage\Repo\Detrack\Detrack.bak) exists = True
Path (d:\Storage\Repo\Detrack\) exists = False
Path (d:\Storage\Repo\Detrack) exists = False

答案 4 :(得分:0)

您可以使用以下代码进行检查:

// get the file attributes for file or directory
        FileAttributes attr = File.GetAttributes(@"c:\Temp");

        //detect whether its a directory or file
        if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
        {
            // Its a directory
            // Do something here
        }
        else
        {
            // Its a file
            // Do something here
        }