//load the sub files and Directories
//get the path Name
---------------------------------------------------------------------------
pirvate void Form1_Load(object sender,EventArgs e)
{
string exePath=Assembly.GetCurrentAssembly().Location;
string exeDirectoryPath=Path.GetDirectoryName(exePath);
string path=Path.Combine(exeDirectoryPath,"资料");
LoadAllFilesAndDirectories(path,treeview1.Nodes);
}
-------------------------------------------------------------------------
//call the function recursively
private void LoadAllFilesAndDirectories(string path ,TreeNodeCollection treeNodeCollection)
{
//1.get all Directories Names under the path
string[] allDirectoriesNames=Directory.GetDirectories(path);
//2.add the directories mumber
for(int i=0;i<allDirectoriesNames.Length;i++)
{
TreeNode tn=treeNodeCollection.Add(Path.GetFileName(allDirectoriesNames[i]));
LoadAllFilesAndDirectories(allDirectoriesNames[i],tn.Nodes); //此处调用递归,传递的参数 为每个要显示的路径,以及每个节点的子节点集合
}
//3.get all files Names under the path
string[] allFilesNames=Directory.GetFiles(path);
for(int i=0;i<allFilesNames.Length;i++)
{
treeNodeCollection.Add(allFilesNames[i]);
}
}
---------------------------------------------------------------------