快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

文件操作之移动文件

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();
本文题目:文件操作之移动文件
URL分享:http://6mz.cn/article/sspjgj.html

其他资讯