Share via


StorageFile.CopyAsync 메서드

정의

오버로드

CopyAsync(IStorageFolder)

지정된 폴더에 파일의 복사본을 만듭니다.

CopyAsync(IStorageFolder, String)

지정된 폴더에 파일의 복사본을 만들고 복사본의 이름을 바꿉니다.

CopyAsync(IStorageFolder, String, NameCollisionOption)

지정된 폴더에 파일의 복사본을 만들고 복사본의 이름을 바꿉니다. 이 메서드는 동일한 이름의 파일이 대상 폴더에 이미 있는 경우 수행할 작업도 지정합니다.

CopyAsync(IStorageFolder)

지정된 폴더에 파일의 복사본을 만듭니다.

public:
 virtual IAsyncOperation<StorageFile ^> ^ CopyAsync(IStorageFolder ^ destinationFolder) = CopyAsync;
/// [Windows.Foundation.Metadata.Overload("CopyOverloadDefaultNameAndOptions")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder const& destinationFolder);
[Windows.Foundation.Metadata.Overload("CopyOverloadDefaultNameAndOptions")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder);
function copyAsync(destinationFolder)
Public Function CopyAsync (destinationFolder As IStorageFolder) As IAsyncOperation(Of StorageFile)

매개 변수

destinationFolder
IStorageFolder

파일의 복사본이 만들어지는 대상 폴더입니다.

반환

이 메서드가 완료되면 destinationFolder에서 만든 파일의 복사본을 나타내는 StorageFile을 반환합니다.

구현

특성

예외

대상 폴더에 파일을 복사할 수 있는 권한이 없습니다.

예제

다음 예제에서는 지정된 폴더에 파일을 복사하고 CopyAsync(IStorageFolder, String, NameCollisionOption) 메서드를 호출하여 복사본의 이름을 바꿉니다. 이 예제에서는 지정된 이름의 파일이 이미 있는 경우 대상 폴더의 파일을 대체하는 옵션 의 값을 명시적으로 지정합니다.

using Windows.Storage;
using System.Threading.Tasks;

// Get the app's temporary folder.
StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;

// Create a sample file in the temporary folder.
string newFileName = "test.txt";
StorageFile newFile = await tempFolder.CreateFileAsync(newFileName);

// Get the app's local folder to use as the destination folder.
StorageFolder localFolder = ApplicationData.Current.LocalFolder;

// Specify a new name for the copied file.
string renamedFileName = "renamed_test.txt";

// Copy the file to the destination folder and rename it.
// Replace the existing file if the file already exists.
StorageFile copiedFile = await newFile.CopyAsync(localFolder, renamedFileName, NameCollisionOption.ReplaceExisting);
IAsyncAction MainPage::CopyFileAsync()
{
    // Get the app's temporary folder.
    StorageFolder sourceFolder{ ApplicationData::Current().TemporaryFolder() };
    StorageFolder targetFolder{ ApplicationData::Current().LocalFolder() };

    // Choose source file name and new name for the copied file.
    std::wstring newFileName{ L"test.txt" };
    std::wstring copyFileName{ L"renamed_test.txt" };

    // Create a sample file in the temporary folder.
    StorageFile sourceFile{ co_await sourceFolder.CreateFileAsync(newFileName, Windows::Storage::CreationCollisionOption::ReplaceExisting) };
    // Overwrite any existing file with the same name.
    StorageFile copiedFile{ co_await sourceFile.CopyAsync(targetFolder, copyFileName, Windows::Storage::NameCollisionOption::ReplaceExisting) };
    // Do something with copied file.
}
//Get the app's temporary folder
StorageFolder^ sourceFolder = ApplicationData::Current->TemporaryFolder;
StorageFolder^ targetFolder = ApplicationData::Current->LocalFolder;

// Choose source file name and new name for the copied file
String^ newFileName = "test.txt";
String^ copyFileName = "renamed_test.txt";

//Create a sample file in the temporary folder
auto copyFileTask = create_task(sourceFolder->CreateFileAsync(newFileName, Windows::Storage::CreationCollisionOption::ReplaceExisting)).then
([targetFolder, copyFileName](StorageFile^ sourceFile) -> task<StorageFile^>
{
    //Overwrite any existing file with the same name 
    auto copyFileTask = sourceFile->CopyAsync(targetFolder, copyFileName, Windows::Storage::NameCollisionOption::ReplaceExisting);
    return create_task(copyFileTask);
}).then([](StorageFile^ copiedFile) {
    //do something with copied file
});

설명

이 메서드는 기본적으로 CreationCollisionOption 열거형의 FailIfExists 값을 사용합니다. 즉, 이 메서드는 이름이 같은 파일이 대상 폴더에 이미 있는 경우 예외를 발생합니다. 다른 방식으로 파일 이름 충돌을 처리하려면 CopyAsync(IStorageFolder, String, NameCollisionOption) 오버로드된 메서드를 호출합니다.

참고

StorageFile.CopyAsync를 사용하여 암호화된 파일을 암호화되지 않은 대상에 복사하는 경우 다음 예외로 호출이 실패합니다. System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

추가 정보

적용 대상

CopyAsync(IStorageFolder, String)

지정된 폴더에 파일의 복사본을 만들고 복사본의 이름을 바꿉니다.

public:
 virtual IAsyncOperation<StorageFile ^> ^ CopyAsync(IStorageFolder ^ destinationFolder, Platform::String ^ desiredNewName) = CopyAsync;
/// [Windows.Foundation.Metadata.Overload("CopyOverloadDefaultOptions")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder const& destinationFolder, winrt::hstring const& desiredNewName);
[Windows.Foundation.Metadata.Overload("CopyOverloadDefaultOptions")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName);
function copyAsync(destinationFolder, desiredNewName)
Public Function CopyAsync (destinationFolder As IStorageFolder, desiredNewName As String) As IAsyncOperation(Of StorageFile)

매개 변수

destinationFolder
IStorageFolder

파일의 복사본이 만들어지는 대상 폴더입니다.

desiredNewName
String

Platform::String

winrt::hstring

destinationFolder에서 만든 파일의 복사본에 대한 새 이름입니다.

반환

이 메서드가 완료되면 destinationFolder에서 만든 파일의 복사본을 나타내는 StorageFile을 반환합니다.

참고

StorageFile.CopyAsync를 사용하여 암호화된 파일을 암호화되지 않은 대상에 복사하는 경우 다음 예외로 호출이 실패합니다. System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

구현

M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,System.String) M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,Platform::String) M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,winrt::hstring)
특성

예외

대상 폴더에 파일을 복사할 수 있는 권한이 없습니다.

추가 정보

적용 대상

CopyAsync(IStorageFolder, String, NameCollisionOption)

지정된 폴더에 파일의 복사본을 만들고 복사본의 이름을 바꿉니다. 이 메서드는 동일한 이름의 파일이 대상 폴더에 이미 있는 경우 수행할 작업도 지정합니다.

public:
 virtual IAsyncOperation<StorageFile ^> ^ CopyAsync(IStorageFolder ^ destinationFolder, Platform::String ^ desiredNewName, NameCollisionOption option) = CopyAsync;
/// [Windows.Foundation.Metadata.Overload("CopyOverload")]
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<StorageFile> CopyAsync(IStorageFolder const& destinationFolder, winrt::hstring const& desiredNewName, NameCollisionOption const& option);
[Windows.Foundation.Metadata.Overload("CopyOverload")]
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<StorageFile> CopyAsync(IStorageFolder destinationFolder, string desiredNewName, NameCollisionOption option);
function copyAsync(destinationFolder, desiredNewName, option)
Public Function CopyAsync (destinationFolder As IStorageFolder, desiredNewName As String, option As NameCollisionOption) As IAsyncOperation(Of StorageFile)

매개 변수

destinationFolder
IStorageFolder

파일의 복사본이 만들어지는 대상 폴더입니다.

desiredNewName
String

Platform::String

winrt::hstring

destinationFolder에서 만든 파일의 복사본에 대한 새 이름입니다.

option
NameCollisionOption

지정된 desiredNewName 파일이 대상 폴더에 이미 있는 경우 충돌을 처리하는 방법을 결정하는 열거형 값 중 하나입니다.

반환

이 메서드가 완료되면 destinationFolder에서 만든 파일의 복사본을 나타내는 StorageFile을 반환합니다.

참고

StorageFile.CopyAsync를 사용하여 암호화된 파일을 암호화되지 않은 대상에 복사하는 경우 다음 예외로 호출이 실패합니다. System.UnauthorizedAccessException: Access is denied. (Excep_FromHResult 0x80070005)

구현

M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,System.String,Windows.Storage.NameCollisionOption) M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,Platform::String,Windows.Storage.NameCollisionOption) M:Windows.Storage.IStorageFile.CopyAsync(Windows.Storage.IStorageFolder,winrt::hstring,Windows.Storage.NameCollisionOption)
특성

예외

대상 폴더에 파일을 복사할 수 있는 권한이 없습니다.

추가 정보

적용 대상