다음을 통해 공유


방법: 격리된 저장소에 파일 및 디렉터리 만들기

업데이트: 2007년 11월

저장소를 가져온 다음에는 데이터를 저장할 디렉터리와 파일을 만들 수 있습니다. 저장소 내에서 파일 및 디렉터리 이름은 가상 파일 시스템의 루트와 관련하여 지정됩니다.

디렉터리를 만들려면 IsolatedStorageFileCreateDirectory 인스턴스 메서드를 사용하십시오. 만들지 않은 디렉터리의 하위 디렉터리를 지정하면 두 디렉터리가 모두 만들어지고 이미 있는 디렉터리를 지정하면 예외가 생성되지 않습니다. 그러나 잘못된 문자를 포함하는 디렉터리 이름을 지정하면 IsolatedStorageException이 생성됩니다.

파일을 만들고 열려면 IsolatedStorageFileStream 생성자 중 하나를 사용하여 파일 이름, FileModeOpenOrCreate 및 파일을 만들 저장소를 전달합니다. 그런 다음 읽기, 검색, 쓰기 등 파일 스트림에서 원하는 데이터 작업을 수행할 수 있습니다. 또한 IsolatedStorageFileStream 생성자를 사용하여 파일을 다른 용도로 열 수도 있습니다.

IsolatedStorageFile 인수를 받지 않는 IsolatedStorageFileStream 생성자 중 하나를 사용하여 저장소를 먼저 가져오지 않고 파일을 만들거나 열 수 있습니다. 이런 형식의 생성자를 사용하면 파일의 도메인 저장소에 파일이 만들어집니다.

Windows 파일 시스템 격리 저장소 파일 및 디렉터리 이름을 비교할 때는 대/소문자를 구분하지 않습니다. 따라서 ThisFile.txt라는 파일을 만든 다음 THISFILE.TXT라는 다른 파일을 만들면 한 파일만 만들어집니다. 파일 이름은 표시를 위해 원래의 대/소문자를 그대로 유지합니다.

CreatingFilesAndDirectories 예제

다음 코드 예제에서는 격리된 저장소에서 파일과 디렉터리를 만드는 방법을 보여 줍니다. 먼저 사용자, 도메인 및 어셈블리별로 격리된 저장소를 검색하여 isoStore 변수에 대입합니다. CreateDirectory 메서드는 몇 개의 디렉터리를 설정하는 데 사용되고 IsolatedStorageFileStream 메서드는 이 디렉터리에 몇 개의 파일을 만듭니다.

Imports System
Imports System.IO
Imports System.IO.IsolatedStorage

Public Module modmain

   Sub Main()

      ' Get an isolated store for user, domain, and assembly and put it into 
      ' an IsolatedStorageFile object.

      Dim isoStore As IsolatedStorageFile
      isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)

      ' This code creates a few different directories.

      isoStore.CreateDirectory("TopLevelDirectory")
      isoStore.CreateDirectory("TopLevelDirectory/SecondLevel")

      ' This code creates two new directories, one inside the other.

      isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory")

      ' This file is placed in the root.

      Dim isoStream1 As New IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore)
      Console.WriteLine("Created a new file in the root.")
      isoStream1.Close()

      ' This file is placed in the InsideDirectory.

      Dim isoStream2 As New IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore)
      Console.WriteLine("Created a new file in the root.")
      isoStream2.Close()

      Console.WriteLine("Created a new file in the InsideDirectory.")

   End Sub
End Module
using System;
using System.IO;
using System.IO.IsolatedStorage;

public class CreatingFilesDirectories{

   public static void Main(){

      // Get a new isolated store for this user, domain, and assembly.  
      // Put the store into an IsolatedStorageFile object.

      IsolatedStorageFile isoStore =  IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Domain | IsolatedStorageScope.Assembly, null, null);

      // This code creates a few different directories.

      isoStore.CreateDirectory("TopLevelDirectory");
      isoStore.CreateDirectory("TopLevelDirectory/SecondLevel");

      // This code creates two new directories, one inside the other.
      isoStore.CreateDirectory("AnotherTopLevelDirectory/InsideDirectory");


      // This file is placed in the root.

      IsolatedStorageFileStream isoStream1 = new IsolatedStorageFileStream("InTheRoot.txt", FileMode.Create, isoStore);
      Console.WriteLine("Created a new file in the root.");
      isoStream1.Close();

      // This file is placed in the InsideDirectory.

      IsolatedStorageFileStream isoStream2 = new IsolatedStorageFileStream("AnotherTopLevelDirectory/InsideDirectory/HereIAm.txt", FileMode.Create, isoStore);
      isoStream2.Close();

      Console.WriteLine("Created a new file in the InsideDirectory.");

   }// End of Main.
}

참고 항목

참조

IsolatedStorageFile

IsolatedStorageFileStream

기타 리소스

격리된 저장소 작업 수행