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 中)
猿导航
阅读排行榜
- 1vue和el-table使用经验-如何刷新表格数据10956
- 2three.js加载3D瓦片和3dtiles数据生成交互式地图的开源项目9505
- 3Microsoft Visual C++ Redistributable是什么,有什么作用?7282
- 4mybatis使用经验——mybatis-spring-boot-starter和mybatis的版本对应关系表(持续更新~)5862
- 5uni-app使用经验—vue页面和html页面如何互相调用接口并传参5486
- 6Intellij IDEA下的版本控制VCS的启用与关闭4918
- 7Spring学习经验—@ResponseBody注解的使用说明4901
- 8Druid异常解决经验—java.sql.SQLException url not set4520
- 9如何用批处理命令(bat脚本)启动和停止windows服务4289
- 10nuxt.js项目中如何添加和使用全局变量4164
- 11解决SpringBoot使用maven下载不了jar包的问题3502
- 12linux中解压tar.gz文件报错“gzip: stdin: invalid compressed data--format violated”3401
- 13nuxtjs asyncData使用经验—如何发起多个axios请求并携带参数3248
- 14在Nuxt.js项目的head中引用外部js文件3093
- 15在NVIDIA控制面板设置参数时提示“拒绝访问 无法应用选定的设置到您的系统”的解决方法之一3071