- 遍历所有网站和列表
1 SPSite oSiteCollection = SPContext.Current.Site; 2 SPWebCollection collWebsite = oSiteCollection.AllWebs; 3 4 for (int i = 0; i < collWebsite.Count; i++) 5 { 6 using (SPWeb oWebsite = collWebsite[i]) 7 { 8 SPListCollection collList = oWebsite.Lists; 9 10 for (int j = 0; j < collList.Count; j++) 11 { 12 Label1.Text += SPEncode.HtmlEncode(collWebsite[i].Title) + " " 13 + SPEncode.HtmlEncode(collList[j].Title) + "<BR>"; 14 } 15 } 16 }
- 遍历一个文件夹下的所有文件
1 using (SPWeb oWebsite = new SPSite("http://Server/sites/SiteCollection").OpenWeb()) 2 { 3 string folderUrl = "/Shared Documents/MySubFolder"; 4 SPFolder oFolder = oWebsite.GetFolder(folderUrl); 5 SPFileCollection collFile = oFolder.Files; 6 7 foreach (SPFile oFile in collFile) 8 { 9 Label1.Text += "<BR>Url: " + oFile.Url.ToString() + " Size: " + oFile.Length.ToString(); 10 } 11 }
- 移动文件
1 SPWeb oWebsite = SPContext.Current.Web; 2 SPFolder oFolder = oWebsite.GetFolder("Shared Documents"); 3 SPFileCollection collFile = oFolder.Files; 4 5 6 List<SPFile> listFiles = new List<SPFile>(collFile.Count); 7 8 foreach (SPFile oFile in collFile) 9 { 10 listFiles.Add(oFile); 11 } 12 13 14 foreach (SPFile moveFile in listFiles) 15 { 16 moveFile.MoveTo("Shared Documents/StorageFolder/" + moveFile.Name, true); 17 }