Domanda

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

È stato utile?

Soluzione

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;
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top