日期:2014-05-18  浏览次数:20873 次

遍历文件夹中的文件
我的意思是这样的。
在一个文件夹有有大量的JPG(比方员工照片)文件,放在同一个文件夹内。
1。我需要从程序中看到这个文件的列表(用个什么方法可以把文件名列表放入DataGridView的Cell中),
2。单击即打开此文件。

我想知道的是第1步,第二步可以用超链接之内的。
WinForm的,请高手Show下代码。



------解决方案--------------------
C# code
string[] fs=Directory.GetFiles("你的路径", "*.jpg");
foreach(string s in fs)
{
 //把文件名称加到DataTable中
}

------解决方案--------------------
文件夹就是一颗树.
用遍历树的算法就行了.
------解决方案--------------------
遍历JPG

 private void button1_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text.Trim() == "")
{
MessageBox.Show("请输入JPG路径");
return;
}

string srcPath = "D:\\Microsoft SQL Server\\DataBase";
srcPath=textBox1.Text.Trim();
string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath);

foreach (string file in fileList)
{

if (file.Trim().Substring(file.Trim().Length - 3, 3).Trim().ToUpper()=="JPG")
{
bin.AddNew();
cdgv.CurrentRow.Cells["sDish_c"].Value = file.Trim();
}
//if (file.EndsWith("jpg"))
//{
// bin.AddNew();
// cdgv.CurrentRow.Cells["sDish_c"].Value = file.Trim();
//}
//if (file.EndsWith("JPG"))
//{
// bin.AddNew();
// cdgv.CurrentRow.Cells["sDish_c"].Value = file.Trim();
//}

}

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
------解决方案--------------------
同意一楼的