我正在尝试下载Web的所有子网站中的所有文档库,下面给出了代码,但由于某种原因,它没有创建正确的目录结构,当我尝试调试时,我得到了一个异常,下面给出,在代码之后。
private void bFolder_Click(object sender, EventArgs e)
{
TreeNode currentNode = tvSource.SelectedNode;
SPObjectData objectData = (SPObjectData)currentNode.Tag;
using (SPWeb TopLevelWeb = objectData.Web)
{
string CurrentPath = tbDirectory.Text;
string FolderName = TopLevelWeb.Name;
dwnEachWeb(TopLevelWeb, CurrentPath, FolderName);
}
}
private void dwnEachWeb(SPWeb TopLevelWeb, string CurrentPath, string FolderName)
{
if (TopLevelWeb != null)
{
if (TopLevelWeb.Webs != null)
{
CreateDirectoryStructure(CurrentPath, FolderName);
dwnEachList(TopLevelWeb, CurrentPath, FolderName);
foreach (SPWeb ChildWeb in TopLevelWeb.Webs)
{
FolderName = ChildWeb.Name;
CurrentPath = CurrentPath + "\\" + FolderName;
dwnEachWeb(ChildWeb, CurrentPath, FolderName);
ChildWeb.Dispose();
}
}
}
}
private void dwnEachList(SPWeb oWeb, string CurrentPath, string FolderName)
{
foreach (SPList oList in oWeb.Lists)
{
if (oList is SPDocumentLibrary && !oList.Hidden)
{
CurrentPath = CurrentPath + "//" + oList.Title;
FolderName = oList.Title;
CreateDirectoryStructure(CurrentPath, FolderName);
dwnEachFile(oList.RootFolder, CurrentPath, FolderName);
}
}
}
/*private void dwnEachFolder(SPFolder oFolder)
{
if (oFolder.SubFolders.Count == 0)
{
dwnEachFile(oFolder);
}
foreach (SPFolder SubFolder in oFolder.SubFolders)
{
dwnEachFolder(SubFolder);
}
}*/
private void dwnEachFile(SPFolder oFolder, string CurrentPath, string FolderName)
{
if (oFolder.Files.Count != 0)
{
foreach (SPFile ofile in oFolder.Files)
{
if (CreateDirectoryStructure(CurrentPath, ofile.Url))
{
var filepath = System.IO.Path.Combine(CurrentPath, ofile.Url);
byte[] binFile = ofile.OpenBinary();
System.IO.FileStream fstream = System.IO.File.Create(filepath);
fstream.Write(binFile, 0, binFile.Length);
fstream.Close();
}
}
}
}
//creating directory
private bool CreateDirectoryStructure(string baseFolder, string filepath)
{
if (!Directory.Exists(baseFolder)) return false;
var paths = filepath.Split('/');
for (var i = 0; i < paths.Length - 1; i++)
{
baseFolder = System.IO.Path.Combine(baseFolder, paths[i]);
Directory.CreateDirectory(baseFolder);
}
return true;
}
在OS Loader锁中尝试托管执行。不要尝试在DllMain或图像初始化函数中运行托管代码,因为这样做会导致应用程序挂起。
错误在线,“dwnEachWeb(ChildWeb,CurrentPath,FolderName);”
但我很确定我的逻辑可能不正确
答案 0 :(得分:0)
private void bFolder_Click(object sender, EventArgs e)
{
TreeNode currentNode = tvSource.SelectedNode;
SPObjectData objectData = (SPObjectData)currentNode.Tag;
using (SPWeb TopLevelWeb = objectData.Web)
{
dwnEachWeb(TopLevelWeb, TopLevelWeb.Title, tbDirectory.Text);
}
}
private void dwnEachWeb(SPWeb TopLevelWeb, string FolderName, string CurrentDirectory)
{
if (TopLevelWeb != null)
{
if (TopLevelWeb.Webs != null)
{
CurrentDirectory = CurrentDirectory + "//" + TopLevelWeb.Title;
CreateFolder(CurrentDirectory);
foreach (SPWeb ChildWeb in TopLevelWeb.Webs)
{
dwnEachWeb(ChildWeb, ChildWeb.Title, CurrentDirectory);
ChildWeb.Dispose();
}
dwnEachList(TopLevelWeb, CurrentDirectory);
//dwnEachList(TopLevelWeb, FolderName, CurrentDirectory);
}
}
}
private void dwnEachList(SPWeb oWeb, string CurrentDirectory)
{
foreach (SPList oList in oWeb.Lists)
{
if (oList is SPDocumentLibrary && !oList.Hidden)
{
dwnEachFile(oList.RootFolder, CurrentDirectory);
}
}
}
private void dwnEachFile(SPFolder oFolder, string CurrentDirectory)
{
if (oFolder.Files.Count != 0)
{
CurrentDirectory = CurrentDirectory + "//" + oFolder.Name;
CreateFolder(CurrentDirectory);
foreach (SPFile ofile in oFolder.Files)
{
if (CreateDirectoryStructure(CurrentDirectory, ofile.Url))
{
var filepath = System.IO.Path.Combine(CurrentDirectory, ofile.Url);
byte[] binFile = ofile.OpenBinary();
System.IO.FileStream fstream = System.IO.File.Create(filepath);
fstream.Write(binFile, 0, binFile.Length);
fstream.Close();
}
}
}
}
//creating directory where files will be download
private bool CreateDirectoryStructure(string baseFolder, string filepath)
{
if (!Directory.Exists(baseFolder)) return false;
var paths = filepath.Split('/');
for (var i = 0; i < paths.Length - 1; i++)
{
baseFolder = System.IO.Path.Combine(baseFolder, paths[i]);
Directory.CreateDirectory(baseFolder);
}
return true;
}
//creating folders
private bool CreateFolder(string CurrentDirectory)
{
if (!Directory.Exists(CurrentDirectory))
{
Directory.CreateDirectory(CurrentDirectory);
}
return true;
}