Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (2 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:49am
v2
Comments
Abhishek Sur 23-Sep-10 6:49am    
Use Proper formatting to post your code.

1 solution

I think you need to use self join based on your data in tblFiles.

ParentId will be on the same table where ID is. Create the hierarchy from the root and select each element which have parentId in common. :)
 
Share this answer
 
Comments
@nuraGGupta@ 23-Sep-10 6:59am    
this is exactly what i am doing, but the ids are somewhere mishaping after first 1 level, and hence the relation is getting useless.
Abhishek Sur 23-Sep-10 15:48pm    
Oh ... I think you need recursion in your query.
There are few examples of Union All recursion which might help you
http://www.eggheadcafe.com/software/aspnet/36213394/preventing-infinite-recursion.aspx

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