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; } }}

 

上一篇:asp.net 4.5 练习~test14-5 写文件
下一篇:asp.net 4.5 练习~test14-3 统计目录大小

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年03月28日 20时13分13秒