本文整理汇总了C#中Folder.AddFolder方法的典型用法代码示例。如果您正苦于以下问题:C# Folder.AddFolder方法的具体用法?C# Folder.AddFolder怎么用?C# Folder.AddFolder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Folder
的用法示例。
在下文中一共展示了Folder.AddFolder方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DirectoryDFS
private Folder DirectoryDFS(Folder folder)
{
try
{
var files = Directory.GetFiles(folder.Name);
foreach (string item in files)
{
FileInfo fileinfo = new FileInfo(item);
File file = new File(item, (int)fileinfo.Length);
folder.AddFile(file);
}
var childFolders = Directory.GetDirectories(folder.Name);
foreach (string item in childFolders)
{
Folder subFolder = new Folder(item);
folder.AddFolder(subFolder);
DirectoryDFS(subFolder);
}
}
//catches the exceptions given if access restricted folder reached
catch (UnauthorizedAccessException)
{
}
return folder;
}
示例2: FillDirectoryTree
private static void FillDirectoryTree(string path, Folder folder)
{
try
{
// Append files
var files = Directory.GetFiles(path);
foreach (var file in files)
{
string fileName = GetName(file);
FileInfo fileInfo = new FileInfo(file);
folder.AddFile(new File(fileName, fileInfo.Length));
}
// Append dirs recursively
var dirs = Directory.GetDirectories(path);
foreach (var dir in dirs)
{
string dirName = GetName(dir);
Folder newFolder = new Folder(dirName);
folder.AddFolder(newFolder);
FillDirectoryTree(dir, newFolder);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
示例3: TraverseDirectory
private static void TraverseDirectory(Folder currentFolder)
{
try
{
DirectoryInfo currentDirecotoryInfo = new DirectoryInfo(currentFolder.Name);
DirectoryInfo[] subDirectories = currentDirecotoryInfo.GetDirectories();
foreach (var file in currentDirecotoryInfo.GetFiles())
{
currentFolder.AddFile(file.Name, (int)file.Length);
}
foreach (var dir in subDirectories)
{
currentFolder.AddFolder(dir.FullName);
}
foreach (var child in currentFolder.ChildFolders)
{
TraverseDirectory(child);
}
}
catch (UnauthorizedAccessException uae)
{
Console.WriteLine("Cannot access directory: {0}", uae.Message);
}
catch(DirectoryNotFoundException dnf)
{
Console.WriteLine("Directory not found: {0}", dnf.Message);
}
}
示例4: GetAllFilesAndFolders
private static Folder GetAllFilesAndFolders(DirectoryInfo directory)
{
Folder currentFolder = new Folder(directory.Name);
try
{
var directories = directory.GetDirectories();
foreach (var dir in directories)
{
currentFolder.AddFolder(GetAllFilesAndFolders(dir));
}
}
catch (UnauthorizedAccessException uae)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(uae.Message);
Console.ForegroundColor = ConsoleColor.Gray;
}
foreach (var file in directory.GetFiles())
{
currentFolder.AddFile(new File(file.Name, file.Length));
}
return currentFolder;
}
示例5: Traverse
static Folder Traverse(string root)
{
var folder = new Folder(root);
foreach (string file in Directory.GetFiles(root))
folder.Add(new File(file, new FileInfo(file).Length));
foreach (string directory in Directory.GetDirectories(root))
folder.AddFolder(Traverse(directory));
return folder;
}
示例6: TraverseDFS
// Traverse input directory using recursive DFS and reflect its structure in input folder
private static void TraverseDFS(DirectoryInfo directory, Folder currentFolder)
{
FileInfo[] files = directory.GetFiles();
List<File> fileList = new List<File>();
foreach (var file in files)
{
fileList.Add(new File(file.Name, file.Length));
}
currentFolder.AddFiles(fileList);
DirectoryInfo[] children = directory.GetDirectories();
List<Folder> folderList = new List<Folder>();
foreach (DirectoryInfo child in children)
{
Folder childFolder = new Folder(child.Name);
currentFolder.AddFolder(childFolder);
TraverseDFS(child, childFolder);
}
}
示例7: FillFolder
// Adds all files and folders from 'source' to the root folder recursively
private static Folder FillFolder(string path, Folder root)
{
try
{
var source = new DirectoryInfo(path);
foreach (var file in source.EnumerateFiles())
{
root.AddFile(new File(file.Name, file.Length));
}
foreach (DirectoryInfo dir in source.EnumerateDirectories())
{
root.AddFolder(FillFolder(dir.FullName, new Folder(dir.Name)));
}
}
catch (System.Exception)
{
}
return root;
}
示例8: SaveFilesAndDirectories
private void SaveFilesAndDirectories(Folder parent)
{
if (parent == null)
{
return;
}
currentDirectory = new DirectoryInfo(parent.Path);
FileInfo[] folderFiles;
try
{
folderFiles = currentDirectory.GetFiles();
}
catch (UnauthorizedAccessException)
{
return;
}
foreach (var file in folderFiles)
{
parent.AddFile(new File(file.Name, file.Length));
}
subDirectories = currentDirectory.GetDirectories();
Folder subFolder;
foreach (var directory in subDirectories)
{
subFolder = new Folder(directory.FullName, directory.Name);
parent.AddFolder(subFolder);
SaveFilesAndDirectories(subFolder);
}
}