List all files and directories in a directory + subdirectories

前端 未结 15 1875
陌清茗
陌清茗 2020-12-13 01:46

I want to list every file and directory contained in a directory and subdirectories of that directory. If I chose C:\\ as the directory, the program would get every name of

相关标签:
15条回答
  • 2020-12-13 02:37
    string[] allfiles = Directory.GetFiles("path/to/dir", "*.*", SearchOption.AllDirectories);
    

    where *.* is pattern to match files

    If the Directory is also needed you can go like this:

     foreach (var file in allfiles){
         FileInfo info = new FileInfo(file);
     // Do something with the Folder or just add them to a list via nameoflist.add();
     }
    
    0 讨论(0)
  • 2020-12-13 02:39

    You could use FindFirstFile which returns a handle and then recursively cal a function which calls FindNextFile.This is a good aproach as the structure referenced would be filled with various data such as alternativeName,lastTmeCreated,modified,attributes etc

    But as you use .net framework, you would have to enter the unmanaged area.

    0 讨论(0)
  • 2020-12-13 02:39

    With this you can just run them and chosse the sub folder when console run

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using data.Patcher; // The patcher XML
    namespace PatchBuilder
    {
    class Program
    {
        static void Main(string[] args)
        {
            string patchDir;
            if (args.Length == 0)
            {
                Console.WriteLine("Give the patch directory in argument");
                patchDir = Console.ReadLine();
            }
            else
            {
                patchDir = args[0];
            }
    
            if (File.Exists(Path.Combine(patchDir, "patch.xml")))
                File.Delete(Path.Combine(patchDir, "patch.xml"));
    
            var files = Directory.EnumerateFiles(patchDir, "*", SearchOption.AllDirectories).OrderBy(p => p).ToList();
    
            foreach (var file in files.Where(file => file.StartsWith("patch\\Resources")).ToArray())
            {
                files.Remove(file);
                files.Add(file);
            }
    
            var tasks = new List<MetaFileEntry>();
            using (var md5Hasher = MD5.Create())
            {
                for (int i = 0; i < files.Count; i++)
                {
                    var file = files[i];
    
                    if ((File.GetAttributes(file) & FileAttributes.Hidden) != 0)
                        continue;
    
                    var content = File.ReadAllBytes(file);
                    var md5Hasher2 = MD5.Create();
    
                    var task =
                        new MetaFileEntry
                        {
                            LocalURL = GetRelativePath(file, patchDir + "\\"),
                            RelativeURL = GetRelativePath(file, patchDir + "\\"),
                            FileMD5 = Convert.ToBase64String(md5Hasher2.ComputeHash(content)),
                            FileSize = content.Length,
                        };
    
                    md5Hasher2.Dispose();
    
                    var pathBytes = Encoding.UTF8.GetBytes(task.LocalURL.ToLower());
                    md5Hasher.TransformBlock(pathBytes, 0, pathBytes.Length, pathBytes, 0);
                    if (i == files.Count - 1)
                        md5Hasher.TransformFinalBlock(content, 0, content.Length);
                    else
                        md5Hasher.TransformBlock(content, 0, content.Length, content, 0);
    
                    tasks.Add(task);
                    Console.WriteLine(@"Add " + task.RelativeURL);
                }
    
                var patch = new MetaFile
                {
                    Tasks = tasks.ToArray(),
                    FolderChecksum = BitConverter.ToString(md5Hasher.Hash).Replace("-", "").ToLower(),
                };
    
    
                //XmlUtils.Serialize(Path.Combine(patchDir, "patch.xml"), patch);
                Console.WriteLine(@"Created Patch in {0} !", Path.Combine(patchDir, "patch.xml"));
            }
    
            Console.Read();
        }
    
        static string GetRelativePath(string fullPath, string relativeTo)
        {
            var foldersSplitted = fullPath.Split(new[] { relativeTo.Replace("/", "\\").Replace("\\\\", "\\") }, StringSplitOptions.RemoveEmptyEntries); // cut the source path and the "rest" of the path
    
            return foldersSplitted.Length > 0 ? foldersSplitted.Last() : ""; // return the "rest"
        }
    }
    }
    

    and this the patchar for XML export

    using System.Xml.Serialization;
    
    namespace data.Patcher
    {
        public class MetaFile
        {
    
            [XmlArray("Tasks")]
            public MetaFileEntry[] Tasks
            {
                get;
                set;
            }
    
            [XmlAttribute("checksum")]
            public string FolderChecksum
            {
                get;
                set;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题