Directory.GetFiles 方法
[C#]
public static string[] GetFiles(
string path
);
参数
path
将从其检索文件的目录。
返回值
指定目录中文件名的 String 数组。备注
此方法与将星号 (*) 指定为搜索模式的 GetFiles(String, String) 方法相同。允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory。path 参数不区分大小写。
[C#]
public static string[] GetFiles(
string path
);
参数
path
将从其检索文件的目录。
返回值
指定目录中文件名的 String 数组。备注
此方法与将星号 (*) 指定为搜索模式的 GetFiles(String, String) 方法相同。允许 path 参数指定相对或绝对路径信息。相对路径信息被解释为相对于当前工作目录。若要获取当前工作目录,请参见 GetCurrentDirectory。path 参数不区分大小写。
解决方案 »
- 求助:在从服务器接收结果时发生传输级错误。 (provider: TCP 提供程序, error: 0 - 信号灯超时时间已到)
- C#中对多个Byte[]进行MD5 Hash
- C++写的DLL,C#调用,会不会出现名字改编问题?如何解决?
- C#如何判断那个超级练级被点中
- 怎样取得我在datagrid里面选中的记录在datatable里面的row编号?
- 在.net环境下获取硬件参数
- 关于C#的问题
- 各位高手帮忙阿
- 用vs向导生成的dataset,能否设置全局默认值?
- 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作。急求解决啊!
- 请问如何在C#中声明一个有固定长度的 string ?
- ★★★★★紧急求助,多线程的问题★★★★★
获取指定目录中子目录的名称。受 .NET Framework 精简版的支持。[C#] public static string[] GetDirectories(string);
从当前目录获取与指定搜索模式匹配的目录的数组。受 .NET Framework 精简版的支持。[C#] public static string[] GetDirectories(string, string);
示例[C#]
using System;
using System.IO;class Test
{
public static void Main()
{
try
{
// Only get subdirectories that begin with the letter "p."
string[] dirs = Directory.GetDirectories(@"c:\", "p*");
Console.WriteLine("The number of directories starting with p is {0}.", dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}请参见
Directory 类 | Directory 成员 | System.IO 命名空间 | C++ 托管扩展编程
我想实现的结果如下:
c:\1.txt
c:\2.txt
c:\a\1.txt
c:\f\2\f\3.txt
就是把某个目录下的包括其子目录中的所有文件名列出
关键字 递归算法--遍历指定目录下的子目录及文件(C#.net)
出处
//递归算法--遍历指定目录下的子目录及文件(C#.net),希望有用,顺带数据入库 private void button1_Click(object sender, System.EventArgs e)
{
Conn.Open(); displayItems(textBox1.Text);
//MessageBox.Show(dirs.Length.ToString());
Conn.Close();
}
/*这里是递归算法的遍历程序部分*/
private void displayItems(string path)
{
try
{
DirectoryInfo di=new DirectoryInfo(path);
FileInfo[] SubFiles=di.GetFiles(); FileSystemInfo[] dirs = di.GetDirectories(); foreach(FileInfo fileNext in SubFiles)
{
/*-----------------------------------------------------------------------------------------------------*/
string path_total=path + "/" +fileNext.ToString();
int path_start=path_total.IndexOf("/")+1;
int path_end=path_total.LastIndexOf("/");
string path_name;
if(path_start-1==path_end)
{
path_name="";
}
else
{
path_name=path.Substring(path_start,path_end-path_start);
}
switch(path_start.ToString())
{
case "....":
break;
}
/*-----------------------------------------------------------------------------------------------------*/
string sql="insert into pic_data(pic_name,pic_path,pic_time) values(’"+ fileNext.ToString() +"’,’"+ path_name +"’,’"+ DateTime.Now.ToString() +"’)";
SqlCommand Cmd=new SqlCommand(sql,Conn);
if(checkBox_insertdb.Checked==true)
Cmd.ExecuteNonQuery(); richTextBox1.Text=richTextBox1.Text + "\r\n" + path + "/" +fileNext.ToString() + " ("+sql+")";
}
foreach(DirectoryInfo diNext in dirs)
{
displayItems(path + "/" + diNext.ToString());
}
}
catch(Exception ex)
{
richTextBox1.Text=ex.Message +"\r\n"+ richTextBox1.Text;
} }
}
//供参考