C# 檔案資訊及操作

目的:實現磁碟資訊及檔案複製等等功能






private void button1_Click(object sender, EventArgs e)
{
    String result = "";
    foreach (DriveInfo di in DriveInfo.GetDrives())
    {
        //取得磁碟的資訊,並逐一列出
        if(di.IsReady)
            //表示有東西,若不是可能是光碟、軟碟機
            result += String.Format("{0}\t{1}\t{2}\t{3}\r\n", di.Name, di.DriveType, di.TotalSize, di.TotalFreeSpace);
            //印出資訊
        else
            result += String.Format("{0}\t{1}\r\n", di.Name, di.DriveType);
    }
    textBox1.Text = result;
}
//-----------------------------------------------------------------------
private void button2_Click(object sender, EventArgs e)
{
    /*
    File.Encrypt(@"C:\VS2012\TT.txt");
    MessageBox.Show("加密成功!");
    */

    File.Copy(@"C:\VS2012\TT.txt", @"C:\練習資料夾\TT2.txt");
    //檔案複製,注意要確認C:\VS2012\TT.txt有檔案
    MessageBox.Show("複製成功!");

}
//-----------------------------------------------------------------------
private void button3_Click(object sender, EventArgs e)
{
    String result="";
    foreach (String a in Directory.GetDirectories(@"C:\VS2012"))
        result += a + "\r\n";
        //取得C:\VS2012下的資料夾資訊
    foreach (String a in Directory.GetFiles(@"C:\VS2012"))
        result += a + "\r\n";
        //取得C:\VS2012下的檔案資訊
    textBox1.Text = result;
}
//-----------------------------------------------------------------------
private void button4_Click(object sender, EventArgs e)
{
    FileInfo fi = new FileInfo(@"C:\VS2012\TT.txt");
    //取得檔案資訊
    //fi.CopyTo(@"C:\練習資料夾\TT2.txt");
    //MessageBox.Show("複製成功!");

    textBox1.Text = fi.Length.ToString() + " Bytes";
}

留言

熱門文章