本文整理汇总了C#中IDirectoryInfo.GetFiles方法的典型用法代码示例。如果您正苦于以下问题:C# IDirectoryInfo.GetFiles方法的具体用法?C# IDirectoryInfo.GetFiles怎么用?C# IDirectoryInfo.GetFiles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IDirectoryInfo
的用法示例。
在下文中一共展示了IDirectoryInfo.GetFiles方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CopyDirectory
private static IDirectoryInfo CopyDirectory(IDirectoryInfo source, IDirectoryInfo destination)
{
var targetDir = destination.GetDirectories().FirstOrDefault(dir => string.Equals(dir.Name, source.Name, StringComparison.OrdinalIgnoreCase))
?? destination.CreateSubdirectory(source.Name);
foreach (var sourceFile in source.GetFiles())
{
var name = sourceFile.Name;
var targetFile = targetDir.GetFiles().FirstOrDefault(item => string.Equals(item.Name, name, StringComparison.OrdinalIgnoreCase))
?? destination.DriveInfo.GetFileInfo(Path.Combine(targetDir.FullName, name));
using (var input = sourceFile.Open(FileMode.Open, FileAccess.Read))
using (var output = targetFile.Open(FileMode.OpenOrCreate, FileAccess.Write))
{
input.CopyTo(output);
}
}
foreach (var subSource in source.GetDirectories())
{
CopyDirectory(subSource, targetDir);
}
return targetDir;
}
示例2: CreateTestFile
/// <summary>
/// Creates a test file with the specified name under the TV directory.
/// </summary>
/// <param name="directory">
/// The directory that the file is in.
/// </param>
/// <param name="names">
/// The names of the files to create.
/// </param>
/// <returns>
/// The files that have been created.
/// </returns>
protected IFileInfo[] CreateTestFile(IDirectoryInfo directory, params string[] names)
{
var files = new IFileInfo[names.Length];
for (int i = 0; i < names.Length; i++)
{
var file = Substitute.For<IFileInfo>();
file.Name.Returns(names[i]);
string fullName = string.Concat(directory.FullName, Path.DirectorySeparatorChar, names[i]);
file.FullName.Returns(fullName);
file.Extension.Returns(names[i].Substring(names[i].LastIndexOf('.')));
file.Exists.Returns(true);
file.Directory.Returns(directory);
files[i] = file;
}
directory.GetFiles().Returns(files);
return files;
}
示例3: AddDirectoryToZipFile
private void AddDirectoryToZipFile (IDirectoryInfo directoryInfo, ZipOutputStream zipOutputStream, ZipNameTransform nameTransform)
{
foreach (var file in directoryInfo.GetFiles ())
AddFileToZipFile (file, zipOutputStream, nameTransform);
foreach (var directory in directoryInfo.GetDirectories ())
AddDirectoryToZipFile (directory, zipOutputStream, nameTransform);
}
示例4: doesFolderContainAspxFiles
bool doesFolderContainAspxFiles(IDirectoryInfo di, int callLevel)
{
if (di.Name.ToLower() == "node_modules") return false;
if (callLevel > 6) return false;
try {
if (di.GetFiles("*.aspx").Length > 0) return true;
foreach (var sdi in di.GetDirectories()) {
if (doesFolderContainAspxFiles(sdi, callLevel + 1)) return true;
}
} catch (PathTooLongException) { }
return false;
}
示例5: doesFolderContainAspxFiles
bool doesFolderContainAspxFiles(IDirectoryInfo di)
{
if (di.GetFiles("*.aspx").Length > 0) return true;
foreach (var sdi in di.GetDirectories()) {
if (doesFolderContainAspxFiles(sdi)) return true;
}
return false;
}
示例6: DeleteEmptySubdirectories
///// <summary>
///// Deletes the subdirectories of the file if they are empty.
///// </summary>
///// <param name="file">
///// The file to check.
///// </param>
//private void DeleteEmptySubdirectories(FileResult file)
//{
// // If no files exist in the directory
// if (file.InputFile.Directory != null && !file.InputFile.Directory.GetFiles().Any()
// && !file.InputFile.Directory.GetDirectories().Any())
// {
// // If this isn't the source directory
// if (
// !file.InputFile.Directory.FullName.TrimEnd(Path.DirectorySeparatorChar).Equals(
// this.settings.SourceDirectory.TrimEnd(Path.DirectorySeparatorChar)))
// {
// file.InputFile.Directory.Delete(true);
// Logger.OnLogMessage(this, "Delete directory: {0}", LogType.Info, file.InputFile.DirectoryName.Truncate());
// }
// }
//}
/// <summary>
/// Deletes the Empty source folder after copy
/// </summary>
/// <param name="directory">Directory to delete</param>
private void DeleteEmptySubdirectories(IDirectoryInfo directory)
{
if(directory != null && !directory.GetFiles().Where(file => this.settings.FileExtensions.Contains(file.Extension)).Any())
{
if (!directory.FullName.ToLower().Equals(this.settings.DefaultDestinationDirectory))
{
directory.Delete(true);
Logger.OnLogMessage(this, "Delete directory: {0}", LogType.Info, directory.FullName.Truncate());
}
}
}
示例7: doesFolderContainCssFiles
bool doesFolderContainCssFiles(IDirectoryInfo di)
{
if (di.FullName.Contains("\\App_Themes\\Default\\WAF\\")) return false;
if (di.GetFiles("*.css").Length > 0) return true;
foreach (var sdi in di.GetDirectories()) {
if (doesFolderContainCssFiles(sdi)) return true;
}
return false;
}
示例8: ScanDirectories
/// <summary>
/// Scans a directory recursively.
/// </summary>
/// <param name="rootDirectory">The root directory.</param>
private void ScanDirectories(IDirectoryInfo rootDirectory)
{
if (this.IsStopped) { return; }
try
{
if (rootDirectory.Exists)
{
IEnumerable<IFileInfo> files = rootDirectory.GetFiles();
foreach (IFileInfo file in files)
{
if (this.IsStopped) { return; }
if (file.Exists)
{
this.OnFileFound(new FileFoundEventArgs(file));
}
}
IEnumerable<IDirectoryInfo> directories = rootDirectory.GetDirectories();
foreach (IDirectoryInfo directory in directories)
{
if (this.IsStopped)
{
return;
}
if (directory.Name == "$RECYCLE.BIN" || !directory.Exists)
{
continue;
}
this.OnDirectoryFound(new DirectoryFoundEventArgs(directory));
this.ScanDirectories(directory);
}
}
}
// Catch the exceptions and don't handle anything,
// we want to skip those files or directories
catch (UnauthorizedAccessException)
{
}
catch (SecurityException)
{
}
catch (IOException)
{
}
this.OnDirectoryProceeded(EventArgs.Empty);
}
示例9: ProcessDirectory
/// <summary>
/// Processes the specified directory looking for episodes.
/// </summary>
/// <param name="directory">
/// The directory to search.
/// </param>
/// <param name="overrideRecurse">
/// A value indicating whether the setting for recurse subdirectories should be overriden.
/// </param>
/// <param name="ignoreShowUpdate">
/// A value indicating whether the settings for updating and locked shows should be ignored.
/// </param>
/// <returns>
/// The list of identified files.
/// </returns>
private IEnumerable<FileResult> ProcessDirectory(
IDirectoryInfo directory, bool overrideRecurse = false, bool ignoreShowUpdate = false)
{
// Get the files where the extension is in the list of extensions.
IEnumerable<IFileInfo> files =
directory.GetFiles().Where(file => this.settings.FileExtensions.Contains(file.Extension));
foreach (FileResult result in files.Select(info => this.ProcessFile(info, ignoreShowUpdate)))
{
if (result != null)
{
yield return result;
}
}
// If ignore show update is on then this is being run from the FileManger as part
// of a move or copy operation. It should add to log that it is scanning the directory.
if (!ignoreShowUpdate)
{
Logger.OnLogMessage(this, "Scanned directory: {0}", LogType.Info, directory.FullName.Truncate());
}
if (!this.settings.RecurseSubdirectories && !overrideRecurse)
{
yield break;
}
IDirectoryInfo[] dirs = directory.GetDirectories().Where(d => !d.DirectoryAttributes.HasFlag(FileAttributes.System)
&& !settings.IgnoredDirectories.Contains(d.FullName)).ToArray();
foreach (FileResult result in
dirs.SelectMany(dir => this.ProcessDirectory(dir, overrideRecurse, ignoreShowUpdate)))
{
yield return result;
}
}
示例10: GetLocalDirectoryTree
/// <summary>
/// Gets the local directory tree.
/// </summary>
/// <returns>The local directory tree.</returns>
/// <param name="parent">Parent directory.</param>
/// <param name="filter">Filter for files.</param>
public static IObjectTree<IFileSystemInfo> GetLocalDirectoryTree(IDirectoryInfo parent, IFilterAggregator filter) {
var children = new List<IObjectTree<IFileSystemInfo>>();
try {
foreach (var child in parent.GetDirectories()) {
string reason;
if (!filter.InvalidFolderNamesFilter.CheckFolderName(child.Name, out reason) && !filter.FolderNamesFilter.CheckFolderName(child.Name, out reason) && !filter.SymlinkFilter.IsSymlink(child, out reason)) {
children.Add(GetLocalDirectoryTree(child, filter));
} else {
Logger.Info(reason);
}
}
foreach (var file in parent.GetFiles()) {
string reason;
if (!filter.FileNamesFilter.CheckFile(file.Name, out reason) && !filter.SymlinkFilter.IsSymlink(file, out reason)) {
children.Add(new ObjectTree<IFileSystemInfo> {
Item = file,
Children = new List<IObjectTree<IFileSystemInfo>>()
});
} else {
Logger.Info(reason);
}
}
} catch (System.IO.PathTooLongException) {
Logger.Fatal(string.Format("One or more children paths of \"{0}\" are to long to be synchronized, synchronization is impossible since the problem is fixed", parent.FullName));
throw;
}
IObjectTree<IFileSystemInfo> tree = new ObjectTree<IFileSystemInfo> {
Item = parent,
Children = children
};
return tree;
}
示例11: BundleExistsInRepository
private bool BundleExistsInRepository(IDirectoryInfo bundleDir, string bundleFileName)
{
IFileInfo bundleFile = bundleDir.GetFiles(bundleFileName).FirstOrDefault();
if (bundleFile == null)
return false;
return bundleFile.Exists;
}
示例12: AddDirectoryFilesToRepository
internal void AddDirectoryFilesToRepository(IDirectoryInfo directory, Version version, int? bundleId = null,
int? projectId = null)
{
IFileInfo[] files = directory.GetFiles("*.*", SearchOption.AllDirectories);
var totalFiles = files.Length;
if (totalFiles == 0)
return;
using (var connection = GetOpenConnection())
{
IDbTransaction trans = null;
for (int i = 0; i < totalFiles; i++)
{
if (trans == null)
trans = connection.BeginTransaction();
if (FileShouldBeTracked(files[i].Url))
AddFileToReleaseDb(files[i], version, connection, bundleId, projectId);
if (i % 100 == 0)
{
trans.Commit();
trans.Dispose();
trans = null;
}
//notify progress every 100 files
if ((i % 100 == 0) && (AddFileProgress != null))
{
var percentComplete = (int)((i / (float)totalFiles) * 100);
AddFileProgress(this, new FileProgressEventArgs(percentComplete));
}
}
if (trans != null)
{
trans.Commit();
trans.Dispose();
}
if (AddFileProgress != null)
AddFileProgress(this, new FileProgressEventArgs(100));
}
}
示例13: BuildBaseProject
public bool BuildBaseProject(ProjectInstallInfo installedProjectInfo, IDirectoryInfo projectBackupDir, string baseProjectPath)
{
if (!projectBackupDir.Exists)
throw new ArgumentException("Path does not exist.", "projectBackupPath");
if (string.IsNullOrEmpty(installedProjectInfo.ProjectVersionInfo.BackupFileName))
throw new ArgumentException("BackupFileName is not set.", "projectInfo");
IFileInfo backupFile = projectBackupDir.GetFiles(installedProjectInfo.ProjectVersionInfo.BackupFileName).FirstOrDefault();
if (backupFile == null)
{
throw new ApplicationException(string.Format("Project backup file {0} does not exist.",
Path.Combine(projectBackupDir.FullName, installedProjectInfo.ProjectVersionInfo.BackupFileName)));
}
IDirectoryInfo baseDir = FileSystem.FileSystem.GetDirectoryInfo(baseProjectPath);
if (!baseDir.Exists)
baseDir.Create();
var bundlesToInstall = installedProjectInfo.BundlesApplied
.OrderBy(b => b.Version.Major)
.ThenBy(b => b.Version.Minor)
.ThenBy(b => b.Version.Build)
.ThenBy(b => b.Version.Revision);
//ensure we can find all the bundle files
foreach (BundleInfo bundleInfo in bundlesToInstall)
{
if (!BundleExistsInRepository(projectBackupDir, bundleInfo.FileName))
{
_log.Error(string.Format("The bundle file \"{0}\" could not be found.", bundleInfo.FileName));
Environment.Exit(-1);
}
}
var backup = new ProjectBackup(backupFile.FullName);
var workspace = new ProjectWorkspace(baseProjectPath);
workspace.RestoreProjectFromBackup(backup);
IProject project = new Project(workspace);
var pcs = new SimpleProjectContextService(project);
ApplicationContext.Current.Services.Add(typeof(IProjectContextService), pcs);
var installResults = bundlesToInstall.Select(bundleInfo =>
InstallBundle(projectBackupDir.FullName, bundleInfo.FileName, project));
return installResults.All(result => result);
}