令人讨厌的逻辑错误
我正在尝试下载网站所有子网站中的所有文档库,下面给出了代码,但由于某种原因,它没有创建正确的目录结构,当我尝试调试时,我得到了一个异常,在代码之后给出了下面的异常。
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;
}
尝试在操作系统加载程序锁内托管执行。不要尝试在 DllMain 或映像初始化函数内运行托管代码,因为这样做可能会导致应用程序挂起。
在线错误“dwnEachWeb(ChildWeb, CurrentPath, FolderName);”
但我很确定我的逻辑可能不正确
I am trying to download all the document library in all the subwebs of a web and code is given below, but for some reason its not creating a proper directory structure, when i tried to debug i got an exception which is given below , after code.
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;
}
Attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.
Error is on line , " dwnEachWeb(ChildWeb, CurrentPath, FolderName);"
but i am pretty sure my logic isn't right maybe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)