C#获得文件属性信息的实现方法

所属分类: 软件编程 / C#教程 阅读数: 81
收藏 0 赞 0 分享

本文实例演示了用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#抽象类与抽象方法的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

C#代码实现扑克牌排序的几种方式

今天小编就为大家分享一篇关于C#代码实现扑克牌排序,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#泛型概念的简介与泛型的使用

今天小编就为大家分享一篇关于C#泛型概念的简介与泛型的使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C# 7.0 使用下划线忽略使用的变量的原因分析

这篇文章主要介绍了C# 7.0 使用下划线忽略使用的变量的原因浅析,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

C# 中使用正则表达式匹配字符的含义

正则表达式的作用用来描述字符串的特征。本文重点给大家介绍C# 中使用正则表达式匹配字符的含义,非常不错,具有一定的参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

C# Dictionary和SortedDictionary的简介

今天小编就为大家分享一篇关于C# Dictionary和SortedDictionary的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#中SQL Command的基本用法

今天小编就为大家分享一篇关于C#中SQL Command的基本用法,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataReader访问数据的优点和实例

今天小编就为大家分享一篇关于C#使用SQL DataReader访问数据的优点和实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL Dataset数据集代码实例

今天小编就为大家分享一篇关于的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

C#使用SQL DataAdapter数据适配代码实例

今天小编就为大家分享一篇关于C#使用SQL DataAdapter数据适配代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享
查看更多