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

所属分类: 软件编程 / C#教程 阅读数: 39
收藏 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# 字符串、数组和List的截取和转换实例

下面小编就为大家分享一篇C# 字符串、数组和List的截取和转换实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

c#打包文件解压缩的实例

下面小编就为大家分享一篇c#打包文件解压缩的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题

下面小编就为大家分享一篇浅谈C#跨线程调用窗体控件(比如TextBox)引发的线程安全问题,具有很好的参考价值,希望对大家有所帮助
收藏 0 赞 0 分享

判断一个整数是否是2的N次幂实现方法

下面小编就为大家分享一篇判断一个整数是否是2的N次幂实现方法,实例简洁,具有很好的参考价值。希望对大家有所帮助
收藏 0 赞 0 分享

浅谈c#中config.exe 引发的一些问题

下面小编就为大家分享一篇浅谈c#中config.exe 引发的一些问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

基于C#开发中的那些编码问题(详谈)

下面小编就为大家分享一篇基于C#开发中的那些编码问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

C#实现WPS文件转PDF格式的方法示例

这篇文章主要介绍了C#实现WPS文件转PDF格式的方法,涉及C#针对office组件的相关引用与操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

C#实现的文件上传下载工具类完整实例【上传文件自动命名】

这篇文章主要介绍了C#实现的文件上传下载工具类,结合完整实例形式分析了C#操作文件上传与下载功能,并且还可针对上传文件自动命名以避免服务器中的文件名重复,需要的朋友可以参考下
收藏 0 赞 0 分享

C# Socket 发送&接收&返回 简单应用实例

下面小编就为大家分享一篇C# Socket 发送&接收&返回 简单应用实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多