C# Directory.GetFiles的代码示例
通过代码示例来学习C# Directory.GetFiles方法
通过代码示例来学习编程是非常高效的。
1. 代码示例提供了一个具体而直观的学习环境,使初学者能够立即看到编程概念和语法的实际应用。
2. 通过分析和模仿现有的代码实例,初学者可以更好地理解编程逻辑和算法的工作原理。
3. 代码实例往往涵盖了多种编程技巧和最佳实践,通过学习和模仿这些实例,学习者可以逐步掌握如何编写高效、可读性强和可维护的代码。这对于初学者来说,是一种快速提升编程水平的有效途径。
Directory.GetFiles是C#的System.IO命名空间下中的一个方法, 小编为大家找了一些网络大拿们常见的代码示例,源码中的Directory.GetFiles() 已经帮大家高亮显示了,大家可以重点学习Directory.GetFiles() 方法的写法,从而快速掌握该方法的应用。
Directory.GetFiles的代码示例1 - SearchForGameExe()
using System.IO; string SearchForGameExe(string dirPath) { List
filesToCheck = new List (); // Try searching for UE4/UE5 specific EXE name var allExeFiles = Directory.GetFiles(dirPath, "*.exe", SearchOption.AllDirectories); foreach (var filePath in allExeFiles) { var fileName = Path.GetFileNameWithoutExtension(filePath).ToLower(); if (fileName.Contains("-win") && fileName.EndsWith("-shipping")) filesToCheck.Add(filePath); } // blacklist some DLLs that are known to not be relevant var blacklistDlls = new string[] { "dlsstweak", "igxess", "libxess", "nvngx", "EOSSDK-", "steam_api", "sl.", "D3D12", "dstorage", "PhysX", "NvBlast", "amd_", "PeanutButter." }; if (filesToCheck.Count <= 0) { // Fetch all EXE/DLL files in the chosen dir filesToCheck.AddRange(Directory.GetFiles(dirPath, "*.exe")); filesToCheck.AddRange(Directory.GetFiles(dirPath, "*.dll")); } long dllSizeMinimum = 2_097_152; // 2MB // Return largest EXE we find in the specified folder, most likely to be the game EXE // TODO: search subdirectories too and recommend user change folder if larger EXE was found? FileInfo largest = null; foreach (var file in filesToCheck) { var lowerName = Path.GetFileName(file).ToLower(); // Check against blacklistDlls array if (blacklistDlls.Any(blacklistDll => lowerName.StartsWith(blacklistDll.ToLower()))) continue; var info = new FileInfo(file); if (info.Extension.ToLower() == "dll" && info.Length < dllSizeMinimum) continue; if (largest == null || info.Length > largest.Length) largest = info; } if (largest == null) return null; return largest.FullName; }
开发者ID:emoose,项目名称:DLSSTweaks,代码行数:66,代码来源:Main.cs
在SearchForGameExe()方法中,Directory的代码示例类中的GetFiles的代码示例方法一共出现了3次, 见黄色背景高亮显示的地方,欢迎大家点赞
Directory.GetFiles的代码示例2 - CloneCreatesCorrectFilesInRoot()
using System.IO; [TestCase] public void CloneCreatesCorrectFilesInRoot() { GVFSFunctionalTestEnlistment enlistment = GVFSFunctionalTestEnlistment.CloneAndMount(GVFSTestConfig.PathToGVFS); try { string[] files = Directory.GetFiles(enlistment.EnlistmentRoot); files.Length.ShouldEqual(1); files.ShouldContain(x => Path.GetFileName(x).Equals("git.cmd", StringComparison.Ordinal)); string[] directories = Directory.GetDirectories(enlistment.EnlistmentRoot); directories.Length.ShouldEqual(2); directories.ShouldContain(x => Path.GetFileName(x).Equals(GVFSTestConfig.DotGVFSRoot, StringComparison.Ordinal)); directories.ShouldContain(x => Path.GetFileName(x).Equals("src", StringComparison.Ordinal)); } finally { enlistment.UnmountAndDeleteAll(); } }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:21,代码来源:CloneTests.cs
在CloneCreatesCorrectFilesInRoot()方法中,Directory的代码示例类中的GetFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
Directory.GetFiles的代码示例3 - TryBackupFilesInFolder()
using System.IO; private bool TryBackupFilesInFolder(ITracer tracer, string folderPath, string backupPath, string searchPattern, params string[] filenamesToSkip) { string errorMessage; foreach (string file in Directory.GetFiles(folderPath, searchPattern)) { string fileName = Path.GetFileName(file); if (!filenamesToSkip.Any(x => x.Equals(fileName, GVFSPlatform.Instance.Constants.PathComparison))) { if (!this.TryIO( tracer, () => File.Move(file, file.Replace(folderPath, backupPath)), $"Backing up {Path.GetFileName(file)}", out errorMessage)) { return false; } } } return true; }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:23,代码来源:DehydrateVerb.cs
在TryBackupFilesInFolder()方法中,Directory的代码示例类中的GetFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
Directory.GetFiles的代码示例4 - GetLooseObjectFiles()
using System.IO; private List
GetLooseObjectFiles() { List looseObjectFiles = new List (); foreach (string directory in Directory.GetDirectories(this.GitObjectRoot)) { // Check if the directory is 2 letter HEX if (Regex.IsMatch(directory, @"[/\\][0-9a-fA-F]{2}$")) { string[] files = Directory.GetFiles(directory); looseObjectFiles.AddRange(files); } } return looseObjectFiles; }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:17,代码来源:LooseObjectStepTests.cs
在GetLooseObjectFiles()方法中,Directory的代码示例类中的GetFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
Directory.GetFiles的代码示例5 - DeleteDirectory()
using System.IO; public virtual void DeleteDirectory(string path, bool recursive = true, bool ignoreDirectoryDeleteExceptions = false) { if (!Directory.Exists(path)) { return; } DirectoryInfo directory = new DirectoryInfo(path); if (recursive) { foreach (FileInfo file in directory.GetFiles()) { file.Attributes = FileAttributes.Normal; file.Delete(); } foreach (DirectoryInfo subDirectory in directory.GetDirectories()) { this.DeleteDirectory(subDirectory.FullName, recursive, ignoreDirectoryDeleteExceptions); } } try { directory.Delete(); } catch (Exception) { if (!ignoreDirectoryDeleteExceptions) { throw; } } }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:37,代码来源:PhysicalFileSystem.cs
在DeleteDirectory()方法中,Directory的代码示例类中的GetFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
Directory.GetFiles的代码示例6 - PrefetchCleansUpStaleTempPrefetchPacks()
using System.IO; [TestCase, Order(8)] public void PrefetchCleansUpStaleTempPrefetchPacks() { this.Enlistment.UnmountGVFS(); // Create stale packs and idxs in the temp folder string stalePackContents = "StalePack"; string stalePackPath = Path.Combine(this.TempPackRoot, $"{PrefetchPackPrefix}-123456-{Guid.NewGuid().ToString("N")}.pack"); this.fileSystem.WriteAllText(stalePackPath, stalePackContents); stalePackPath.ShouldBeAFile(this.fileSystem).WithContents(stalePackContents); string staleIdxContents = "StaleIdx"; string staleIdxPath = Path.ChangeExtension(stalePackPath, ".idx"); this.fileSystem.WriteAllText(staleIdxPath, staleIdxContents); staleIdxPath.ShouldBeAFile(this.fileSystem).WithContents(staleIdxContents); string stalePackPath2 = Path.Combine(this.TempPackRoot, $"{PrefetchPackPrefix}-123457-{Guid.NewGuid().ToString("N")}.pack"); this.fileSystem.WriteAllText(stalePackPath2, stalePackContents); stalePackPath2.ShouldBeAFile(this.fileSystem).WithContents(stalePackContents); string stalePack2TempIdx = Path.ChangeExtension(stalePackPath2, ".tempidx"); this.fileSystem.WriteAllText(stalePack2TempIdx, staleIdxContents); stalePack2TempIdx.ShouldBeAFile(this.fileSystem).WithContents(staleIdxContents); // Create other unrelated file in the temp folder string otherFileContents = "Test file, don't delete me!"; string otherFilePath = Path.Combine(this.TempPackRoot, "ReadmeAndDontDeleteMe.txt"); this.fileSystem.WriteAllText(otherFilePath, otherFileContents); otherFilePath.ShouldBeAFile(this.fileSystem).WithContents(otherFileContents); this.Enlistment.Prefetch("--commits"); this.PostFetchJobShouldComplete(); // Validate stale prefetch packs are cleaned up Directory.GetFiles(this.TempPackRoot, $"{PrefetchPackPrefix}*.pack").ShouldBeEmpty("There should be no .pack files in the tempPack folder"); Directory.GetFiles(this.TempPackRoot, $"{PrefetchPackPrefix}*.idx").ShouldBeEmpty("There should be no .idx files in the tempPack folder"); Directory.GetFiles(this.TempPackRoot, $"{PrefetchPackPrefix}*.tempidx").ShouldBeEmpty("There should be no .tempidx files in the tempPack folder"); // Validate other files are not impacted otherFilePath.ShouldBeAFile(this.fileSystem).WithContents(otherFileContents); }
开发者ID:microsoft,项目名称:VFSForGit,代码行数:43,代码来源:PrefetchVerbWithoutSharedCacheTests.cs
在PrefetchCleansUpStaleTempPrefetchPacks()方法中,Directory的代码示例类中的GetFiles的代码示例方法一共出现了3次, 见黄色背景高亮显示的地方,欢迎大家点赞
Directory.GetFiles的代码示例7 - GetFiles()
using System.IO; ///
/// Analyses /// The base directory for file analysis. It is used in conjunction with /// relativeand returns all files matching . /// .Though takes precedence if it is an absolute path. /// Array of public File[] GetFiles(string baseDirectory) { if (IO.Path.IsPathRooted(Directory)) baseDirectory = Directory; if (baseDirectory.IsEmpty()) baseDirectory = Environment.CurrentDirectory; baseDirectory = IO.Path.GetFullPath(baseDirectory); string rootDirPath; if (IO.Path.IsPathRooted(Directory)) rootDirPath = Directory; else rootDirPath = Utils.PathCombine(baseDirectory, Directory); var files = new Lists. (); var excludeWildcards = new List (); foreach (string file in IO.Directory.GetFiles(rootDirPath, IncludeMask)) { bool ignore = false; if (!ignore && Filter(file)) { var filePath = IO.Path.GetFullPath(file); var item = new File(filePath) { Feature = this.Feature, Features = this.Features, AttributesDefinition = this.AttributesDefinition, Attributes = this.Attributes.Clone() }; OnProcess?.Invoke(item); files.Add(item); } } return files.ToArray(); }
开发者ID:oleg-shilo,项目名称:wixsharp,代码行数:49,代码来源:DirFiles.cs
在GetFiles()方法中,Directory的代码示例类中的GetFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
Directory.GetFiles的代码示例8 - DeleteIfExists()
using System.IO; ///
/// Deletes File/Directory from by the specified path if it exists. /// /// The path. /// if set tofalse handle all exceptions silently. ///public static string DeleteIfExists(this string path, bool @throw = false) { void deleteFile(string file) { try { var fullPath = IO.Path.GetFullPath(file); if (IO.File.Exists(fullPath)) IO.File.Delete(fullPath); } catch { if (@throw) throw; } } void deleteDir(string file) { try { var fullPath = IO.Path.GetFullPath(file); if (IO.Directory.Exists(fullPath)) IO.Directory.Delete(fullPath); } catch { if (@throw) throw; } } if (path.IsDirectory()) { IO.Directory.GetFiles(path, "*", IO.SearchOption.AllDirectories) .ForEach(deleteFile); IO.Directory.GetDirectories(path, "*", IO.SearchOption.AllDirectories) .OrderByDescending(x => x) .ForEach(deleteDir); deleteDir(path); } else { deleteFile(path); } return path; }
开发者ID:oleg-shilo,项目名称:wixsharp,代码行数:58,代码来源:Extensions.cs
在DeleteIfExists()方法中,Directory的代码示例类中的GetFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞
本文中的Directory.GetFiles方法示例由csref.cn整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。