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)
{
Co开发者_JAVA技巧nsole.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;
}
}
}
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));
}
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.
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.
精彩评论