asp.net 4.5 ejercicio ~ test14-4 mover y copiar archivos

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test14_4.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="源文件路径:"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
        <asp:Button ID="Button1" runat="server" Text="移动" OnClick="Button1_Click" />&nbsp;&nbsp;
        <asp:Button ID="Button2" runat="server" Text="复制" OnClick="Button2_Click" /><br /><br />
        <asp:Label ID="Label2" runat="server" Text="目标文件路径:"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
        <asp:Label ID="Label3" runat="server" Text="执行情况:"></asp:Label><br />
        <asp:Label ID="lblTips" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

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.源文件所在文件夹不存在,新建源文件所在的文件夹。<br>";
                }
                //文件不存在,新建
                if (System.IO.File.Exists(pathSource) == false)
                {
                    System.IO.FileStream fs = System.IO.File.Create(pathSource);
                    fs.Close();
                    resMsg += "2.源文件不存在,新建源文件。<br>";
                }

                //目标文件夹目录
                string directoryNameTarget = System.IO.Path.GetDirectoryName(pathTarget);
                //目标文件目录不存在,新建
                if (!System.IO.Directory.Exists(directoryNameTarget))
                {
                    System.IO.Directory.CreateDirectory(directoryNameTarget);
                    resMsg += "3.目标文件所在文件夹不存在,新建目标文件所在文件夹。<br>";
                }

                //保留,即复制,不保留,即移动
                if (keepSource)
                {
                    System.IO.File.Copy(pathSource, pathTarget, true);
                    resMsg += "5.复制文件。<br>";
                }
                else
                {
                    //移动时,存在文件,先删除,在移动。
                    if (System.IO.File.Exists(pathTarget))
                    {
                        System.IO.File.Delete(pathTarget);
                        resMsg += "4.目标文件存在,删除目标文件。<br>";
                    }
                    System.IO.File.Move(pathSource, pathTarget);
                    resMsg += "6.移动文件。<br>";
                }

                if (System.IO.File.Exists(pathSource))
                {
                    resMsg += "7.源文件存在,复制操作完成。<br>";
                }
                else
                {
                    resMsg += "7.源文件不存在,移动操作完成。<br>";
                }
            }
            catch (Exception error)
            {
                resMsg += "8.程序执行异常,错误:" + error.Message.ToString();
            }

            return resMsg;
        }
    }
}

 

Supongo que te gusta

Origin blog.csdn.net/modern358/article/details/114740517
Recomendado
Clasificación