using (SPSite site = new SPSite("Your Site URL"))
{
using (SPWeb web = site.OpenWeb())
{
if (web != null)
{
SPList list = web.Lists["CPR"];
SPQuery spQuery = new SPQuery();
SPListItemCollection items = web.Lists["CPR"].GetItems(spQuery);
//SPListItemCollection spListItemCollection = _objweb.Lists["CPR"].GetItems(spQuery);
if (items.Count > 0)
{
//This will give your all the folders in root folder
foreach (SPFolder folder in list.RootFolder.SubFolders)
{
//This is the default folder which is not visible so skip this
if (folder.Name.ToLower() != "_w")
{
SPQuery query = new SPQuery();
query.Folder = folder;
SPListItemCollection listitem = list.GetItems(query);
if (listitem.Count > 0)
{
//This will return you the current folder name
string Name = folder.Name;
//This will return you the total number of items in that folder
int count = folder.ItemCount;
for (int i = 0; i < count; i++)
{
// SPFolder subFolder = list.RootFolder.SubFolders[i];
SPFolderCollection subFoldersOfSubFolder = folder.SubFolders;
if (subFoldersOfSubFolder.Count > 0)
{
for (int j = 0; j < subFoldersOfSubFolder.Count; j++)
{
SPFolder specificSubFolder = subFoldersOfSubFolder[j];
/*
At this point you could use properties like Name,
ServerRelativeUrl or UniqueId of the SubFolder class to get
the information you need.
*/
if (specificSubFolder.Name == "Unfunded Report")
{
SPFolderCollection subFoldersUnfunded = specificSubFolder.SubFolders;
for (int k = 0; k < subFoldersUnfunded.Count; k++)
{
SPFolder specificProfitCenterfolders = subFoldersUnfunded[k];
if (specificProfitCenterfolders.Name == "USBL")
{
SPFolderCollection subFoldersfyear = specificProfitCenterfolders.SubFolders;
for (int l = 0; l < subFoldersfyear.Count; l++)
{
SPFolder fyear = subFoldersfyear[l];
if (fyear.Name == "FY 2017")
{
SPFileCollection fileitems = fyear.Files;
foreach (SPFile file in fileitems)
{
string filename = file.Name;
string URl = file.Url;
}
}
}
}
}
}
}
}
else
{
//If you get to here, it means that the sub-folder had no sub-folders
}
}
//Thi will return you the Title of the first Item. You Can also use loop here to iterate through all items
string Title = listitem[0]["Title"].ToString();
if (Title == "Unfunded Report")
{
//SPFolder subFolder = list.RootFolder.SubFolder[i];
SPFolder subFolder = list.RootFolder.SubFolders[0];
SPFolderCollection subFoldersOfSubFolder = subFolder.SubFolders;
if (subFoldersOfSubFolder.Count > 0)
{
for (int j = 0; j < subFoldersOfSubFolder.Count; j++)
{
SPFolder specificSubFolder = subFoldersOfSubFolder[j];
/*
At this point you could use properties like Name,
ServerRelativeUrl or UniqueId of the SubFolder class to get
the information you need.
*/
}
}
else
{
//If you get to here, it means that the sub-folder had no sub-folders
}
}
}
}
}
}
}
}
}
}