Form1.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace WindowsFormsIOFileInfoDemo
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. //判断打开文件对话框ShowDialog()方法的返回值
  22. //当选择文件并点击确定后,其返回值就是DialogResult.OK
  23. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  24. {
  25. //通过打开文件对话框的FileName属性,获取打开的文件的名称
  26. textBox1.Text = openFileDialog1.FileName;
  27. //实例化FileInfo对象,并加载打开的文件的名称和路径
  28. FileInfo finfo = new FileInfo(textBox1.Text);
  29. //定义若干变量来存储文件信息
  30. string strCTime, strLATime, strLWTime, strName, strFName, strDName, strISRead;
  31. long lgLength;
  32. //通过FileInfo实例的CreationTime属性获取文件创建时间
  33. strCTime = finfo.CreationTime.ToShortDateString();
  34. //获取上次访问该文件的时间
  35. strLATime = finfo.LastAccessTime.ToShortDateString();
  36. //获取上次写入文件的时间
  37. strLWTime = finfo.LastWriteTime.ToShortDateString();
  38. //获取文件名称
  39. strName = finfo.Name;
  40. //获取文件的完整目录
  41. strFName = finfo.FullName;
  42. //获取文件的完整路径(去除文件名后的路径)
  43. strDName = finfo.DirectoryName;
  44. //获取文件是否只读
  45. strISRead = finfo.IsReadOnly.ToString();
  46. //获取文件长度(即字节数)
  47. lgLength = finfo.Length;
  48. MessageBox.Show("文件信息:\n创建时间:" + strCTime + " 上次访问时间:" + strLATime + "\n上次写入时间:" + strLWTime + " 文件名称:" + strName + "\n完整目录:" + strFName + "\n完整路径:" + strDName + "\n是否只读:" + strISRead + " \n文件长度:" + lgLength);
  49. }
  50. }
  51. }
  52. }