
asp.net 4.5 练习~test14-4 移动复制文件
发布日期:2021-05-06 21:16:23
浏览次数:15
分类:技术文章
本文共 3960 字,大约阅读时间需要 13 分钟。
webform1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test14_4.WebForm1" %>
webform1.aspx.cs
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace test14_4{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } //移动 protected void Button1_Click(object sender, EventArgs e) { string pathSource = TextBox1.Text.Trim(); string pathTarget = TextBox2.Text.Trim(); try { lblTips.Text = MoveCopyFile(pathSource, pathTarget, false); } catch (Exception ex) { lblTips.Text = ex.Message.ToString(); } } //复制 protected void Button2_Click(object sender, EventArgs e) { string pathSource = TextBox1.Text.Trim(); string pathTarget = TextBox2.Text.Trim(); try { lblTips.Text = MoveCopyFile(pathSource, pathTarget, true); } catch (Exception ex) { lblTips.Text = ex.Message.ToString(); } } //复制移动的方法, private string MoveCopyFile(string pathSource, string pathTarget, bool keepSource) { //返回的提示 string resMsg = ""; //取得服务器根目录 string pathRoot = Server.MapPath(""); //合并路径 pathSource = System.IO.Path.Combine(pathRoot, pathSource); pathTarget = System.IO.Path.Combine(pathRoot, pathTarget); try { //获得源文件目录名称 string directoryName = System.IO.Path.GetDirectoryName(pathSource); //目录不存在,新建 if (System.IO.Directory.Exists(directoryName) == false) { System.IO.Directory.CreateDirectory(directoryName); resMsg += "1.源文件所在文件夹不存在,新建源文件所在的文件夹。"; } //文件不存在,新建 if (System.IO.File.Exists(pathSource) == false) { System.IO.FileStream fs = System.IO.File.Create(pathSource); fs.Close(); resMsg += "2.源文件不存在,新建源文件。"; } //目标文件夹目录 string directoryNameTarget = System.IO.Path.GetDirectoryName(pathTarget); //目标文件目录不存在,新建 if (!System.IO.Directory.Exists(directoryNameTarget)) { System.IO.Directory.CreateDirectory(directoryNameTarget); resMsg += "3.目标文件所在文件夹不存在,新建目标文件所在文件夹。"; } //保留,即复制,不保留,即移动 if (keepSource) { System.IO.File.Copy(pathSource, pathTarget, true); resMsg += "5.复制文件。"; } else { //移动时,存在文件,先删除,在移动。 if (System.IO.File.Exists(pathTarget)) { System.IO.File.Delete(pathTarget); resMsg += "4.目标文件存在,删除目标文件。"; } System.IO.File.Move(pathSource, pathTarget); resMsg += "6.移动文件。"; } if (System.IO.File.Exists(pathSource)) { resMsg += "7.源文件存在,复制操作完成。"; } else { resMsg += "7.源文件不存在,移动操作完成。"; } } catch (Exception error) { resMsg += "8.程序执行异常,错误:" + error.Message.ToString(); } return resMsg; } }}
发表评论
最新留言
关注你微信了!
[***.104.42.241]2025年03月28日 20时13分13秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Mac系统投屏到电视机的方法
2019-03-03
【Docker&ARM】ARM架构服务器上docker的安装
2019-03-03
【Tinyproxy】CentOS7.X http代理tinyproxy的安装配置与使用
2019-03-03
php--自定义错误处理函数的使用方法
2019-03-03
php--异常处理主动抛出异常的使用方法
2019-03-03
php--简易的相册1
2019-03-03
php---xml基础知识
2019-03-03
php--class static
2019-03-03
php--匿名函数的使用
2019-03-03
php--json_decode
2019-03-03
php--class的工厂模式的示例
2019-03-03
php--模拟调用接口的代码
2019-03-03
php教程--案例20(用户登录)
2019-03-03
php教程--案例29(文件上传类)
2019-03-03
jQuery练习t76
2019-03-03
jQuery练习t78
2019-03-03
jQuery练习t79
2019-03-03
jQuery练习t80
2019-03-03
jQuery练习t81
2019-03-03
jQuery中使用animate方法替代其他动画方法
2019-03-03