如何:访问、复制和移动文件
上次修改时间: 2010年4月12日
适用范围: SharePoint Foundation 2010
若要从网站中检索文件和文件夹,请使用 SPWeb 类的 GetFile() 或 GetFolder() 方法。
可以使用 SPWeb 类的 GetFolder 方法返回指定的文件夹,然后可以访问该文件夹中的单个文件。在实例化 SPWeb 对象之后(例如,实例化为 oWebsite),使用 SPFolder oFolder = oWebsite.GetFolder("Shared Documents")(在 Microsoft Visual Basic 中,使用 Dim oFolder As SPFolder = oWebsite.GetFolder("Shared Documents"))返回网站的 Shared Documents 文件夹。
示例
下面的示例返回文件夹中的文件集合并显示有关这些文件的信息。
Using oWebsite As SPWeb = New SPSite("https://Server/sites/SiteCollection").OpenWeb()
Dim folderUrl As String = "/Shared Documents/MySubFolder"
Dim oFolder As SPFolder = oWebsite.GetFolder(folderUrl)
Dim collFile As SPFileCollection = oFolder.Files
For Each oFile As SPFile In collFile
Label1.Text += ("<BR>Url: " & oFile.Url.ToString() & " Size: ") + oFile.Length.ToString()
Next
End Using
using (SPWeb oWebsite = new SPSite("https://Server/sites/SiteCollection").OpenWeb())
{
string folderUrl = "/Shared Documents/MySubFolder";
SPFolder oFolder = oWebsite.GetFolder(folderUrl);
SPFileCollection collFile = oFolder.Files;
foreach (SPFile oFile in collFile)
{
Label1.Text += "<BR>Url: " + oFile.Url.ToString() + " Size: " + oFile.Length.ToString();
}
}
前面的示例列出 Shared Documents 中每个文件的 URL 和大小。
该示例要求对 Microsoft.SharePoint 命名空间使用 using 指令(在 Visual Basic 中为 Imports)。
可以将文件加载到一般 List<T> 对象中以枚举集合。下面的示例将所有文件从当前网站的 Shared Documents 列表移动到名为 StorageFolder 的子文件夹,这将覆盖位于该文件夹中的具有相同名称的任何文件。
Dim oWebsite As SPWeb = SPContext.Current.Web
Dim oFolder As SPFolder = oWebsite.GetFolder("Shared Documents")
Dim collFile As SPFileCollection = oFolder.Files
'Copy the files to a generic List of type SPFile
Dim listFiles As New List(Of SPFile)(collFile.Count)
For Each oFile As SPFile In collFile
listFiles.Add(oFile)
Next
' Enumerate the List and move the files into the subfolder.
For Each moveFile As SPFile In listFiles
moveFile.MoveTo("Shared Documents/StorageFolder/" & moveFile.Name, True)
Next
SPWeb oWebsite = SPContext.Current.Web;
SPFolder oFolder = oWebsite.GetFolder("Shared Documents");
SPFileCollection collFile = oFolder.Files;
/*Copy the files to a generic List of type SPFile*/
List<SPFile> listFiles = new List<SPFile>(collFile.Count);
foreach (SPFile oFile in collFile)
{
listFiles.Add(oFile);
}
/* Enumerate the List and move the files into the subfolder.*/
foreach (SPFile moveFile in listFiles)
{
moveFile.MoveTo("Shared Documents/StorageFolder/" + moveFile.Name, true);
}
前面的示例要求对 Microsoft.SharePoint 和 System.Collections.Generic 命名空间使用 using 指令(在 Visual Basic 中为 Imports)。
若要将文件从一个位置复制到另一个位置,请使用 SPFile 类的 CopyTo() 方法之一。下面的示例枚举文档库中的所有文件夹和文件并将它们复制到另一文档库。Button_Click 事件处理程序循环访问顶级文件夹中的所有文件,列出大小超过用户在文本框中指定值的一倍的每个文件的名称和大小(以 KB 为单位),然后调用 CopyToTarget 方法以将文件复制到指定的文件夹。EnumerateFolder 方法然后以递归方式循环访问所有子文件夹并将每个文件集合传递到 CopyToTarget 方法。该示例假定存在一个按钮、一个标签和三个文本框,以便为操作指定文件大小、源文件夹和目标文件夹。
Private oWebsite As SPWeb
Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim fromFolder As String = TextBox3.Text
Dim oFolder As SPFolder = oWebsite.GetFolder(fromFolder)
Dim collFile As SPFileCollection = oFolder.Files
CopyToTarget(collFile)
Dim collFolder As SPFolderCollection = oFolder.SubFolders
EnumerateFolders(collFolder)
End Sub
Private Sub CopyToTarget(ByVal copyFiles As SPFileCollection)
Dim mySize As String = TextBox1.Text
Dim toFolder As String = TextBox2.Text
Dim maxLength As Integer = Convert.ToInt32(mySize)
For Each oFile As SPFile In copyFiles
If oFile.Length > maxLength * 1024 Then
Label1.Text += (SPEncode.HtmlEncode(oFile.Name) & ": ") + oFile.Length /1024 & "kb<BR>"
oFile.CopyTo((toFolder & "/") + oFile.Name, True)
End If
Next
End Sub
Private Sub EnumerateFolders(ByVal copyFolders As SPFolderCollection)
For Each subFolder As SPFolder In copyFolders
If subFolder.Name <> "Forms" Then
Dim subFiles As SPFileCollection = subFolder.Files
CopyToTarget(subFiles)
End If
Dim subFolders As SPFolderCollection = subFolder.SubFolders
EnumerateFolders(subFolders)
Next
End Sub
private SPWeb oWebsite;
protected void Button_Click(object sender, EventArgs e)
{
string fromFolder = TextBox3.Text;
SPFolder oFolder = oWebsite.GetFolder(fromFolder);
SPFileCollection collFile = oFolder.Files;
CopyToTarget(collFile);
SPFolderCollection collFolder = oFolder.SubFolders;
EnumerateFolders(collFolder);
}
private void CopyToTarget(SPFileCollection copyFiles)
{
string mySize = TextBox1.Text;
string toFolder = TextBox2.Text;
int maxLength = Convert.ToInt32(mySize);
foreach (SPFile oFile in copyFiles)
{
if (oFile.Length > maxLength * 1024)
{
Label1.Text += SPEncode.HtmlEncode(oFile.Name) + ": " + oFile.Length / 1024 + "kb<BR>";
oFile.CopyTo(toFolder + "/" + oFile.Name, true);
}
}
}
private void EnumerateFolders(SPFolderCollection copyFolders)
{
foreach (SPFolder subFolder in copyFolders)
{
if (subFolder.Name != "Forms")
{
SPFileCollection subFiles = subFolder.Files;
CopyToTarget(subFiles);
}
SPFolderCollection subFolders = subFolder.SubFolders;
EnumerateFolders(subFolders);
}
}
在该示例中,CopyTo 方法使用两个参数,其中一个参数指定已复制文件的目标 URL;而另一个参数为布尔值,它指定是否覆盖目标位置上具有相同名称的任何文件。
前面的示例要求对 Microsoft.SharePoint 和 Microsoft.SharePoint.Utilities 命名空间使用 using 指令(在 Visual Basic 中为 Imports)。