빠른 시작: 임시 앱 데이터(HTML)

[ 이 문서는 Windows 런타임 앱을 작성하는 Windows에서 8.x 및 Windows Phone 8.x 개발자를 대상으로 합니다. Windows 10용으로 개발하는 경우에는 최신 설명서를 참조하세요.]

임시 앱 데이터 저장소에서 파일을 저장하고 검색하는 방법에 대해 알아봅니다.

앱 파일 컨테이너 가져오기

파일을 가져오려면 ApplicationData.temporaryFolder 속성을 사용합니다. 다음 단계에서는 이 단계의 temporaryFolder 변수를 사용합니다.


var applicationData = Windows.Storage.ApplicationData.current;
var temporaryFolder = applicationData.temporaryFolder;

파일에 데이터 쓰기

Windows.Storage.StorageFolder.createFileAsyncWindows.Storage.FileIO.writeTextAsync 같은 파일 API를 사용하여 임시 앱 데이터 저장소에서 파일을 만들고 업데이트합니다. 이 예제에서는 temporaryFolder 컨테이너에 이름이 dataFile.txt인 파일을 만들고 이 파일에 현재 날짜와 시간을 씁니다. CreationCollisionOption 열거형의 replaceExisting 값은 파일이 이미 있는 경우 파일을 바꾸어야 함을 나타냅니다.

function writeTimestamp() {
   temporaryFolder.createFileAsync("dataFile.txt", Windows.Storage.CreationCollisionOption.replaceExisting)
      .then(function (sampleFile) {
         var formatter = new Windows.Globalization.DateTimeFormatting.DateTimeFormatter("longtime");
         var timestamp = formatter.format(new Date());

         return Windows.Storage.FileIO.writeTextAsync(sampleFile, timestamp);
      }).done(function () {      
      });
}

파일에서 데이터 읽기

Windows.Storage.StorageFolder.getFileAsync, Windows.Storage.StorageFile.GetFileFromApplicationUriAsyncWindows.Storage.FileIO.readTextAsync 같은 파일 API를 사용하여 임시 앱 데이터 저장소에서 파일을 열고 읽습니다. 이 예제에서는 앞 단계에서 만든 dataFile.txt 파일을 열고 파일에서 날짜를 읽습니다. CreationCollisionOption 열거형의 openIfExists 값은 파일이 반드시 있어야 함을 나타냅니다. 다양한 위치에서 파일 리소스 로드에 대한 자세한 내용은 파일 리소스를 로드하는 방법을 참조하세요.

function readTimestamp() {
   temporaryFolder.getFileAsync("dataFile.txt")
      .then(function (sampleFile) {
         return Windows.Storage.FileIO.readTextAsync(sampleFile);
      }).done(function (timestamp) {
         // Data is contained in timestamp
      }, function () {
         // Timestamp not found
      });
}

관련 항목

작업

파일 리소스를 로드하는 방법

빠른 시작: 로컬 앱 데이터

빠른 시작: 로밍 앱 데이터

개념

Windows 런타임을 사용하여 앱 데이터 액세스

참조

Windows.Storage.ApplicationData

Windows.Storage.ApplicationDataCompositeValue

Windows.Storage.ApplicationDataContainer

Windows.Storage.ApplicationDataContainerSettings

샘플

응용 프로그램 데이터 샘플