在spweb中下载列表时出错

本文关键字:出错 列表 下载 spweb | 更新日期: 2023-09-27 18:18:53

代码

private void downloadList(SPObjectData objectData)
    {
        using (SPWeb currentWeb = objectData.Web)
        {
            foreach (SPList list in currentWeb.Lists)
            {
                    foreach (SPFolder oFolder in list.Folders)
                    {
                        if (oFolder != null)
                        {
                            foreach (SPFile file in oFolder.files)
                            {
                                if (CreateDirectoryStructure(tbDirectory.Text, file.Url))
                                {
                                    var filepath = System.IO.Path.Combine(tbDirectory.Text, file.Url);
                                    byte[] binFile = file.OpenBinary();
                                    System.IO.FileStream fstream = System.IO.File.Create(filepath);
                                    fstream.Write(binFile, 0, binFile.Length);
                                    fstream.Close();
                                }
                            }
                        }
                }
            }
        }
    }

编译时出错

错误无法强制转换类型为"Microsoft.SharePoint"的对象。SPListItem'输入'Microsoft.SharePoint.SPFolder'.

foreach (SPFolder oFolder in list.Folders)出错

我正在尝试在列表中分配一个文件夹。文件夹到文件夹,但由于某种原因,它给出了上面提到的错误。

我试图从列表中获取文件夹,但在阅读下面给出的链接上的文件夹和列表对象之间的差异后,我更改了下面提到的代码,干杯

输入链接描述

private void downloadList(SPObjectData objectData)
    {
        using (SPWeb currentWeb = objectData.Web)
        {
            foreach (SPFolder oFolder in currentWeb.Folders)
            {
                if (oFolder != null)
                {
                    foreach (SPFile file in oFolder.Files)
                    {
                        if (CreateDirectoryStructure(tbDirectory.Text, file.Url))
                        {
                            var filepath = System.IO.Path.Combine(tbDirectory.Text, file.Url);
                            byte[] binFile = file.OpenBinary();
                            System.IO.FileStream fstream = System.IO.File.Create(filepath);
                            fstream.Write(binFile, 0, binFile.Length);
                            fstream.Close();
                        }
                    }
                }
            }
        }
    }

在spweb中下载列表时出错

try

foreach (SPFile file in oFolder.Files)

更新:如果有问题,只需索引到它:

SPFileCollection collFiles = oFolder.Files;
        long lngTotalFileSize = 0;
        for (int intIndex = 0; intIndex < collFiles.Count; intIndex++)
        {
            lngTotalFileSize += collFiles[intIndex].Length;
        }

HTH