.NET - Generate next file name with pattern

获取匹配指定模式的下一个文件名,如果该模式为/var/log/auth.log,该文件夹下存在auth1.log,那么下一个文件名是auth2.log

public static string GetNextFileName(string pattern)
{
    if (Path.GetFileName(pattern).IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) {
        throw new ArgumentException();
    }
    else {
        var dir = Path.GetDirectoryName(pattern);
        var name = Path.GetFileNameWithoutExtension(pattern);
        var ext = Path.GetExtension(pattern);
        int index = 1;
        string path;
        do { path = Path.Combine(dir, $"{name}{index++}{ext}"); }
        while (File.Exists(path));
        return path;
    }
}