在数据库中保存文件夹层次结构 [英] Saving folder hierarchy in database

查看:110
本文介绍了在数据库中保存文件夹层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要用C#开发一个Windows应用程序,在该应用程序中,我必须获取数据库中列出的文件夹,子文件夹和文件的所有层次结构,并按它们在磁盘上的顺序排列.我停留在向数据库添加相同层次结构的地方.我没有从我的代码中获得文件夹和子文件夹的ID的确切父子关系.请提出建议.

我的代码如下:


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






Anurag

推荐答案

我认为您需要根据tblFiles中的数据使用自连接.

ParentId将位于ID所在​​的同一表上.从根目录创建层次结构,并选择每个具有parentId共同的元素. :)
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. :)


这篇关于在数据库中保存文件夹层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆