严重的逻辑错误

本文关键字:错误 | 更新日期: 2023-09-27 18:19:30

我正在尝试下载网站的所有子网站中的所有文档库,下面给出了代码,但由于某些原因,它没有创建正确的目录结构,当我尝试调试时,在代码之后,我得到了一个异常,如下所示。

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);"行

但我很确定我的逻辑是不对的,也许

严重的逻辑错误

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;
    }