文件操作之移动文件

File类的Move方法可以移动文件,使用格式如下:

创新互联专注于农安企业网站建设,自适应网站建设,购物商城网站建设。农安网站建设公司,为农安等地区提供建站服务。全流程按需制作网站,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务


Move(string sourceFileName,string desfFileName)//第一个参数是源文件,第二个参数是目标文件
移动文件时可能存在下面三种情况
1)、源文件存在,目标文件不存在。
2)、源文件存在、目标文件存在。
3)、源文件不存在。
代码如下:
string pathSource = @"C:UsersAdministratorDesktopSourcesteng.cn.txt";
string pathDestination = @"C:UsersAdministratorDesktopDestinationcopy.txt";
if (File.Exists(pathSource))//源文件存在
{
try
{
if (File.Exists(pathDestination))//目标文件已存在
{
Console.WriteLine("文件移动失败,该处有同名文件");
}
else
{
File.Move(pathSource, pathDestination);
Console.WriteLine("文件移动成功");
}

}
catch(Exception ex)
{
Console.WriteLine("移动文件出错,原因是:" + ex.Message.ToString());
}
}
else
{
Console.WriteLine("源文件不存在,无法移动!");
}
Console.ReadKey();
分享名称:文件操作之移动文件
链接地址:http://lszwz.com/article/sspjgj.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款