文件夹类Directory的常用方法

```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace FileOptionApplication
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        private static string directory_path = "c:\\qs250";
        private static string directory_otherpath = "c:\\qqqq";
        /// <summary>
        /// 删除目录鼠标单击事件
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.CreateDirectory(directory_path);
                button2.Enabled = true;
                button1.Enabled = false;
                button3.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                MessageBox.Show("文件夹成功建立。", "警报");
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
        /// <summary>
        /// 删除目录鼠标单击事件
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Delete(directory_path);
                button2.Enabled = false;
                button1.Enabled = true;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
                MessageBox.Show("文件夹删除建立。", "警报");
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
        /// <summary>
        /// 移动目录鼠标单击事件
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Move(directory_path, directory_otherpath);
                MessageBox.Show("文件夹移动成功。", "警报");
                //举例来讲,如果您尝试将c:\mydir 移到c:\public,并且c:\public 已存在,
                //则此方法引发IOException。您必须将“c:\\public\\mydir”指定为destDirName 参数,或者指定新目录名,例如“c:\\newdir”。
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
        /// <summary>
        /// 目录创建时间鼠标单击事件
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
    MessageBox.Show(string.Format("{0:G}",Directory.GetCreationTime(directory_path)), "提示");
            //获取时间格式参见DateTimeFormatInfo
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
        /// <summary>
        /// 返回指定目录文件鼠标单击事件
        /// </summary>
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                string[] fileEntries = Directory.GetFiles(directory_path);
                if (fileEntries.Length != 0)
                {
                    foreach (string s in fileEntries)
                    {
                        if (File.Exists(s))
                        {
                            MessageBox.Show("内有文件信息:" + s, "提示");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("空文件夹", "提示");
                }
                //获取时间格式参见DateTimeFormatInfo
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
    }
}
```using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
 
namespace FileOptionApplication
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        private static string directory_path = "c:\\qs250";
        private static string directory_otherpath = "c:\\qqqq";
        /// <summary>
        /// 删除目录鼠标单击事件
        /// </summary>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.CreateDirectory(directory_path);
                button2.Enabled = true;
                button1.Enabled = false;
                button3.Enabled = true;
                button4.Enabled = true;
                button5.Enabled = true;
                MessageBox.Show("文件夹成功建立。", "警报");
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
        /// <summary>
        /// 删除目录鼠标单击事件
        /// </summary>
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Delete(directory_path);
                button2.Enabled = false;
                button1.Enabled = true;
                button3.Enabled = false;
                button4.Enabled = false;
                button5.Enabled = false;
                MessageBox.Show("文件夹删除建立。", "警报");
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
        /// <summary>
        /// 移动目录鼠标单击事件
        /// </summary>
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                Directory.Move(directory_path, directory_otherpath);
                MessageBox.Show("文件夹移动成功。", "警报");
                //举例来讲,如果您尝试将c:\mydir 移到c:\public,并且c:\public 已存在,
                //则此方法引发IOException。您必须将“c:\\public\\mydir”指定为destDirName 参数,或者指定新目录名,例如“c:\\newdir”。
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
        /// <summary>
        /// 目录创建时间鼠标单击事件
        /// </summary>
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
    MessageBox.Show(string.Format("{0:G}",Directory.GetCreationTime(directory_path)), "提示");
            //获取时间格式参见DateTimeFormatInfo
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
        /// <summary>
        /// 返回指定目录文件鼠标单击事件
        /// </summary>
        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                string[] fileEntries = Directory.GetFiles(directory_path);
                if (fileEntries.Length != 0)
                {
                    foreach (string s in fileEntries)
                    {
                        if (File.Exists(s))
                        {
                            MessageBox.Show("内有文件信息:" + s, "提示");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("空文件夹", "提示");
                }
                //获取时间格式参见DateTimeFormatInfo
            }
            catch (Exception mm)
            {
                MessageBox.Show("磁盘操作错误,原因:" + Convert.ToString(mm), "警报");
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值