C# DirectoryInfo.EnumerateFiles的代码示例

通过代码示例来学习C# DirectoryInfo.EnumerateFiles方法

通过代码示例来学习编程是非常高效的。
1. 代码示例提供了一个具体而直观的学习环境,使初学者能够立即看到编程概念和语法的实际应用。
2. 通过分析和模仿现有的代码实例,初学者可以更好地理解编程逻辑和算法的工作原理。
3. 代码实例往往涵盖了多种编程技巧和最佳实践,通过学习和模仿这些实例,学习者可以逐步掌握如何编写高效、可读性强和可维护的代码。这对于初学者来说,是一种快速提升编程水平的有效途径。


DirectoryInfo.EnumerateFiles是C#的System.IO命名空间下中的一个方法, 小编为大家找了一些网络大拿们常见的代码示例,源码中的DirectoryInfo.EnumerateFiles() 已经帮大家高亮显示了,大家可以重点学习DirectoryInfo.EnumerateFiles() 方法的写法,从而快速掌握该方法的应用。

DirectoryInfo.EnumerateFiles的代码示例1 - LogDirectoryEnumeration()

    using System.IO;

        private void LogDirectoryEnumeration(string sourceRoot, string targetRoot, string folderName, string logfile)
        {
            try
            {
                if (!Directory.Exists(targetRoot))
                {
                    Directory.CreateDirectory(targetRoot);
                }

                string folder = Path.Combine(sourceRoot, folderName);
                string targetLog = Path.Combine(targetRoot, logfile);

                List lines = new List();

                if (Directory.Exists(folder))
                {
                    DirectoryInfo packDirectory = new DirectoryInfo(folder);

                    lines.Add($"Contents of {folder}:");
                    foreach (FileInfo file in packDirectory.EnumerateFiles())
                    {
                        lines.Add($"{file.Name, -70} {file.Length, 16}");
                    }
                }

                File.WriteAllLines(targetLog, lines.ToArray());
            }
            catch (Exception e)
            {
                this.WriteMessage(string.Format(
                    "Failed to log file sizes for {0} in {1} with exception {2}. logfile: {3}",
                    folderName,
                    sourceRoot,
                    e,
                    logfile));
            }
        }
    

开发者ID:microsoft,项目名称:VFSForGit,代码行数:39,代码来源:DiagnoseVerb.cs

在LogDirectoryEnumeration()方法中,DirectoryInfo的代码示例类中的EnumerateFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

DirectoryInfo.EnumerateFiles的代码示例2 - CopyDirectoryRecursive()

    using System.IO;

        public virtual void CopyDirectoryRecursive(
            string srcDirectoryPath,
            string dstDirectoryPath,
            HashSet excludeDirectories = null)
        {
            DirectoryInfo srcDirectory = new DirectoryInfo(srcDirectoryPath);

            if (!this.DirectoryExists(dstDirectoryPath))
            {
                this.CreateDirectory(dstDirectoryPath);
            }

            foreach (FileInfo file in srcDirectory.EnumerateFiles())
            {
                this.CopyFile(file.FullName, Path.Combine(dstDirectoryPath, file.Name), overwrite: true);
            }

            foreach (DirectoryInfo subDirectory in srcDirectory.EnumerateDirectories())
            {
                if (excludeDirectories == null || !excludeDirectories.Contains(subDirectory.FullName))
                {
                    this.CopyDirectoryRecursive(
                        subDirectory.FullName,
                        Path.Combine(dstDirectoryPath, subDirectory.Name),
                        excludeDirectories);
                }
            }
        }
    

开发者ID:microsoft,项目名称:VFSForGit,代码行数:30,代码来源:PhysicalFileSystem.cs

在CopyDirectoryRecursive()方法中,DirectoryInfo的代码示例类中的EnumerateFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

DirectoryInfo.EnumerateFiles的代码示例3 - TakeOwnDirectory()

    using System.IO;
        public static void TakeOwnDirectory(string path)
        {
            DirectoryInfo directoryInfo = new(path);
            DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();

            directorySecurity.SetOwner(WindowsIdentity.GetCurrent().User);

            Privilege p = new(Privilege.TakeOwnership);
            bool ownershipTaken = false;
            try
            {
                p.Enable();

                new DirectoryInfo(path).SetAccessControl(directorySecurity);
                ownershipTaken = true;
            }
            catch (Privileges.PrivilegeNotHeldException e)
            {
                Console.WriteLine("Failed to assign privileges. " + e.ToString());
            }
            finally
            {
                p.Revert();
            }

            if (ownershipTaken)
            {
                AdjustPermissionsForDirectory(path);

                foreach (string subFile in (System.Collections.Generic.IEnumerable)Directory.EnumerateFiles(path))
                {
                    TakeOwnFile(subFile);
                }

                foreach (string subDir in (System.Collections.Generic.IEnumerable)Directory.EnumerateDirectories(path))
                {
                    TakeOwnDirectory(subDir);
                }
            }
        }
    

开发者ID:gus33000,项目名称:UUPMediaCreator,代码行数:41,代码来源:TakeOwn.cs

在TakeOwnDirectory()方法中,DirectoryInfo的代码示例类中的EnumerateFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

DirectoryInfo.EnumerateFiles的代码示例4 - AddNonMemberItems()

    using System.IO;

        /// 
        /// Add non-member items to the hierarchy
        /// 
        private void AddNonMemberItems()
        {
            string path;

            var ignoredFolders = this.FoldersToIgnore();

            // Get a list of the folders and files in the project folder excluding those that are hidden or not
            // wanted.  Hash sets are used so that we can do case-insensitive comparisons when excluding existing
            // project items from the lists.
            HashSet folders = new HashSet(Directory.EnumerateDirectories(
                this.ProjectFolder, "*", SearchOption.AllDirectories).Where(p =>
                {
                    if(ignoredFolders.Any(folder => p.StartsWith(folder, StringComparison.OrdinalIgnoreCase)))
                        return false;

                    DirectoryInfo di = new DirectoryInfo(p);

                    return !((di.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden);
                }).Select(p => PackageUtilities.MakeRelative(this.ProjectFolder, p)),
                StringComparer.OrdinalIgnoreCase);

            HashSet files = new HashSet(Directory.EnumerateFiles(
                this.ProjectFolder, "*", SearchOption.AllDirectories).Where(f =>
                {
                    if(ignoredFolders.Any(folder => f.StartsWith(folder, StringComparison.OrdinalIgnoreCase)))
                        return false;

                    FileInfo fi = new FileInfo(f);

                    return !((fi.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden);
                }).Select(f => PackageUtilities.MakeRelative(this.ProjectFolder, f)),
                StringComparer.OrdinalIgnoreCase);

            // Remove the folders and files that are already in the project
            foreach(Microsoft.Build.Evaluation.ProjectItem item in this.BuildProject.Items)
                if(folders.Count != 0 && item.ItemType.Equals(ProjectFileConstants.Folder,
                  StringComparison.OrdinalIgnoreCase))
                {
                    path = item.EvaluatedInclude;

                    // It should be relative already but check it just in case
                    if(Path.IsPathRooted(path)) 
                        path = PackageUtilities.MakeRelative(this.ProjectFolder, path);

                    if(folders.Contains(path))
                        folders.Remove(path);
                }
                else
                    if(files.Count != 0 && this.IsItemTypeFileType(item.ItemType))
                    {
                        path = item.EvaluatedInclude;

                        // It should be relative already but check it just in case
                        if(Path.IsPathRooted(path)) 
                            path = PackageUtilities.MakeRelative(this.ProjectFolder, path);

                        if(files.Contains(path))
                            files.Remove(path);
                    }

            // Add the remaining items to the project
            this.AddNonMemberFolderItems(folders);
            this.AddNonMemberFileItems(files);
        }
    

开发者ID:EWSoftware,项目名称:SHFB,代码行数:69,代码来源:ProjectNode.cs

在AddNonMemberItems()方法中,DirectoryInfo的代码示例类中的EnumerateFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

DirectoryInfo.EnumerateFiles的代码示例5 - GetFolderInfo()

    using System.IO;

        protected FolderInfo GetFolderInfo(string folderPath, int level = 0)
        {
            FolderInfo folderInfo = new FolderInfo(folderPath);

            if (settings.MaxDepthLevel == 0 || level < settings.MaxDepthLevel)
            {
                try
                {
                    DirectoryInfo currentDirectoryInfo = new DirectoryInfo(folderPath);

                    foreach (DirectoryInfo directoryInfo in currentDirectoryInfo.EnumerateDirectories())
                    {
                        if (settings.SkipHiddenFolders && directoryInfo.Attributes.HasFlag(FileAttributes.Hidden))
                        {
                            continue;
                        }

                        FolderInfo subFolderInfo = GetFolderInfo(directoryInfo.FullName, level + 1);
                        folderInfo.Folders.Add(subFolderInfo);
                        subFolderInfo.Parent = folderInfo;
                    }

                    foreach (FileInfo fileInfo in currentDirectoryInfo.EnumerateFiles())
                    {
                        if (settings.SkipHiddenFiles && fileInfo.Attributes.HasFlag(FileAttributes.Hidden))
                        {
                            continue;
                        }

                        folderInfo.Files.Add(fileInfo);
                    }

                    folderInfo.Files.Sort((x, y) => x.Name.CompareTo(y.Name));
                }
                catch (UnauthorizedAccessException)
                {
                }
            }

            return folderInfo;
        }
    

开发者ID:ShareX,项目名称:ShareX,代码行数:43,代码来源:Indexer.cs

在GetFolderInfo()方法中,DirectoryInfo的代码示例类中的EnumerateFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

DirectoryInfo.EnumerateFiles的代码示例6 - GetDataLinuxDirectory()

    using System.IO;

		private static bool GetDataLinuxDirectory(DirectoryInfo rootDiectory, out string dataPath, out string name)
		{
			foreach (FileInfo finfo in rootDiectory.EnumerateFiles())
			{
				if (finfo.Extension == x86Extension || finfo.Extension == x64Extension)
				{
					name = Path.GetFileNameWithoutExtension(finfo.Name);
					string dataFolder = $"{name}_{DataFolderName}";
					dataPath = Path.Combine(rootDiectory.FullName, dataFolder);
					if (DirectoryUtils.Exists(dataPath))
					{
						return true;
					}
				}
			}

			name = null;
			dataPath = null;
			return false;
		}
    

开发者ID:mafaca,项目名称:UtinyRipper,代码行数:22,代码来源:LinuxGameStructure.cs

在GetDataLinuxDirectory()方法中,DirectoryInfo的代码示例类中的EnumerateFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

DirectoryInfo.EnumerateFiles的代码示例7 - CollectWebFiles()

    using System.IO;

		private void CollectWebFiles(DirectoryInfo root, IDictionary files)
		{
			foreach (FileInfo levelFile in root.EnumerateFiles())
			{
				string extension = Path.GetExtension(levelFile.Name);
				switch(extension)
				{
					case WebGLGameStructure.DataExtension:
					case WebGLGameStructure.DataGzExtension:
						{
							string name = Path.GetFileNameWithoutExtension(levelFile.Name);
							AddFile(files, name, levelFile.FullName);
						}
						break;

					case WebGLGameStructure.UnityWebExtension:
						{
							if(levelFile.Name.EndsWith(WebGLGameStructure.DataWebExtension, StringComparison.Ordinal))
							{
								string name = levelFile.Name.Substring(0, levelFile.Name.Length - WebGLGameStructure.DataWebExtension.Length);
								AddFile(files, name, levelFile.FullName);
							}
						}
						break;
				}
			}
		}
    

开发者ID:mafaca,项目名称:UtinyRipper,代码行数:29,代码来源:MixedGameStructure.cs

在CollectWebFiles()方法中,DirectoryInfo的代码示例类中的EnumerateFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

DirectoryInfo.EnumerateFiles的代码示例8 - GetDataPCDirectory()

    using System.IO;

		private static bool GetDataPCDirectory(DirectoryInfo rootDiectory, out string dataPath, out string name)
		{
			name = null;
			int exeCount = 0;
			foreach (FileInfo finfo in rootDiectory.EnumerateFiles())
			{
				if (finfo.Extension == ExeExtension)
				{
					exeCount++;
					name = Path.GetFileNameWithoutExtension(finfo.Name);
					string dataFolder = $"{name}_{DataFolderName}";
					dataPath = Path.Combine(rootDiectory.FullName, dataFolder);
					if (DirectoryUtils.Exists(dataPath))
					{
						return true;
					}
				}
			}

			if (exeCount > 0)
			{
				name = exeCount == 1 ? name : rootDiectory.Name;
				dataPath = Path.Combine(rootDiectory.FullName, DataFolderName);
				if (DirectoryUtils.Exists(dataPath))
				{
					return true;
				}
			}

			name = null;
			dataPath = null;
			return false;
		}
    

开发者ID:mafaca,项目名称:UtinyRipper,代码行数:35,代码来源:PCGameStructure.cs

在GetDataPCDirectory()方法中,DirectoryInfo的代码示例类中的EnumerateFiles的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

本文中的DirectoryInfo.EnumerateFiles方法示例由csref.cn整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。