Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 17-Dec-12 21:27pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
Try this code,
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 Smile | :)
  Permalink  
v2
Comments
Member 8378691 at 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 at 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 at 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 at 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)

  Print Answers RSS
0 CPallini 375
1 Sergey Alexandrovich Kryukov 370
2 BillWoodruff 314
3 George Jonsson 274
4 OriginalGriff 257
0 OriginalGriff 5,030
1 CPallini 4,115
2 Sergey Alexandrovich Kryukov 3,554
3 George Jonsson 2,826
4 Gihan Liyanage 2,386


Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 18 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100