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:

<!-- language: c# -->

    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:

<!-- language: c# -->

    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:

<!-- language: c# -->

    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;
            }
        }
    }

