Dependencies/XamlFileSystemExplorer/FSExplorer/FileSystemInfoEx/Tools/IOTools.cs
/// <summary>
/// Rename a file or folder in an directory.
/// </summary>
Rename a file or folder in an directory.
Rename a file or folder in an directory .
public static void Rename(string source, string destName)
        {
            FileSystemInfoEx srcElement = new FileSystemInfoEx(source);
            string srcName = Path.GetFileName(source);
            if (!srcElement.Exists || srcElement.Parent == null)
                throw new IOException("Source not exists");
            using (ShellFolder2 srcParentShellFolder = srcElement.Parent.ShellFolder)
            {
                if (srcParentShellFolder == null)
                    throw new IOException("Source directory does not support IShellFolder");
                int hr = srcElement.RequestRelativePIDL(srcRelPidl =>
                    {
                        IntPtr tmpPtr;
                        int retVal = srcParentShellFolder.SetNameOf(IntPtr.Zero, srcRelPidl.Ptr,
                            destName, ShellAPI.SHGNO.FORPARSING, out tmpPtr);
                        PIDL tmpPIDL = new PIDL(tmpPtr, false); 
                        tmpPIDL.Free();
                        return retVal;
                    });
                if (hr != ShellAPI.S_OK)
                    Marshal.ThrowExceptionForHR(hr);
            }
        }
public static void Rename ( string source , string destName ) { FileSystemInfoEx srcElement = new FileSystemInfoEx ( source ) ; string srcName = Path . GetFileName ( source ) ; if ( ! srcElement . Exists || srcElement . Parent == null ) throw new IOException ( " " ) ; using ( ShellFolder2 srcParentShellFolder = srcElement . Parent . ShellFolder ) { if ( srcParentShellFolder == null ) throw new IOException ( " " ) ; int hr = srcElement . RequestRelativePIDL ( srcRelPidl => { IntPtr tmpPtr ; int retVal = srcParentShellFolder . SetNameOf ( IntPtr . Zero , srcRelPidl . Ptr , destName , ShellAPI . SHGNO . FORPARSING , out tmpPtr ) ; PIDL tmpPIDL = new PIDL ( tmpPtr , false ) ; tmpPIDL . Free ( ) ; return retVal ; } ) ; if ( hr != ShellAPI . S_OK ) Marshal . ThrowExceptionForHR ( hr ) ; } }
Rename a file or folder in an directory.
Rename a file or folder in an directory .
//DirectoryInfoEx srcDir = new DirectoryInfoEx(Path.GetDirectoryName(source)); //0.15: Fixed ShellFolder not freed. //consume the IntPtr, and free it.
source
string
destName
string