Joe Amenta
Joe Amenta

Reputation: 5241

How do I try to create a file, and return a value indicating whether it was created or not?

I'm looking for something similar to what would have a signature like this:

static bool TryCreateFile(string path);

This needs to avoid potential race conditions across threads, processes, and even other machines accessing the same filesystem, without requiring the current user to have any more permissions than they would need for File.Create. Currently, I have the following code, which I don't particularly like:

static bool TryCreateFile(string path)
{
    try
    {
        // If we were able to successfully create the file,
        // return true and close it.
        using (File.Open(path, FileMode.CreateNew))
        {
            return true;
        }
    }
    catch (IOException)
    {
        // We want to rethrow the exception if the File.Open call failed
        // for a reason other than that it already existed.
        if (!File.Exists(path))
        {
            throw;
        }
    }

    return false;
}

Is there another way to do this that I'm missing?

This fits into the following helper method, designed to create the "next" sequential empty file for the directory and return its path, again avoiding potential race conditions across threads, processes, and even other machines accessing the same filesystem. So I guess a valid solution could involve a different approach to this:

static string GetNextFileName(string directoryPath)
{
    while (true)
    {
        IEnumerable<int?> fileNumbers = Directory.EnumerateFiles(directoryPath)
                                                 .Select(int.Parse)
                                                 .Cast<int?>();
        int nextNumber = (fileNumbers.Max() ?? 0) + 1;
        string fileName = Path.Combine(directoryPath, nextNumber.ToString());
        if (TryCreateFile(fileName))
        {
            return fileName;
        }
    }
}

Edit1: We can assume that files will not be deleted from the directory while this code is executing.

Upvotes: 5

Views: 507

Answers (3)

ken2k
ken2k

Reputation: 48985

This is actually a truly complicated issue.

It's pretty hard to have a true thread-safe method. In your TryCreateFile, imagine someone removes the file from another process just after its creation, before you tested File.Exists? Your code would throw an exception.

If your primary goal is

to create the "next" sequential empty file for the directory and return its path

, I wouldn't try to test if a file exists. I would assume a file with a GUID as name is always unique:

private static void Main(string[] args)
{
    var z = GetNextFileName(@"c:\temp");

    Console.ReadLine();
}

public static string GetNextFileName(string directoryPath)
{
    // Gets file name
    string fileName = Guid.NewGuid().ToString();
    string filePath = Path.Combine(directoryPath, fileName);

    // Creates an empty file
    using (var z = File.Open(filePath, FileMode.CreateNew))
    {
    }

    return filePath;
}

EDIT: For people who are asking if a GUID is truly unique, see Is a GUID unique 100% of the time?

Upvotes: 0

Henk Holterman
Henk Holterman

Reputation: 273314

No, there is no direct way and no way to avoid the exception handling.

Even when you try to open an existing file, like

if (File.Exists(fName))
   var s = File.OpenRead(fname);

you can still get all kinds of exceptions, including FileNotFound.

This is because of all the reasons you mentioned:

across threads, processes, and even other machines

But you may want to take a look at System.IO.Path.GetRandomFileName(). I think his i based on a WinAPI function that lets you specify a path etc.

Upvotes: 3

Mitja Bonca
Mitja Bonca

Reputation: 4546

Try it this way:

    private bool TryCreateFile(string path)
    {
        try
        {
            FileStream fs = File.Create(path);
            fs.Close();
            return true;
        }
        catch 
        {
            return false;
        }
    }

There is actually no batter way of checking if file has been created or not. There is none any inbuid function of any kind.

Upvotes: 1

Related Questions