45

I find myself doing this a lot just to ensure the filename is not in use. Is there a better way?

Directory.Exists(name) || File.Exists(name)
5
  • 2
    And what is wrong with that? How can it get any easier or cleaner?
    – Ed S.
    Apr 5, 2010 at 5:27
  • I do it everywhere.... its ugly. Usually i dont worry about precedence but it could happen
    – user34537
    Apr 5, 2010 at 6:22
  • Why do you need these 2 checks? Why don't you use just File.Exists(name) ?
    – Egor4eg
    Apr 8, 2013 at 15:07
  • @Egor4eg: three years to late but I imagine I was checking if a filename was free to use or not. Although I normally just do File.Create or whatever, I probably was doing it in UI and wanted to stop the user if the file existed before finishing everything in the UI/doing the ops
    – user34537
    Apr 9, 2013 at 0:31
  • 3
    The filesystem has a tendency to laugh in your face, and make the file in use after you check it. In the general case, the only way to be sure if a filename is not in use is to try to create it, and if it fails, checking why. It's not completely unreasonable to check if it exists first for fail-fast behaviour, but don't count on it being correct.
    – Martijn
    Apr 17, 2015 at 12:46

9 Answers 9

55

Sure :)

internal static bool FileOrDirectoryExists(string name)
{
   return (Directory.Exists(name) || File.Exists(name));
}
7
  • 1
    Extension method for Path class? "Performs operations on String instances that contain file or directory path information."
    – si618
    Apr 3, 2010 at 10:48
  • Si. semi good idea for having it in a string instance. its really to bad .NET doesnt allow extending a static class :(
    – user34537
    Apr 4, 2010 at 23:42
  • anyone notice ;expected error ?.xD 2014ppl from here, but still thanks @PaulG .
    – Elegiac
    Sep 24, 2014 at 5:55
  • @si618, you cannot create extension methods for static classes. Can you?
    – Jordan
    Jul 31, 2015 at 19:45
  • 1
    True, acidzombie and Jordan are correct, you need an instance to write an extension method.
    – si618
    Jul 31, 2015 at 23:45
36

Note that the fact that you are using Exists() to check for file or directory name in use is subject to race conditions.

At any point after your Exists() test has passed, something could have created a file with that name before your code reaches the point where you create a file, for example.

(I'm assuming it is an exceptional condition for the file to already exist).

It is more reliable to simply to open the file, specifying an appropriate FileShare parameter.

Example:

using System;
using System.IO;

static class FileNameInUse
{
    static void Main(string[] args)
    {
        string path = args[0];
        using (var stream = File.Open(path, FileMode.CreateNew, FileAccess.Write, FileShare.None))
        {
            // Write to file
        }
    }
}

So simply handling the IOException on failure may result in simpler code less prone to race conditions, because now:

  • If something else has already created the file, FileMode.CreateNew will cause an IOException to be thrown
  • If your open and create succeeds, because of FileShare.None, no other process can access the file until you close it.

Unfortunately, it is not possible to check whether a file is currently in use, and not throw an exception, without some ugly P/Invoke:

    bool IsFileInUse(string fileName)
    {
            IntPtr hFile = Win32.CreateFile(fileName, Win32.FILE_READ_DATA, 0, IntPtr.Zero, Win32.OPEN_EXISTING, Win32.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
            if (hFile.ToInt32() == Win32.INVALID_HANDLE_VALUE)
                return true;

            Win32.CloseHandle(hFile);
            return false;
    }

    class Win32
    {
        const uint FILE_READ_DATA = 0x0001;
        const uint FILE_SHARE_NONE = 0x00000000;
        const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
        const uint OPEN_EXISTING = 3;
        const int INVALID_HANDLE_VALUE = -1;

        [DllImport("kernel32.dll", SetLastError=true)]
        internal static extern IntPtr CreateFile(string lpFileName,
                                               uint dwDesiredAccess,
                                               uint dwShareMode,
                                               IntPtr lpSecurityAttributes,
                                               uint dwCreationDisposition,
                                               uint dwFlagsAndAttributes,
                                               IntPtr hTemplateFile);

        [DllImport("kernel32.dll")]
        internal static extern bool CloseHandle(IntPtr hObject);
    }

And this fast check is also prone to race conditions, unless you return the file handle from it, and pass that to the relevant FileStream constructor.

5
  • 2
    For some reason people never seem to get recognized for solving the problem, only for answering the question. +1 for answering the question that SHOULD have been asked.
    – Ben Voigt
    Apr 5, 2010 at 5:23
  • 1
    +1. @Ben Voigt: Well theres no way for anyone to know the problem since i specified it BUT my question is WHAT I WANTED TO KNOW. I am actually using .Exist to check if my app created a temp folder (which it searches for a nonexisting folder and creates one then passing the name). I could just do a try{Directory.Delete()}catch but i prefer exist for convince. Under no circumstance should anything other then my app delete the folder. The folder is renamed once succeeded. So Leon actually didnt solve my problem.
    – user34537
    Apr 5, 2010 at 6:28
  • 1
    @Ben Voigt: actually i was wrong. In that case i use Directory.delete but when i create the directory i use that line to check if it exist then i use either Directory.CreateDirectory or file.open with the return filename. I use the function to generate the unique name in a specific format.
    – user34537
    Apr 5, 2010 at 6:36
  • 1
    @Ben: Thanks for the kind words, but I was just taking a guess. I've been bitten in the past by this type of thing, and thought I recognized a potential problem :) Apr 5, 2010 at 7:40
  • 3
    +1 @Leon - For "At any point after your Exists() test has passed, something could have created a file with that name before your code reaches the point where you create a file, for example.." Apr 5, 2010 at 11:53
4

I think that's the only way. I generally have a "FileManager" class which have static methods encapsulating I/O methods including the ones you indicated and then use that "FileManager" across all the applications as a library.

3

My way of checking this is using the FileSystemInfo, here is my code:

FileSystemInfo info = 
  File.GetAttributes(data.Path).HasFlag(FileAttributes.Directory) ? 
    new DirectoryInfo(data.Path) : (FileSystemInfo)new FileInfo(data.Path);

return info.Exists;
2
  • 2
    File.GetAttributes throws a System.IO.FileNotFoundException if neither a file nor directory exists Jul 30, 2016 at 14:04
  • Yep, does not work with FileSystemWatcher, because the path could be a deleted file
    – Ini
    Aug 21, 2019 at 21:46
3

You can use following function:

[DllImport("shlwapi", EntryPoint = "PathFileExists", CharSet = CharSet.Unicode)]
public static extern bool PathExists(string path);
1
  • It is useful when need to check presence of FS entry while working with P/Invoke, thanks!
    – n0ne
    Jun 24, 2021 at 9:10
0

Check if a directory Exists

string root = @"C:\Temp";        
// If directory does not exist, don't even try   
if (Directory.Exists(root))  
{  
    Directory.Delete(root);  
}  

Use the File.exists method in C# to check if a file exits in C# or not. Firstly, check whether the file is present in the current directory.

if (File.Exists("MyFile.txt")) {
   Console.WriteLine("The file exists.");
}

After that check whether the file exist in a directory or not

if (File.Exists(@"D:\myfile.txt")) {
   Console.WriteLine("The file exists.");
}
-1

Another way to check if file exist.

FileInfo file = new FileInfo("file.txt");

if (file.Exists)
{
    // TO DO
}
5
  • Look at the tags before you post an answer.
    – lsalamon
    Apr 3, 2010 at 16:25
  • 5
    Tag is C# , .NET and file. This is C# code and it can yield desired output.
    – Anonymous
    Apr 4, 2010 at 4:40
  • 1
    +1 to even this out. weird that anyone would downvote :|. actually, not to even out. i would upvote this anyways. interesting and good answer.
    – user34537
    Apr 4, 2010 at 23:40
  • 25
    The down vote is a necessity as this is a wrong answer. A fileinfo Exists call returns false on all directories even if they exist. From the FileInfo.Exists page: true if the file exists; false if the file does not exist or if the file is a directory
    – Daniel
    Mar 21, 2013 at 21:41
  • Instead file name "file.txt" must be file path: msdn.microsoft.com/en-us/library/… Dec 4, 2017 at 12:02
-1

How about checking whether FileAttributes == -1?

public static bool PathExists(this string path) {
    DirectoryInfo dirInfo = null;
    try { dirInfo = new DirectoryInfo(path.TrimEnd(Path.DirectorySeparatorChar)); }
    catch { }
    if (dirInfo == null || dirInfo.Attributes == (FileAttributes)(-1))
        return false;
    return true;
}
-3
bool FileOrDirectoryExists(string path)
{
    try
    {
        File.GetAttributes(_source);
    }
    catch (FileNotFoundException)
    {
        return false;
    }
    return true;
}
1
  • 2
    The point of testing to see if a path and file exist is typically to avoid an exception. If you're going to rely on the error handler, why not just omit the checks completely?
    – Comintern
    Sep 3, 2018 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy