[方法] ファイルにアクセスする、ファイルをコピーおよび移動する

最終更新日: 2010年4月12日

適用対象: SharePoint Foundation 2010

SharePoint Online で使用可能

ファイルおよびフォルダーを Web サイトから取得するには、SPWeb クラスの GetFile() または GetFolder() メソッドを使用します。

SPWeb クラスの GetFolder メソッドを使用すると、指定したフォルダーを返すことができ、次にフォルダー内の個別ファイルにアクセスできます。SPWeb オブジェクト (たとえば、oWebsite) をインスタンス化した後、SPFolder oFolder = oWebsite.GetFolder("Shared Documents" (Microsoft Visual Basic では Dim oFolder As SPFolder = oWebsite.GetFolder("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();
    } 
}

この例では、共有ドキュメント内のすべてのファイルについて、その URL とサイズを一覧で表示します。

この例には、Microsoft.SharePoint 名前空間の using ディレクティブ (Visual Basic では Imports) が必要です。

一般的 List<T> オブジェクトにファイルを読み込んで、コレクションを列挙できます。次の例では、現在のサイトの共有ドキュメント リストからすべてのファイルを 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 メソッドに渡します。この例では、ボタン、ラベル、および 3 つのテキスト ボックスがあり、ファイル サイズ、ソース フォルダー、およびターゲット フォルダーを指定していることを前提としています。

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 メソッドで 2 つのパラメーターを使用します。1 つは、コピーされたファイルのコピー先 URL を指定し、もう 1 つのパラメーターは、コピー先に同名のファイルがある場合に上書きするかどうかを指定するブール値です。

前の例では、Microsoft.SharePoint および Microsoft.SharePoint.Utilities 名前空間の using ディレクティブ (Visual Basic の場合は Imports) が必要です。

関連項目

参照

Microsoft.SharePoint

概念

リストのオブジェクトとコレクションを操作する

Visual Studio を使用して SharePoint 開発を行う

データ更新のためのセキュリティ検証と投稿の作成

権限の昇格