国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C# > 正文

C#獲得文件屬性信息的實(shí)現(xiàn)方法

2020-01-24 02:37:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例演示了用Visual C#獲取任意文件的屬性信息,這些屬性信息包括文件名、創(chuàng)建時(shí)間、訪問時(shí)間、最后寫入時(shí)間等等。本實(shí)例需要用到 FileInfo 類。 FileInfo 類用于提供創(chuàng)建、復(fù)制、刪除、移動(dòng)和打開文件的實(shí)例方法,并且?guī)椭鷦?chuàng)建 FileStream 對(duì)象。

主要功能代碼如下:

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); // 添加項(xiàng) sItem 到 listBox1 中 } public Form1() {  InitializeComponent(); } protected override void Dispose( bool disposing ) {  if( disposing )  {  if (components != null)  {   components.Dispose();  }  }  base.Dispose( disposing ); } #region Windows 窗體設(shè)計(jì)器生成的代碼 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 = "請(qǐng)選擇文件";  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 中所有的項(xiàng)  listBox1.Items.Clear();  FileInfo file = new FileInfo(this.openFileDialog1.FileName);  // 向 ListBox 中一次添加一個(gè)項(xiàng),通過防止該控件繪圖來(lái)維護(hù)性能,  // 直到調(diào)用 EndUpdate 方法為止  listBox1.BeginUpdate();  // 獲取文件名  AddItem("文件名 : " + file.Name);  // 獲取文件的長(zhǎng)度  AddItem("文件長(zhǎng)度(bytes) : " + file.Length);  // 獲取當(dāng)前 FileSystemInfo 對(duì)象的創(chuàng)建時(shí)間  AddItem("創(chuàng)建時(shí)間 : " + file.CreationTime.ToString());  // 獲取上次訪問當(dāng)前文件或目錄的時(shí)間  AddItem("最后訪問時(shí)間 : " + file.LastAccessTime.ToString());  // 獲取上次寫入當(dāng)前文件或目錄的時(shí)間  AddItem("最后寫入時(shí)間 : " + file.LastWriteTime.ToString());  listBox1.EndUpdate();  } } }}

本例演示了功能代碼的主體部分,讀者可以根據(jù)自己的要求進(jìn)一步完善其窗體界面與功能。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 贞丰县| 巴塘县| 安乡县| 江津市| 平塘县| 呼伦贝尔市| 利川市| 灵石县| 厦门市| 湘潭县| 黔西县| 小金县| 南京市| 饶河县| 蚌埠市| 绥化市| 左云县| 仲巴县| 建始县| 汽车| 盐山县| 尉犁县| 石首市| 高唐县| 五大连池市| 临江市| 克拉玛依市| 武山县| 泰和县| 道孚县| 云南省| 鹤峰县| 济南市| 固始县| 濮阳县| 秭归县| 阿拉尔市| 札达县| 抚宁县| 宜宾市| 吉林省|