手机
当前位置:查字典教程网 >编程开发 >C#教程 >C#获得文件属性信息的实现方法
C#获得文件属性信息的实现方法
摘要:本文实例演示了用VisualC#获取任意文件的属性信息,这些属性信息包括文件名、创建时间、访问时间、最后写入时间等等。本实例需要用到File...

本文实例演示了用Visual C#获取任意文件的属性信息,这些属性信息包括文件名、创建时间、访问时间、最后写入时间等等。本实例需要用到 FileInfo 类。 FileInfo 类用于提供创建、复制、删除、移动和打开文件的实例方法,并且帮助创建 FileStream 对象。

主要功能代码如下:

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; namespace 获取文件属性 { public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListBox listBox1; private System.Windows.Forms.Button button1; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.ComponentModel.Container components = null; public void AddItem(string sItem) { listBox1.Items.Add(sItem); // 添加项 sItem 到 listBox1 中 } public Form1() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 private void InitializeComponent() { this.listBox1 = new System.Windows.Forms.ListBox(); this.button1 = new System.Windows.Forms.Button(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.SuspendLayout(); // listBox1 this.listBox1.ItemHeight = 12; this.listBox1.Location = new System.Drawing.Point(8, 56); this.listBox1.Name = "listBox1"; this.listBox1.Size = new System.Drawing.Size(272, 208); this.listBox1.TabIndex = 0; // button1 this.button1.Location = new System.Drawing.Point(96, 8); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(96, 32); this.button1.TabIndex = 1; this.button1.Text = "请选择文件"; this.button1.Click += new System.EventHandler(this.button1_Click); // Form1 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.Add(this.button1); this.Controls.Add(this.listBox1); this.Name = "Form1"; this.Text = "获取文件属性"; this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) { if(this.openFileDialog1.ShowDialog() == DialogResult.OK) { // 清除 listBox1 中所有的项 listBox1.Items.Clear(); FileInfo file = new FileInfo(this.openFileDialog1.FileName); // 向 ListBox 中一次添加一个项,通过防止该控件绘图来维护性能, // 直到调用 EndUpdate 方法为止 listBox1.BeginUpdate(); // 获取文件名 AddItem("文件名 : " + file.Name); // 获取文件的长度 AddItem("文件长度(bytes) : " + file.Length); // 获取当前 FileSystemInfo 对象的创建时间 AddItem("创建时间 : " + file.CreationTime.ToString()); // 获取上次访问当前文件或目录的时间 AddItem("最后访问时间 : " + file.LastAccessTime.ToString()); // 获取上次写入当前文件或目录的时间 AddItem("最后写入时间 : " + file.LastWriteTime.ToString()); listBox1.EndUpdate(); } } } }

本例演示了功能代码的主体部分,读者可以根据自己的要求进一步完善其窗体界面与功能。

【C#获得文件属性信息的实现方法】相关文章:

使用C#获取系统特殊文件夹路径的解决方法

共享锁using范围的实现方法

C#读写文件的方法汇总

c#启动EXE文件的方法实例

C# mysql 插入数据,中文乱码的解决方法

C# 调用C++写的dll的实现方法

在C#中创建和读取XML文件的实现方法

C#图片压缩的实现方法

C#计算代码执行时间的方法

C# 获取属性名的方法

精品推荐
分类导航