在spweb中下载列表时出错

时间:2011-12-28 15:00:28

标签: c# .net sharepoint

这是代码

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)

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

我试图从列表中获取文件夹,但在阅读下面给出的链接上的文件夹和列表对象之间的差异后,我改变了我的代码,如下所述,欢呼

enter link description here

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

1 个答案:

答案 0 :(得分:2)

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