简体   繁体   中英

Rename Directory With SubFolders and Files

How can I rename a directory which has many folders and files underneath it? Directory.Move() keeps throwing errors and I read that you cannot use it on non-empty Directories.

You could always execute the REN command line method:

REN Dir1 Dir2

You could also try:

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("Dir1"));
di.MoveTo(Server.MapPath("Dir2"));

Here is a rough C# version of using the REN method:

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.UseShellExecute = false;
        startInfo.RedirectStandardInput = true;
        startInfo.RedirectStandardOutput = true;

        startInfo.FileName = "cmd.exe";
        startInfo.WorkingDirectory = "D:\\Temp";
        Process oProcess = Process.Start(startInfo);

        oProcess.StandardInput.WriteLine(@"Echo on");
        oProcess.StandardInput.WriteLine(@"ren ~test2 ~test1");
        oProcess.StandardInput.WriteLine(@"EXIT");
        string output = oProcess.StandardOutput.ReadToEnd();

        oProcess.WaitForExit();

        Console.Write(output);

        oProcess.Close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM