C#編程實(shí)現(xiàn)統(tǒng)計(jì)文件夾內(nèi)文件和隱藏文件的方法示例
本文實(shí)例講述了C#編程實(shí)現(xiàn)統(tǒng)計(jì)文件夾內(nèi)文件和隱藏文件的方法。分享給大家供大家參考,具體如下:
C#統(tǒng)計(jì)文件夾內(nèi)的文件,包括隱藏文件,顯示那個(gè)隱藏文件...隱藏的..為什么別人要隱藏呢..
將程序放在任何文件夾內(nèi),點(diǎn)擊“當(dāng)前文件夾”,可以獲取文件夾所在的路徑,也可以直接輸入路徑,再點(diǎn)擊“顯示文件”,就可以看到效果了,下面的狀態(tài)欄實(shí)現(xiàn)統(tǒng)計(jì)功能

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
int m = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int i, n = 0,l=0;
string[] filen;
string filea;
listBox1.Items.Clear();
if (!Directory.Exists(textBox1.Text))
MessageBox.Show(textBox1.Text + "文件夾不存在", "信息提示", MessageBoxButtons.OK);
else
{
filen = Directory.GetFiles(textBox1.Text);
for (i = 0; i <= filen.Length - 1; i++)
{
filea = string.Format("{0}\t{1} {2}", filen[i], File.GetCreationTime(filen[i]), fileatt(filen[i]));
listBox1.Items.Add(filea);
n++;
}
}
l = m;
m = 0;
toolStripStatusLabel1.Text = "文件數(shù):" + n;
toolStripStatusLabel2.Text = "被隱藏的文件數(shù):" + l;
}
public string fileatt(string filename)
{
string fa = "";
switch (File.GetAttributes(filename))
{
case FileAttributes.Archive:
fa = "存檔"; break;
case FileAttributes.ReadOnly:
fa = "只讀"; break;
case FileAttributes.Hidden:
fa = "隱藏"; m++;break;
case FileAttributes.Archive | FileAttributes.ReadOnly:
fa = "存檔+只讀"; break;
case FileAttributes.Archive | FileAttributes.Hidden:
fa = "存檔+隱藏";m++;break;
case FileAttributes.ReadOnly | FileAttributes.Hidden:
fa = "只讀+隱藏"; m++;break;
case FileAttributes.Archive | FileAttributes.ReadOnly | FileAttributes.Hidden:
fa = "存檔+只讀+隱藏";m++;break;
}
return fa;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text=System.AppDomain.CurrentDomain.BaseDirectory.ToString();
}
}
}
更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#文件操作常用技巧匯總》、《C#遍歷算法與技巧總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對大家C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
C# Double轉(zhuǎn)化為String時(shí)的保留位數(shù)及格式方式
這篇文章主要介紹了C# Double轉(zhuǎn)化為String時(shí)的保留位數(shù)及格式方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
C#實(shí)現(xiàn)封裝常用Redis工具類的示例代碼
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)封裝常用Redis工具類的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03

