[英]Check for Sub directory in C#
我将从rar / zip文件中提取的文件存储在一个文件夹中。
之后,我将该文件夹中的文件存储到数据库中。
问题是它只存储文件夹中的文件而不存储子目录及其文件 。
如何查找文件夹是否有任何子目录。
我正在使用下面的代码
Directory.CreateDirectory(StorageRoot + path + New_folder);
decom(StorageRoot + path + New_folder, StorageRoot + path + file.FileName);
foreach (var access_file in Directory.GetFiles(StorageRoot + path + New_folder))
{
string new_name=System.IO.Path.GetFileName(access_file);
FileInfo f = new FileInfo(StorageRoot + path + New_folder+ "/" + new_name);
int new_size = unchecked((int)f.Length);
string new_path = path + New_folder;
statuses.Add(new FilesStatus(new_name, new_size, new_path, StorageRoot, data));
}
如何获取文件夹中的文件和目录列表。 并将它们保存在db中?
要获取文件夹中的文件和目录,可以使用Directory.GetFiles()和Directory.GetDirectories()
使用递归或队列递归遍历目录。
递归示例:
void Traverse(string directory)
{
foreach(var dir in Directories.GetDirectories(directory))
{
Traverse(directory);
}
// Your code here
}
这可能有助于:
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo("YOUR PATH");
//List of directories
var result = info.GetDirectories().Select(i => i.FullName);
您可以使用GetDirectories
获取子文件夹DirectoryInfo并迭代它们直到Getdirectories
返回任何内容。
您可以使用Directory.GetFileSystemEntries()
来获取文件和目录的列表。 http://msdn.microsoft.com/en-us/library/system.io.directory.getfilesystementries.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.