locked
How can I use Directory.GetFiles to get all files in my computer? RRS feed

  • Question

  • I want to get all file in system to calculate md5 

    Is there any solution for this?

    Friday, August 2, 2019 3:21 AM

Answers

  • Hi LHVinh,

    Thank you for posting here.

    For your question, you want to  get all files in your computer.

    You could try the following code.

      static void Main(string[] args)
            {
                var allDrives = DriveInfo.GetDrives();
                List<string> list = new List<string>();
                foreach (DriveInfo d in allDrives)
                {
                   if(d.DriveType==DriveType.Fixed)
                    {
                        list.Add(d.Name);
                    }
                }
                var listfile = new List<string>();
                foreach (var item in list)
                {
                    GetAllFiles(item, listfile);
                }
    
         
    
    
            }
            static void GetAllFiles(string path, IList<string> files)
            {
                try
                {
                    Directory.GetFiles(path).ToList()
                        .ForEach(f => files.Add(f));
    
                    Directory.GetDirectories(path).ToList()
                        .ForEach(f => GetAllFiles(f, files));
                }
                catch (Exception ex)
                {
                    
                }
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, August 2, 2019 6:09 AM
  • Hi

    Thanks for the feedback.

    If you want to add all files to listbox, you could try the following code.

    private void Button1_Click(object sender, EventArgs e)
            {
                var allDrives = DriveInfo.GetDrives();
                List<string> list = new List<string>();
                foreach (DriveInfo d in allDrives)
                {
                    if (d.DriveType == DriveType.Fixed)
                    {
                        list.Add(d.Name);
                    }
                }
                var listfile = new List<string>();
                foreach (var item in list)
                {
                    GetAllFiles(item, listfile);
                }
                foreach (var item in listfile)
                {
                    listBox1.Items.Add(item);
                }
            }
            public void GetAllFiles(string path, IList<string> files)
            {
                try
                {
                    Directory.GetFiles(path).ToList()
                        .ForEach(f => files.Add(f));
    
                    Directory.GetDirectories(path).ToList()
                        .ForEach(f => GetAllFiles(f, files));
                }
                catch (Exception ex)
                {
                 
                }
            }

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, August 2, 2019 7:59 AM

All replies

  • To get all files from your system, you can recursively iterate through the directories. In each directory you can process (e.g. compute MD5) the containing files:

    private void ProcessDir(String directory)
            {
                var directoryInfo = new DirectoryInfo(directory);
                if (!directoryInfo.Exists)
                {
                    return;
                }
    
                var dirs = Directory.GetDirectories(directory);
                var files = Directory.GetFiles(directory);
    
                foreach (var d in dirs)
                {
                    ProcessDir(d);
                }
    
                foreach (var f in files)
                {
                    using (var md5 = MD5.Create())
                    {
                        using (var stream = File.OpenRead(f))
                        {
                            var hash = md5.ComputeHash(stream);
                        }
                    }
                }
            }

    Friday, August 2, 2019 5:28 AM
  • Hi LHVinh,

    Thank you for posting here.

    For your question, you want to  get all files in your computer.

    You could try the following code.

      static void Main(string[] args)
            {
                var allDrives = DriveInfo.GetDrives();
                List<string> list = new List<string>();
                foreach (DriveInfo d in allDrives)
                {
                   if(d.DriveType==DriveType.Fixed)
                    {
                        list.Add(d.Name);
                    }
                }
                var listfile = new List<string>();
                foreach (var item in list)
                {
                    GetAllFiles(item, listfile);
                }
    
         
    
    
            }
            static void GetAllFiles(string path, IList<string> files)
            {
                try
                {
                    Directory.GetFiles(path).ToList()
                        .ForEach(f => files.Add(f));
    
                    Directory.GetDirectories(path).ToList()
                        .ForEach(f => GetAllFiles(f, files));
                }
                catch (Exception ex)
                {
                    
                }
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, August 2, 2019 6:09 AM
  • how to add all files to listbox?
    Friday, August 2, 2019 7:52 AM
  • Hi

    Thanks for the feedback.

    If you want to add all files to listbox, you could try the following code.

    private void Button1_Click(object sender, EventArgs e)
            {
                var allDrives = DriveInfo.GetDrives();
                List<string> list = new List<string>();
                foreach (DriveInfo d in allDrives)
                {
                    if (d.DriveType == DriveType.Fixed)
                    {
                        list.Add(d.Name);
                    }
                }
                var listfile = new List<string>();
                foreach (var item in list)
                {
                    GetAllFiles(item, listfile);
                }
                foreach (var item in listfile)
                {
                    listBox1.Items.Add(item);
                }
            }
            public void GetAllFiles(string path, IList<string> files)
            {
                try
                {
                    Directory.GetFiles(path).ToList()
                        .ForEach(f => files.Add(f));
    
                    Directory.GetDirectories(path).ToList()
                        .ForEach(f => GetAllFiles(f, files));
                }
                catch (Exception ex)
                {
                 
                }
            }

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, August 2, 2019 7:59 AM
  • Thank you very much. This is the best solution for me.
    Friday, August 2, 2019 12:45 PM