Move, copy, create, or delete a file or folder (Windows Runtime apps using C#/VB and XAML)

You can move, copy, create, or delete a user's files or folders on Microsoft OneDrive, from your Windows Runtime apps using C#/VB and XAML.

In this article
Prerequisites
Move or copy a file or folder
Delete a file
Create a folder
Delete a folder

Prerequisites

The user must be signed in with a Microsoft account. To learn how to sign users in from your app, see Signing users in (XAML).

Important

You can't create a file in OneDrive. You must create a file, and then upload the file to OneDrive.

Move or copy a file or folder

In this example, one of the MoveAsync methods is used to move the file. The MoveAsync method determines whether the call was successful, and if so, displays a confirmation message.

To copy a file, call one of the CopyAsync methods instead of MoveAsync.

Note

For move operations, the source can be anything except the root, and the destination must be the folder to which the item will be moved. The destination of a move or copy operation must be a folder. Folders themselves cannot be copied. Also, OneDrive storage is structured so that move and copy operations cannot occur between the OneDrive folder hierarchies of different users. For example, even if user A can read user B's files, user A cannot copy or move them to his or her own OneDrive folders.

private async void btnMoveFile_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient moveFileClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await moveFileClient.MoveAsync("file.9d24497f7fef8f33.9D24497F7FEF8F33!748",
                "folder.9d24497f7fef8f33.9D24497F7FEF8F33!265");
        this.infoTextBlock.Text = "File moved.";
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error moving file: " + exception.Message;
    }
}

Delete a file

To remove a file, photo, video, or audio, use code like this. The wl.skydrive_update scope is required.

private async void btnDeleteFolder_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.DeleteAsync("folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!147");
        this.infoTextBlock.Text = "Folder deleted.";
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error deleting folder: " + exception.Message;
    }
}

Create a folder

To add a new folder, use code like this. The wl.skydrive_update scope is required.

Change me/skydrive to the folder ID (or, in certain cases, the friendly name, like "My Pictures") of the folder in which the new folder will be created.

private async void btnCreateFolder_Click(object sender, RoutedEventArgs e)
{
    try
    {
        var folderData = new Dictionary<string, object>();
        folderData.Add("name", "A brand new folder was created");
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.PostAsync("me/skydrive", folderData);
        dynamic result = operationResult.Result;
        this.infoTextBlock.Text = string.Join(" ", "Created folder:", result.name, "ID:", result.id);
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error creating folder: " + exception.Message;
    }
}

Delete a folder

To remove a folder, use code like this. The wl.skydrive_update scope is required.

Change the folder ID to the folder ID (or, in certain cases, the friendly name) of the folder that you want to remove.

private async void btnDeleteFolder_Click(object sender, RoutedEventArgs e)
{
    try
    {
        LiveConnectClient liveClient = new LiveConnectClient(this.session);
        LiveOperationResult operationResult =
            await liveClient.DeleteAsync("folder.a6b2a7e8f2515e5e.A6B2A7E8F2515E5E!147");
        this.infoTextBlock.Text = "Folder deleted.";
    }
    catch (LiveConnectException exception)
    {
        this.infoTextBlock.Text = "Error deleting folder: " + exception.Message;
    }
}