Using the following code:
使用以下代码:
using (SPSite site = new SPSite("http://localhost/"))
{
using (SPWeb web = site.OpenWeb())
{
foreach (SPList list in web.Lists)
{
if (list.OnQuickLaunch)
{
Console.WriteLine(list.Title);
foreach (SPListItem item in list.Folders)
{
Console.WriteLine("- " + item.Title);
}
}
}
}
}
and the output:
和输出:
... various lists ...
Shared Documents
- Minutes
- Second Level
I get all the folders back as a flat list - no real indication of the nesting that can happen as a folder is created as a child of another folder. Spelunking around with Visual Studio I can see a few interesting properties that might give me some clues (like item.Url and counting / characters or item.Folder.ParentFolder compared against something?), but there has got to be a simpler way.
我将所有文件夹作为一个平面列表返回 - 没有真正指示嵌套可能发生在文件夹被创建为另一个文件夹的子项时。使用Visual Studio进行处理我可以看到一些有趣的属性可能会给我一些线索(例如item.Url和count / characters或item.Folder.ParentFolder与某些东西进行比较?),但必须有一个更简单的方法。
Thanks!
2 个解决方案
#1
You can traverse the folder hierarchy from the root folder of any list, i.e. list.RootFolder. Having the root SPFolder object, you can start traversing the hierarchy recursively using the SubFolders property on each SPFolder object.
您可以从任何列表的根文件夹遍历文件夹层次结构,即list.RootFolder。拥有根SPFolder对象后,可以使用每个SPFolder对象上的SubFolders属性以递归方式开始遍历层次结构。
#2
You may like to check out the following question for answers. While it is not the same question, the underlying problem is similar.
您可以查看以下问题以获得答案。虽然这不是同一个问题,但潜在的问题是类似的。
Should I create my own object model.
我应该创建自己的对象模型。
#1
You can traverse the folder hierarchy from the root folder of any list, i.e. list.RootFolder. Having the root SPFolder object, you can start traversing the hierarchy recursively using the SubFolders property on each SPFolder object.
您可以从任何列表的根文件夹遍历文件夹层次结构,即list.RootFolder。拥有根SPFolder对象后,可以使用每个SPFolder对象上的SubFolders属性以递归方式开始遍历层次结构。
#2
You may like to check out the following question for answers. While it is not the same question, the underlying problem is similar.
您可以查看以下问题以获得答案。虽然这不是同一个问题,但潜在的问题是类似的。
Should I create my own object model.
我应该创建自己的对象模型。