问题描述
我需要用C#开发一个Windows应用程序,在该应用程序中,我必须获取数据库中列出的文件夹,子文件夹和文件的所有层次结构,并按它们在磁盘上的顺序排列.我停留在向数据库添加相同层次结构的地方.我没有从我的代码中获得文件夹和子文件夹的ID的确切父子关系.请提出建议.
我的代码如下:
公共无效PopulateTreeView(字符串directoryValue,TreeNode parentNode)
{
string [] directoryArray = Directory.GetDirectories(directoryValue);
试试
{
如果(directoryArray.Length!= 0)
{
foreach(directoryArray中的字符串目录)
{
substringDirectory = directory.Substring(directory.LastIndexOf(''\\'')+ 1,directory.Length-directory.LastIndexOf(''\\'')-1);
TreeNode myNode =新的TreeNode(substringDirectory);
parentNode.Nodes.Add(myNode);
字符串路径= Convert.ToString(directory.Remove(directory.LastIndexOf("\\"))));
字符串[]文件= Directory.GetFiles(目录);
字符串[]目录= Directory.GetDirectories(目录);
如果(files.Length> 0)
{
collectFileInfo(ID,ParentID,目录);
}
其他
如果(directories.Length> 0)
{
collectFolderInfo(ID,ParentID,目录);
}
ParentID = ParentID +1;
PopulateTreeView(directory,myNode);
}
}
}
捕获(UnauthorizedAccessException)
{
parentNode.Nodes.Add(访问被拒绝");
}//结束捕获
}
私有无效collectFileInfo(Int32 ID,Int32 ParentID,String dir)
{
字符串[]文件= Directory.GetFiles(dir);
如果(files.Length> 0)
{
foreach(文件中的字符串文件)
{
//使用文件对象获取每个文件的详细信息
FileInfo File1 =新的FileInfo(文件);
字符串fileName = File1.Name;
字符串filePath = File1.FullName;
字符串fileSize = File1.Length.ToString();
字符串fileExtension = File1.Extension;
字符串fileCreated = File1.LastWriteTime.ToString();
WriteToTable(ParentID,fileName,filePath,true);
}
}
其他
{
//ParentID = ID;
}
}
私有无效collectFolderInfo(Int32 ID,Int32 ParentID,String dir)
{
String []目录= Directory.GetDirectories(dir);
如果(directories.Length> 0)
{
foreach(目录中的字符串Direc)
{
DirectoryInfo dir1 =新的DirectoryInfo(Direc);
字符串dirName = dir1.Name;
字符串路径= dir1.FullName;
WriteToTable(ParentID,dirName,path,false);
}
}
其他
{
//ParentID = ID;
}
}
私有无效WriteToTable(Int32 ParentID,字符串FileName,字符串FilePath,布尔值IsFile)
{
字符串conStr =数据源= .;初始目录= DBNormalizerTest;集成安全性= True";
SqlConnection conn =新的SqlConnection(conStr);
SqlCommand cmd =新的SqlCommand();
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText =插入tblFiles值(" + ID +," +""+ Convert.ToInt32(ParentID)+"" +," +''" + FileName +""+," +""+ FilePath +"" +," +""+ Convert.ToBoolean(IsFile)+"" +)";
cmd.ExecuteNonQuery();
conn.Close();
ID = ID +1;
}
Anurag
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
推荐答案
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.
这篇关于文件夹层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!