Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hey guys,

i am making a small program just for experience on how to do this, this is a console program. i just need something that looks like this:

--Folders--
folder1
folder2
folder3

--Files--
file1
file2
file3


i am not really sure how to even get started on this. is someone able to give me some source code please? thanks!
Posted

C#
DirectoryInfo di = new DirectoryInfo("A:\\");
var directories= di.GetFiles("*", SearchOption.AllDirectories);

foreach (FileInfo d in directories)
{
       //Add files to a list so that later they can be compared to see if each file
       // needs to be copid or not
}

try above code

more at get-all-files-and-directories-in-specific-path-fast[^]
 
Share this answer
 
Take a look at the File Class[^] and Directory Class[^]
Here are some sample articles
Working With File Class in C#[^]
Working with Directory Class in .Net (C#)[^]
Directory and DirectoryInfo Classes in C#
[^]
C# FileInfo[^]
This should help you you to get started.
 
Share this answer
 
Hi,
Try this code,
C#
static void Main(string[] args)
{
     string location = @"D:\Your_Location";
     Console.WriteLine("Directory: " + location);
     System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(location);
     FullDirList(dir, "*");
     Console.ReadKey();
}
static void FullDirList(DirectoryInfo dir, string patternToSearch)
{
     Console.WriteLine("");
     Console.WriteLine(dir.Name);
     Console.WriteLine("********");
     try
     {
          foreach (FileInfo f in dir.GetFiles(patternToSearch))
          {
               Console.WriteLine(f.Name);
          }
     }
     catch
     {
           Console.WriteLine("Directory {0}  \n could not be accessed!!!!", dir.FullName);
           return;
     }
     foreach (DirectoryInfo d in dir.GetDirectories())
     {
           FullDirList(d, patternToSearch);
     }
}

This will show all the folders and files in your given directory. This will show subdirectory and their files as well.

I hope this will help.
Thanks :)
 
Share this answer
 
v2
Comments
Member 8378691 18-Dec-12 4:14am    
sorry, if i put in the address 'D:\' and i got everything. all i wanted were the names of the files and folders in D, not every file and every folder in D
Sk. Tajbir 18-Dec-12 5:30am    
Try this..Just don't call recursive function again,
static void Main(string[] args)
{
string location = @"D:\Image_Shares_FCI";
Console.WriteLine("Directory: " + location);
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(location);
FullDirList(dir, "*");
Console.ReadKey();
}
static void FullDirList(DirectoryInfo dir, string searchPattern)
{
Console.WriteLine("Folders:");

foreach (DirectoryInfo d in dir.GetDirectories())
{
Console.WriteLine(d.FullName);
}
Console.WriteLine("********");
Console.WriteLine("Files:");
foreach (FileInfo f in dir.GetFiles(searchPattern))
{
Console.WriteLine(f.Name);
}
}
Member 8378691 19-Dec-12 19:32pm    
this looks like a good piece of code, but i'm happy what what i have. it works and that's all that matters at the moment.
Member 8378691 19-Dec-12 19:32pm    
wait, i think i might have done something wrong, just a sec...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900