System.IO.FileExtensions.Rename C# (CSharp) Méthode

Rename() public static méthode

Renames the file to the specified name
public static Rename ( this fileInfo, string newName, bool replace = true ) : bool
fileInfo this
newName string The new name of this file
replace bool Indicates wether to delete the new file path if it exists already
Résultat bool
        public static bool Rename(this FileInfo fileInfo, string newName, bool replace = true)
        {
            // First, make sure the file doesnt already exist
            string newPath = Path.Combine(fileInfo.Directory.FullName, newName);
            if (File.Exists(newPath))
            {
                // If we dont want to replace, return false
                if (!replace)
                    return false;
                else
                    File.Delete(newName);
            }

            // Move file to the new location
            fileInfo.MoveTo(newPath);
            return true;
        }
FileExtensions