Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Windows
Hi,
 
I need to develop a windows application in C#, where I have to get all the hierarchy of folders, sub folders and files listed in a database in the order they are on the disk. The place where i am stuck up adding the same hierarchy to the database.I am not getting the exact parent and child relationship of the ids of folders and subfolders from my code. Please suggest.
 
My code is as follows :
 

public void PopulateTreeView(string directoryValue, TreeNode parentNode)
{
string[] directoryArray = Directory.GetDirectories(directoryValue);

try
{

if (directoryArray.Length != 0)
{
foreach (string directory in directoryArray)
{
substringDirectory = directory.Substring(directory.LastIndexOf('\\') + 1, directory.Length - directory.LastIndexOf('\\') - 1);
 
TreeNode myNode = new TreeNode(substringDirectory);
 
parentNode.Nodes.Add(myNode);
string path = Convert.ToString(directory.Remove(directory.LastIndexOf("\\")));
string[] files = Directory.GetFiles(directory);
string[] directories = Directory.GetDirectories(directory);
if (files.Length > 0)
{
collectFileInfo(ID,ParentID, directory);

}
else
if (directories.Length > 0)
{
collectFolderInfo(ID,ParentID, directory);

}
ParentID = ParentID + 1;
PopulateTreeView(directory, myNode);

}

}

}
catch (UnauthorizedAccessException)
{
parentNode.Nodes.Add("Access denied");
} // end catch
}
 
private void collectFileInfo(Int32 ID, Int32 ParentID, String dir)
{

String[] files = Directory.GetFiles(dir);
 
if (files.Length > 0)
{
foreach (String file in files)
{
//get details of each file using file object
FileInfo File1 = new FileInfo(file);
String fileName = File1.Name;
String filePath = File1.FullName;
String fileSize = File1.Length.ToString();
String fileExtension = File1.Extension;
String fileCreated = File1.LastWriteTime.ToString();
 
WriteToTable(ParentID, fileName, filePath, true);
 
}
}
else
{
// ParentID = ID;
}

}
private void collectFolderInfo(Int32 ID, Int32 ParentID, String dir)
{

String[] directories = Directory.GetDirectories(dir);
if (directories.Length > 0)
{
foreach (String Direc in directories)
{
DirectoryInfo dir1 = new DirectoryInfo(Direc);
string dirName = dir1.Name;
string path = dir1.FullName;
 
WriteToTable(ParentID, dirName, path, false);

}

}
else
{
// ParentID = ID;
}
}
 

private void WriteToTable(Int32 ParentID, string FileName,string FilePath,Boolean IsFile)
{

String conStr = "Data Source=.;Initial Catalog=DBNormalizerTest;Integrated Security=True";
SqlConnection conn = new SqlConnection(conStr);
SqlCommand cmd = new SqlCommand();
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Insert into tblFiles values(" + ID + "," + "'" + Convert.ToInt32(ParentID) + "'" + "," + "'" + FileName + "'" + "," + "'" + FilePath + "'" + "," + "'" + Convert.ToBoolean(IsFile) + "'" + ")";
cmd.ExecuteNonQuery();
conn.Close();
ID = ID + 1;

}
 

 

 
Anurag
Posted 21-Sep-10 19:17pm
Edited 23-Sep-10 0:03am
v3
Comments
Söderlund at 23-Sep-10 16:23pm
   
Aha,that's why you dont accept answers, you got more questions on your questions?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You Will find many Search result if you try to ask Google first rather then Here.
 
Take an Sample
 
http://www.dreamincode.net/code/snippet2591.ht[^]
 
Get Recursively,
http://www.java2s.com/Code/CSharp/GUI-Windows-Form/RecursivelyloadDirectoryinfointoTreeView.htm[^]
  Permalink  
v2
Comments
@nuraGGupt@ at 22-Sep-10 1:34am
   
Hello Hiren,
Try to understand the problem before replying. I am not asking for a tree view creation. This treeview is generated on the click of every subfolder. I need to iterate through each and every sub folder in the very first go. Pull out each folder, subfolder and files name in the same hierarchy. I have implemented and tried that points provided in your link way before. Anyways, thanks for your concern.
Hiren Solanki at 22-Sep-10 1:38am
   
sorry for my bad understanding, but you could use article upadated to my answer for recursively get all the directories and files, Hope you will like that.
@nuraGGupt@ at 22-Sep-10 1:39am
   
Posting my question again in different manner:
I need a (Recursive) function that can save a folder with all its sub folders ( , sub sub folders sub sub sub, etc all the way to the end) to a database. I need to know the folder name as well as position as I need this to re-create the folder structure from the database. I need to use an recursive function to drill down from level 1 to the botom.
@nuraGGupt@ at 22-Sep-10 4:21am
   
ok thanks, i will check that one out.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Since you havent posted any code i wont give you the whole solution.
 
What you need to do is to use recursion.
Write a method/function that calls itself like this.
void recursive_method(string Folder_To_Browse)
{
System.IO.DirectoryInfo DI = new System.IO.DirectoryInfo(Folder_To_Browse);
 
//IF you want to browse through the files in the folder.
//you can do DI.GetFiles(); here
foreach(System.IO.DirectoryInfo sub_folder in DI.GetDirectories())
{
//Calls itself until we are out of subfolders.
recursive_method(sub_folder.FullName);
}
}
 
This was written on the top of my head so it might contain errors.
But it should help get you started.
  Permalink  
v2
Comments
@nuraGGupt@ at 22-Sep-10 1:47am
   
Good.Gr8 thanks soderlund. I am happy to see this.This clicked right away to my mind. thanks for the help.
Söderlund at 22-Sep-10 3:53am
   
Yw,make a habit out of voting or accepting answer.
@nuraGGupt@ at 22-Sep-10 5:16am
   
I do have an habit of providing vote or accepting answer but when people talk in a bit of egoistic mood, it takes me time to come out of the complex. ;-)
Söderlund at 22-Sep-10 8:54am
   
Good,keep it up.
It´s nice to know if people are satisfied with answers given because i have posted
answers on already answered questions.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

To store them on a database you can use a self join table just like the classical employee - manager table
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

c# code
//i have a code to list all files and subfolders
string sourcePath = sourceFolderPath//enter your folder name
int numcount = 0;
int i = 0;
int f=0;
foreach (string fl in Directory.GetFiles(sourcePath))
{
filelist[f] = fl;//all files in parent directory
f++;
}
while (sourcePath != null)
{
//get all subfolders recursively
foreach (string drs in Directory.GetDirectories(sourcePath))
{
//get all files in subdirectories
foreach (string files in Directory.GetFiles(drs))
{
filelist[f] = files;
f++;
}
directoylist[i] = drs;//sub directories
 
i++;
}
sourcePath = directoylist[numcount];
numcount++;
}
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 23 Sep 2010
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