天天看點

3-2 檔案夾類Directory的常用方法(2)

u 實驗步驟(2):

在類Form3裡添加二個靜态字段directory_path、directory_otherpath,都為string類型,分别代表工作目錄路徑和其他目錄路徑;輕按兩下“建立目錄”、“删除目錄”、“移動目錄”、“目錄建立時間”、“傳回指定目錄檔案”,在click事件處理方法裡分别添加代碼如下:

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), "警報");

        private void button2_Click(object sender, EventArgs e)

                Directory.Delete(directory_path);

                button2.Enabled = false;

                button1.Enabled = true;

                button3.Enabled = false;

                button4.Enabled = false;

                button5.Enabled = false;

                MessageBox.Show("檔案夾删除建立。", "警報");

        /// 移動目錄滑鼠單擊事件

        private void button3_Click(object sender, EventArgs e)

                Directory.Move(directory_path, directory_otherpath);

                MessageBox.Show("檔案夾移動成功。", "警報");

                //舉例來講,如果您嘗試将c:\mydir 移到c:\public,并且c:\public 已存在,

                //則此方法引發IOException。您必須将“c:\\public\\mydir”指定為destDirName 參數,或者指定新目錄名,例如“c:\\newdir”。

        /// 目錄建立時間滑鼠單擊事件

        private void button4_Click(object sender, EventArgs e)

    MessageBox.Show(string.Format("{0:G}",Directory.GetCreationTime(directory_path)), "提示");

            //擷取時間格式參見DateTimeFormatInfo

        /// 傳回指定目錄檔案滑鼠單擊事件

        private void button5_Click(object sender, EventArgs e)

                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

    }

}

本文轉自 qianshao 51CTO部落格,原文連結:http://blog.51cto.com/qianshao/210949,如需轉載請自行聯系原作者

繼續閱讀