Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
2.67/5 (3 votes)
See more:
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
Updated 23-Sep-10 0:03am
v3
Comments
Per Söderlund 23-Sep-10 16:23pm    
Aha,that's why you dont accept answers, you got more questions on your questions?

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[^]
 
Share this answer
 
v2
Comments
Hiren solanki 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.
@nuraGGupta@ 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.
@nuraGGupta@ 22-Sep-10 4:21am    
ok thanks, i will check that one out.
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++;
}
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
@nuraGGupta@ 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.
Per Söderlund 22-Sep-10 3:53am    
Yw,make a habit out of voting or accepting answer.
Per Söderlund 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.
To store them on a database you can use a self join table just like the classical employee - manager table
 
Share this answer
 

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