asp.net 4.5 练习~test14-2 对文件夹操作
发布日期:2021-05-06 21:16:21 浏览次数:32 分类:精选文章

本文共 3287 字,大约阅读时间需要 10 分钟。

文件夹管理工具
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;namespace test14_2{    public partial class WebForm1 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            // 初始化提示信息标签            lblTips.Text = "";        }        // 新建文件夹        protected void Button1_Click(object sender, EventArgs e)        {            try            {                string folderPath = TextBox1.Text;                if (Directory.Exists(folderPath))                {                    lblTips.Text = "此文件夹已经存在,请重新输入!";                    return;                }                Directory.CreateDirectory(folderPath);                lblTips.Text = "文件夹创建成功!";            }            catch (Exception error)            {                lblTips.Text = "创建失败!原因是" + error.Message.ToString();            }        }        // 移动文件夹        protected void Button2_Click(object sender, EventArgs e)        {            try            {                string sourcePath = TextBox2.Text;                string targetPath = TextBox3.Text;                if (!Directory.Exists(sourcePath))                {                    lblTips.Text = "源文件夹不存在,无法移动";                    return;                }                if (Directory.Exists(targetPath))                {                    lblTips.Text = "新文件夹已经存在,无法移动";                    return;                }                Directory.Move(sourcePath, targetPath);                lblTips.Text = "文件夹移动成功!";            }            catch (Exception ex)            {                lblTips.Text = "移动失败!原因是" + ex.Message.ToString();            }        }        // 删除文件夹        protected void Button3_Click(object sender, EventArgs e)        {            try            {                string folderPath = TextBox4.Text;                if (Directory.Exists(folderPath))                {                    if (CheckBox1.Checked)                    {                        // 删除文件夹及其所有内容                        Directory.Delete(folderPath, true);                        lblTips.Text = "文件夹删除成功!";                    }                    else                    {                        // 只删除文件夹本身                        Directory.Delete(folderPath);                        lblTips.Text = "文件夹删除成功!";                    }                }                else                {                    lblTips.Text = "此文件夹不存在!";                }            }            catch (Exception error)            {                lblTips.Text = "删除失败!原因是" + error.Message.ToString();            }        }    }}
上一篇:asp.net 4.5 练习~test14-3 统计目录大小
下一篇:asp.net 4.5 练习~test14-1 对磁盘操作

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年03月25日 15时27分21秒