Thứ Ba, 6 tháng 8, 2013

Đặt thuộc tính thư mục và tệp tin


Các thuộc tính tập tin là siêu dữ liệu liên quan đến tập tin máy tính để xác định hành vi của hệ thống. Giống như các file, thư mục, các đối tượng hệ thống tập tin khác có thể có các thuộc tính. Tất cả các hệ điều hành có thường tức là bốn thuộc tính lưu trữ, ẩn, chỉ đọc và hệ thống. Tìm hiểu thêm về thuộc tính tập tin.
FileAttribute áp dụng cho thư mục và tập tin, nằm trong lớp System.IO.

Ta thiết kế form như sau:
Ở đây ta cần OpenFileDialog để mở, duyệt tập tin.

Đối với duyệt cả thư mục con, ta cần phải đệ quy tiến trình, việc này cần nhiều thời gian và code dễ dàng.

Sự kiện duyệt file:

private void btnBrowser_Click(object sender, EventArgs e)
{
string path = (openFileDialog1.ShowDialog() == DialogResult.OK) ?
openFileDialog1.FileName : "";
if (path == "")
return;
}

Code xử lí:
private void btnSet_Click(object sender, EventArgs e)
        {
            if (rdoOnlyHidden.Checked == true)
            {
                count = 0;
                setHidden(txtPath.Text);
                lblStatus.Text = "Number Of Scanned Files And Folders:" + count.ToString();
            }
            if (rdoOnlyRead.Checked == true)
            {
                count = 0;
                setReadOnly(txtPath.Text);
                lblStatus.Text = "Number Of Scanned Files And Folders:" + count.ToString();
            }
            if (rdoRemoveAll.Checked == true)
            {
                count = 0;
                RemoveAll (txtPath.Text);
                lblStatus.Text = "Number Of Scanned Files And Folders:" + count.ToString();
            }
        }

        private void setHidden(string dir)
        {
            if (!Directory.Exists(dir))
            {
                lblStatus.Text = "Invalid path!";
                return;
            }
            if (chkSubFolder.Checked)
            {
                foreach (string d in System.IO.Directory.GetDirectories(dir))
                {
                    count++;
                    lblStatus.Text = dir;
                    setHidden(d);
                    DirectoryInfo df = new DirectoryInfo(d);

                    df.Attributes = System.IO.FileAttributes.Hidden;
                    foreach (string fil in System.IO.Directory.GetFiles(d))
                    {
                        FileInfo f = new FileInfo(fil);
                        f.Attributes = System.IO.FileAttributes.Hidden;
                        Application.DoEvents();
                        count++;
                    }
                }
            }
        }

        private void setReadOnly(string dir)
        {
            if (!Directory.Exists(dir))
            {
                lblStatus.Text = "Invalid path!";
                return;
            }
            if (chkSubFolder.Checked)
            {
                foreach (string d in System.IO.Directory.GetDirectories(dir))
                {
                    count++;
                    lblStatus.Text = dir;
                    setReadOnly(d);
                    DirectoryInfo df = new DirectoryInfo(d);

                    df.Attributes = System.IO.FileAttributes.ReadOnly;
                    foreach (string fil in System.IO.Directory.GetFiles(d))
                    {
                        FileInfo f = new FileInfo(fil);
                        f.Attributes = System.IO.FileAttributes.ReadOnly;
                        Application.DoEvents();
                        count++;
                    }
                }
            }
        }

        private void RemoveAll(string dir)
        {
            if (!Directory.Exists(dir))
            {
                lblStatus.Text = "Invalid path!";
                return;
            }
            if (chkSubFolder.Checked)
            {
                foreach (string d in System.IO.Directory.GetDirectories(dir))
                {
                    count++;
                    lblStatus.Text = dir;
                    RemoveAll(d);
                    DirectoryInfo df = new DirectoryInfo(d);

                    df.Attributes = System.IO.FileAttributes.Normal;
                    foreach (string fil in System.IO.Directory.GetFiles(d))
                    {
                        FileInfo f = new FileInfo(fil);
                        f.Attributes = System.IO.FileAttributes.Normal;
                        Application.DoEvents();
                        count++;
                    }
                }
            }

        }

2 nhận xét:

  1. How to make money at casinos online
    How to Make Money Online — Best gambling sites offer the opportunity to make money 바카라 for themselves. But if you want to make money online, 5 worrione answers  ·  0 หารายได้เสริม votes: I want to make money on the internet. The answer is yes, and as a result, every day.

    Trả lờiXóa
  2. ᐈ Play UK online casino site for real money right now
    Online casino UK · Play luckyclub UK casino site for real money right now · Online casino UK · Play UK casino site for real money right now · Online casino UK · Play UK casino

    Trả lờiXóa