Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6051971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:52:34+00:00 2026-05-23T07:52:34+00:00

I have a program where the user should be able to locate any folder

  • 0

I have a program where the user should be able to locate any folder and the program will return the files/folders (sub directories) within the path that was selected, as well as the date and size of each file/folder.

I used the folderBrowserDialog to allow the system to be searched, including networks (most every where else I was looking ONLY returned the C:\, but we have more than just this. The folderBrowserDialog allows this to happen. Earlier I thought I found some code that would help me futher with what I am trying to accomplish, but it turned out to be what I don’t need, at least it was a learning experience, except for the recursion process.

I believe I have the code for the size of the files and directories as well as the date time. I found those size and date.

At the moment when I build the code I am given two error messages

1. 'DD.Form1.GetFileSize(double)' not all code paths return a value. 

2. The name 'txtFile' does not exist in the current context 

and only the first part of the code runs (with the folderBroweserDialog), the textbox isn’t working and I would like for the user to enter the path and it search for the path and bring up the same data – Folder/File names and paths including subdirectories along with date and size of all. What do I need to do to get this working properly? Thank You

Here is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DD
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void browse_Click(object sender, EventArgs e)
        {
            //
            // This event handler was created by double-clicking the window in the designer.
            // It runs on the program's startup routine.
            //
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                //
                // The user selected a folder and pressed the OK button.
                // We print the number of files found.
                //
                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
            }

            //Obtaining the sub directories in a folder
        }

        void DirSearch(string sDir)
        {
            try
            {
                foreach (string d in Directory.GetDirectories(sDir))
                {
                    foreach (string f in Directory.GetFiles(d, txtFile.Text))
                    {

                    }
                    DirSearch(d);
                }
            }
            catch (System.Exception excpt)
            {
                Console.WriteLine(excpt.Message);
            }
            {
                //Obtaining the date and time of a file
                // Write file containing the date with BIN extension
                //

                string n = string.Format("text-{0:yyyy-MM-dd_hh-mm-ss-tt}.bin",
        DateTime.Now);
                File.WriteAllText(n, "aaa");
            }
            //Obtaining the size of a file
        }
        private string GetFileSize(double byteCount)
        {
            string size = "0 Bytes";
            if (byteCount >= 1073741824.0)
                size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB";
            else if (byteCount >= 1048576.0)
                //etc...     

                return size;

        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:52:34+00:00Added an answer on May 23, 2026 at 7:52 am

    Ok, I have taken a quick look at the article you referenced and here are some comments that may help out.

    First lets take a look at

    private string GetFileSize(double byteCount)         
    {
                 string size = "0 Bytes";
                 if (byteCount >= 1073741824.0)
                     size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB";
                 else if (byteCount >= 1048576.0)
                     //etc...
                 return size;
    } 
    

    What this should look like is something more like this

    private string GetFileSize(double byteCount)
    {
            string size = "0 Bytes";
            if (byteCount > = 1073741824.0)
            {
               size = String.Format("{0:##.##}", byteCount / 1073741824.0) + " GB";
            }
            else if (byteCount >= 1048576.0)
            {
                //do something else in here
            }
            return size;
    }
    

    You do not have to use the braces in the if/else statement to deliniate your blocks of code to execute if it is only a single line, I have included here for clarity. The C# compiler will associate the next executable line of code after and if or else with that if or else unless you use the {} to indicate the block or end the if or else line itself with a “;”

    The next problem you are having, is with the textFile.Text reference in the foreach loop. This is most likely a Textbox control on the form in the example. The parameter that you are filling in there is a filter for the type of files you are searching for. So you most likely do not have a Textbox on your form that has the name textFile.

    Edit:
    There is no one line command to get a list of all files recursively in a directory structure built in to the .NET framework. So you have the method below to do that for you. The way you would use this to have a single list of all the paths would be as follows.

    List<FileInfo> DirSearch(string sDir)
         {
             List<FileInfo> filesList = new List<FileInfo>();
             try
             {
                 foreach (string d in Directory.GetDirectories(sDir))
                 {
                     foreach (string f in Directory.GetFiles(d, txtFile.Text))
                     {
                        fileList.Add(new FileInfo(f));
                      }
                     DirSearch(d);
                 }
             }
             catch (System.Exception excpt)
             {
                 Console.WriteLine(excpt.Message);
             }
             return fileList;
         } 
    

    Now you can print out the details of each of those files by something like this.

    foreach (FileInfo fi in new DirSearch("c:\"))
    {
         console.Writeline(String.Format("Filename: {0}   Size:  {1}", fi.Name, fi.Length));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have writted a Java program that will be able to add customers to
I have a program that creates a Windows user account using the NetUserAdd() API
I have a program in which the user adds multiple objects to a scene.
I have a program that allows the user to enter a level number, and
I have a program in which a user selects a row in a Datagrid
I have a program that needs to run as a separate NT user to
I have a program that needs to run as a normal user most of
I have a single user java program that I would like to have store
I have a Win32 C++ program that validates user input and updates the UI
I have a console program and I want if the user press ctrl-z the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.