Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 878
Joe Amenta Avatar asked Oct 07 '22 03:10

Joe Amenta


1 Answers

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.

like image 90
Henk Holterman Avatar answered Oct 13 '22 00:10

Henk Holterman