如何循环指定文件夹中的所有文件夹?急!!
我指定一个文件夹:E:\heal该文件夹下面有很多个文件夹,每个文件夹下面就是文件
我想要实现的是:循环E:\heal下面所有的文件夹,判断每个文件夹下面是否有文件,如果有,则上传这个文件到我的数据库,然后删除该文件,请问 我该怎么做呢?才接触文件夹、文件这类东西,希望各位帮帮忙!
谢谢!
程序代码:/// <summary>
/// 拷贝目录(包含子目录及所有文件)
/// </summary>
/// <param name="sourceDirName">源文件目录名</param>
/// <param name="destDirName">目标文件目录名</param>
/// <param name="copySubDirs">是否拷贝子目录</param>
internal static void DirectoryCopy(
string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the source directory does not exist, throw an exception.
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory does not exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the file contents of the directory to copy.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
// Create the path to the new copy of the file.
string temppath = (destDirName, file.Name);
// Copy the file.
file.CopyTo(temppath, false);
}
// If copySubDirs is true, copy the subdirectories.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
// Create the subdirectory.
string temppath = (destDirName, subdir.Name);
// Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}把上面程序改改就可以了。