[英]Check if file or folder by given path exists
我讓用戶將路徑作為字符串傳遞。
路徑可能像
C:\\someFolder
C:\\someFolder\\someFile
C:\\someFolder\\someFile.jpg
我要檢查給定的路徑是文件還是文件夾,如果是文件,則要檢查它是否實際退出。
我一直在使用FileAttributes fileRoot = File.GetAttributes(@path);
檢查它是文件還是文件夾,但是不能正常工作。
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
變量,確定它是目錄還是文件,然后檢查是否存在。 只要確保您處理FileAttributes attributes = File.GetAttributes(path);
行,例如將其放在try / catch塊中,因為如果文件或文件夾不存在,它將引發異常。
您可以使用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);
}
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();
}
您能否澄清以下聲明:
但它不能正常工作
在什么情況下“不適當”?
關於問題:
您的任務是否需要知道其文件或目錄?
如果沒有(例如,如果文件存在,則只想具有“ 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
您可以在下面使用此代碼進行檢查:
// 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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.