C#查询指定目录下的文件夹以及文件数量,现在只能查询得到当前目录的数量,关于子目录下的数量无法得出。
望高手指点一下问题出在哪?
具体代码如下:
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 WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
FolderBrowserDialog folder = new FolderBrowserDialog();
if (folder.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folder.SelectedPath;
label2.Text = "";
}
}
else
{
if (label2.Text == "")
{
SearchFile(textBox1.Text /*,0,0*/);
}
else
{
textBox1.Text = "";
}
}
} private void SearchFile(string directory /*,countdir,countfile*/)
{
////DirectoryInfo dir = new DirectoryInfo(directory);
////FileInfo[] fi = dir.GetFiles();
////countfile += fi.Length;
////DirectoryInfo[] di = dir.GetDirectories();
////countdir+=di.Length;
////foreach (DirectoryInfo d in di)
////{
//// string str = dir.ToString() + "\\" + d.ToString();
//// SearchFile(str, countdir, countfile);
////}
int countfile = 0;
int countdir = 0;
DirectoryInfo dir = new DirectoryInfo(directory);
FileSystemInfo[] f = dir.GetFileSystemInfos();
foreach (FileSystemInfo i in f)
{
if (i is DirectoryInfo)
{
countdir++;
SearchFile(i.FullName);
}
else if (i is FileInfo)
{
countfile++;
}
}
label2.Text = string.Format("該目錄下共有 {0} 個文件夾,{1} 個文件", countdir, countfile);
} private void From_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)027)
{
this.Close();
}
} private void Form1_Load(object sender, EventArgs e)
{
label2.Text = "";
}
}
}
望高手指点一下问题出在哪?
具体代码如下:
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 WindowsFormsApplication12
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
FolderBrowserDialog folder = new FolderBrowserDialog();
if (folder.ShowDialog() == DialogResult.OK)
{
textBox1.Text = folder.SelectedPath;
label2.Text = "";
}
}
else
{
if (label2.Text == "")
{
SearchFile(textBox1.Text /*,0,0*/);
}
else
{
textBox1.Text = "";
}
}
} private void SearchFile(string directory /*,countdir,countfile*/)
{
////DirectoryInfo dir = new DirectoryInfo(directory);
////FileInfo[] fi = dir.GetFiles();
////countfile += fi.Length;
////DirectoryInfo[] di = dir.GetDirectories();
////countdir+=di.Length;
////foreach (DirectoryInfo d in di)
////{
//// string str = dir.ToString() + "\\" + d.ToString();
//// SearchFile(str, countdir, countfile);
////}
int countfile = 0;
int countdir = 0;
DirectoryInfo dir = new DirectoryInfo(directory);
FileSystemInfo[] f = dir.GetFileSystemInfos();
foreach (FileSystemInfo i in f)
{
if (i is DirectoryInfo)
{
countdir++;
SearchFile(i.FullName);
}
else if (i is FileInfo)
{
countfile++;
}
}
label2.Text = string.Format("該目錄下共有 {0} 個文件夾,{1} 個文件", countdir, countfile);
} private void From_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)027)
{
this.Close();
}
} private void Form1_Load(object sender, EventArgs e)
{
label2.Text = "";
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货