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;

        }

    }
}
9
  • 1
    This is a horrible question it hardly deserves an up vote. Jun 22, 2011 at 18:37
  • You have ommitted important code to be able to answer part 1. we need to see all of the GetFileSize(double) method. Second, in your DirSearch method, the second foreach loop the assumption would be there is a winforms control with a Text property called txtFile on your form that contains a filter for the files you wish to search for. The compiler seems to believe that control does not exist.
    – Bueller
    Jun 22, 2011 at 18:37
  • That was part of the code was from what I found earlier here - support.microsoft.com/kb/303974. I don't want lstFiles....lstFilesFound.Items.Add(f); this is what I ommitted.
    – Richell
    Jun 22, 2011 at 18:42
  • then what you are doing is only returning the size in the else if condition. since you do not have {} in place to signify you want to do nothing, the else if takes the next executable line as its code block. that means that the return only executes in the else if condition
    – Bueller
    Jun 22, 2011 at 18:45
  • @Comp - If you want help post any and all code you want help on. At this point what you have posted is not complete, undeclared variables, and syntax problems. Jun 22, 2011 at 18:48

3 Answers 3

1

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));
}
15
  • OH I see, thank you and I also added the MB and KB sizes according to the chart on that site. code } else if (byteCount >= 1048576.0) { size = String.Format("{0:##.##}", byteCount / 1048576.0) + " MB"; } else if (byteCount >= 1024.0) { size = String.Format("{0:##.##}", byteCount / 1024.0) + " KB"; } return size; }code
    – Richell
    Jun 22, 2011 at 19:21
  • @CompGeekess: looks much more correct to me now :) Did you get the part about the textbox control?
    – Bueller
    Jun 22, 2011 at 19:24
  • Yes, I went to my textbox control and renamed it to txtField. That error went away :). However, there are still just two tiny issues. One when I run the code it only picks up the folderBroweserDialog. When I select a drive, it outputs how many files are in there and closes. The one last thing is where the recursion occurs: code foreach (string d in Directory.GetDirectories(sDir)){ foreach (string f in Directory.GetFiles(d, txtFile.Text)){ code I have lstFilesFound.Items.Add(f); I honestly dont know what should go here as this is the recursion to get all files and folders, sub directories.
    – Richell
    Jun 22, 2011 at 19:32
  • @CompGeekess: The internal foreach loop is where you will do what you want to do with each individual file. Right now you are doing nothing. The f string variable will contain the path to a file contained in the directory. so you could for example write the path to the console with a line like console.Writeline(f); and it will process each directory recursively. (I suspect this is where you would get your file size but I am not really sure what your end goal is.
    – Bueller
    Jun 22, 2011 at 19:46
  • @Bueller- I want the program to generate a list (text file) of all the files from whatever path the user chooses (this includes network drives) - including all the sub directories below it. It needs to list the date of the file, the size and the file name with path.
    – Richell
    Jun 22, 2011 at 19:57
0

lstFilesFound isn't declared in the method using it and I can't see it in your code. txtFile isn't declared in the method using it, and like the other variable, I don't see it in another scope in your code. GetFileSize should include an else after the else if. It's required because of your use of if/else if prior to it.

1
0

The code in the following code block clearly does not return a value for the TRUE and FALSE statement.

In this context IF = TRUE else = FALSE of course I suggest using { and } on ALL IF statements until you have a general understanding of this fact.

    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;   
       }  

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

You never declared a variable called txtFile thus you cannot use it as a variable. My guess your trying to use a textbox control's Text property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.