Rename File Extension : File Command « File Stream « C# / C Sharp






Rename File Extension

   
using System;
using System.Diagnostics;
using System.IO;

public static class FileInfoExt
{
    public static bool RenameFileExtension(this FileInfo iFileInfo, string iNewFileName)//test it
    {
        if (iFileInfo != null && string.IsNullOrEmpty(iNewFileName) == false)
        {
            if (iFileInfo.Exists)
            {
                string vNewExtension = iNewFileName;
                if (iNewFileName[0] == '.')
                {
                    vNewExtension = iNewFileName.Substring(1);
                }
                string vNewName = iFileInfo.Name.Replace(iFileInfo.Extension, "." + vNewExtension);
                string vNewFullName = iFileInfo.DirectoryName + vNewName;
                try
                {
                    iFileInfo.MoveTo(vNewFullName);
                    if (File.Exists(vNewFullName))
                    {
                        return true;
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        return false;
    }
}

   
    
    
  








Related examples in the same category

1.C# Implementation of the File Copy Command
2.Use the FileInfo Class to Delete Files with Ease
3.File Move Implementation
4.Rename File Name
5.Backup File
6.Copy directory
7.Find string in a file
8.Converts a number of bytes to a shorter, more readable string representation
9.Compares the specified file1. This method accepts two strings the represent two files to compare.
10.File Renamer
11.Format File Size
12.Is File Existed
13.Is File readable or writeable
14.Replace in file
15.Send a file to the recycle bin
16.Copy file and folder
17.Gets last write time for a file.
18.Execute File
19.Gets the file size text.
20.Get File Bytes And Readable Size Information
21.Copy folder
22.Empty a Folder