using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsIOFileInfoDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //判断打开文件对话框ShowDialog()方法的返回值 //当选择文件并点击确定后,其返回值就是DialogResult.OK if (openFileDialog1.ShowDialog() == DialogResult.OK) { //通过打开文件对话框的FileName属性,获取打开的文件的名称 textBox1.Text = openFileDialog1.FileName; //实例化FileInfo对象,并加载打开的文件的名称和路径 FileInfo finfo = new FileInfo(textBox1.Text); //定义若干变量来存储文件信息 string strCTime, strLATime, strLWTime, strName, strFName, strDName, strISRead; long lgLength; //通过FileInfo实例的CreationTime属性获取文件创建时间 strCTime = finfo.CreationTime.ToShortDateString(); //获取上次访问该文件的时间 strLATime = finfo.LastAccessTime.ToShortDateString(); //获取上次写入文件的时间 strLWTime = finfo.LastWriteTime.ToShortDateString(); //获取文件名称 strName = finfo.Name; //获取文件的完整目录 strFName = finfo.FullName; //获取文件的完整路径(去除文件名后的路径) strDName = finfo.DirectoryName; //获取文件是否只读 strISRead = finfo.IsReadOnly.ToString(); //获取文件长度(即字节数) lgLength = finfo.Length; MessageBox.Show("文件信息:\n创建时间:" + strCTime + " 上次访问时间:" + strLATime + "\n上次写入时间:" + strLWTime + " 文件名称:" + strName + "\n完整目录:" + strFName + "\n完整路径:" + strDName + "\n是否只读:" + strISRead + " \n文件长度:" + lgLength); } } } }