C#中通过System.IO.Directory.Move 方法将一个文件夹包括其中的所有文件移动到另一个文件夹中

分类:计算机 | C# | 文件和流 1210
更新:2021-08-06 00:10:38
编辑

方法描述

将文件或目录及其中的所有内容移到新位置

语法定义

public static void Move(
    string sourceDirName,
    string destDirName
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
sourceDirName System-String 要移动的文件或目录的路径
destDirName System-String 指向 sourceDirName 的新位置的路径。如果 sourceDirName 是一个文件,则 destDirName 也必须是一个文件名。
返回值 void

注意事项

举例来讲,如果您尝试将 c:\mydir 移到 c:\public,并且 c:\public 已存在,则此方法引发 IOException。 您必须将“c:\public\mydir”指定为 destDirName 参数(假设“c:\public”下不存在“mydir”),或者指定一个新的目录名,例如“c:\newdir”。

允许 sourceDirName 和 destDirName 参数指定相对或绝对路径信息。 相对路径信息被解释为相对于当前工作目录。 若要获取当前工作目录,请参见 GetCurrentDirectory。

System.IO.Directory.Move使用示例

将该示例配置为捕捉此方法的所有常见错误。

using System;

namespace GetFileSystemEntries
{
    class Class1 
    {
        static void Main(string[] args) 
        {
            Class1 snippets = new Class1();

            string path = System.IO.Directory.GetCurrentDirectory();
            string filter = "*.exe";

            snippets.PrintFileSystemEntries(path);
            snippets.PrintFileSystemEntries(path, filter);        
            snippets.GetLogicalDrives();
            snippets.GetParent(path);
            snippets.Move("C:\\proof", "C:\\Temp");
        }


        void PrintFileSystemEntries(string path) 
        {

            try 
            {
                // Obtain the file system entries in the directory path.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path); 

                foreach (string str in directoryEntries) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException) 
            {
                System.Console.WriteLine("The path encapsulated in the " + 
                    "Directory object does not exist.");
            }
        }
        void PrintFileSystemEntries(string path, string pattern) 
        {
            try 
            {
                // Obtain the file system entries in the directory
                // path that match the pattern.
                string[] directoryEntries =
                    System.IO.Directory.GetFileSystemEntries(path, pattern); 

                foreach (string str in directoryEntries) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");
            }
            catch (System.IO.DirectoryNotFoundException) 
            {
                System.Console.WriteLine("The path encapsulated in the " + 
                    "Directory object does not exist.");
            }
        }

        // Print out all logical drives on the system.
        void GetLogicalDrives() 
        {
            try 
            {
                string[] drives = System.IO.Directory.GetLogicalDrives();

                foreach (string str in drives) 
                {
                    System.Console.WriteLine(str);
                }
            }
            catch (System.IO.IOException) 
            {
                System.Console.WriteLine("An I/O error occurs.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
        }
        void GetParent(string path) 
        {
            try 
            {
                System.IO.DirectoryInfo directoryInfo =
                    System.IO.Directory.GetParent(path);

                System.Console.WriteLine(directoryInfo.FullName);
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, or " +
                    "contains invalid characters.");
            }
        }
        void Move(string sourcePath, string destinationPath) 
        {
            try 
            {
                System.IO.Directory.Move(sourcePath, destinationPath);
                System.Console.WriteLine("The directory move is complete.");
            }
            catch (ArgumentNullException) 
            {
                System.Console.WriteLine("Path is a null reference.");
            }
            catch (System.Security.SecurityException) 
            {
                System.Console.WriteLine("The caller does not have the " +
                    "required permission.");
            }
            catch (ArgumentException) 
            {
                System.Console.WriteLine("Path is an empty string, " +
                    "contains only white spaces, " + 
                    "or contains invalid characters.");    
            }
            catch (System.IO.IOException) 
            {
                System.Console.WriteLine("An attempt was made to move a " +
                    "directory to a different " +
                    "volume, or destDirName " +
                    "already exists."); 
            }
        }
    }
}

异常

异常 异常描述
IOException 尝试将一个目录移到不同的卷。destDirName 已存在;sourceDirName 和 destDirName 参数引用相同的文件或录;
UnauthorizedAccessException 调用方没有所要求的权限。
ArgumentException sourceDirName 或 destDirName 是一个零长度字符串,仅包含空白或者包含一个或多个由 InvalidPathChars 定义的无效字符。
ArgumentNullException sourceDirName 或 destDirName 为 null。
PathTooLongException 指定的路径、文件名或者两者都超出了系统定义的最大长度。 例如,在基于 Windows 的平台上,路径必须小于 248 个字符,文件名必须小于 260 个字符。
DirectoryNotFoundException 由 sourceDirName 指定的路径无效(例如,它位于未映射的驱动器上)。

命名空间

namespace:System.IO

程序集:mscorlib(在 mscorlib.dll 中)