c# check if directory contains file code example
Example 1: c# check if a file exists in a folder
using System;
using System.IO;
public class Example
{
public static void Main()
{
string path = @"C:\path\to\some\dir";
string filename = "somefile.ext";
if (File.Exists(path + @"\" + filename))
{
Console.WriteLine("File found in the specified directory!");
}
else
{
Console.WriteLine("File does not exist in the specified directory!");
}
}
}
Example 2: c# check if string is path or file
FileAttributes attr = File.GetAttributes(@"c:\Temp");
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
MessageBox.Show("Its a directory");
else
MessageBox.Show("Its a file");