Share via


방법: 격리된 저장소의 파일 읽기 및 쓰기

IsolatedStorageFileStream 클래스를 사용하여 저장소 내에서 파일을 열 수 있는 여러 가지 방법이 있습니다. IsolatedStorageFileStream을 가져온 다음에는 이를 사용하여 StreamReader 또는 StreamWriter를 가져올 수 있습니다. StreamReaderStreamWriter를 사용하여 다른 파일에서와 마찬가지로 저장소의 파일을 읽고 이 파일에 쓸 수 있습니다. 파일 읽기 및 쓰기 작업에 대한 자세한 내용은 파일 및 스트림 I/O를 참조하십시오.

ReadingAndWritingToFiles 예제

다음 코드 예제에서는 격리된 저장소를 가져오고 TestStore.txt라는 파일을 만든 다음 파일에 "Hello Isolated Storage"를 씁니다. 그런 다음 파일을 읽고 그 결과를 콘솔에 출력합니다.

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

Public Class ReadingAndWritingToFiles
    Public Shared Function Main() As Integer
        ' Get an isolated store for this assembly and put it into an
        ' IsolatedStoreFile object.
        Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or
            IsolatedStorageScope.Assembly, Nothing, Nothing)

        ' This code checks to see if the file already exists.

        Dim fileNames() As String = isoStore.GetFileNames("TestStore.txt")
        For Each file As String In fileNames
            If file = "TestStore.txt" Then
                Console.WriteLine("The file already exists!")
                Console.Write("Type ""StoreAdm /REMOVE"" at the command line to delete all ")
                Console.WriteLine("Isolated Storage for this user.")

                ' Exit the program.
                Return 0
            End If
        Next

        writeToFile(isoStore)

        Console.WriteLine("The file ""TestStore.txt"" contains:")

        ' Call the readFromFile and write the returned string to the
        'console.
        Console.WriteLine(readFromFile(isoStore))

        ' Exit the program.
        Return 0
    End Function ' End of main.

    ' This method writes "Hello Isolated Storage" to the file.
    Private Shared Sub writeToFile(isoStore As IsolatedStorageFile)
        ' Declare a new StreamWriter.
        Dim writer As StreamWriter = Nothing

        ' Assign the writer to the store and the file TestStore.
        writer = New StreamWriter(New IsolatedStorageFileStream(
            "TestStore.txt", FileMode.CreateNew, isoStore))

        ' Have the writer write "Hello Isolated Storage" to the store.
        writer.WriteLine("Hello Isolated Storage")

        writer.Close()

        Console.WriteLine("You have written to the file.")

    End Sub ' End of writeToFile.

    ' This method reads the first line in the "TestStore.txt" file.
    Private Shared Function readFromFile(isoStore As IsolatedStorageFile) As String
        ' This code opens the TestStore.txt file and reads the string.
        Dim reader As New StreamReader(New IsolatedStorageFileStream(
            "TestStore.txt", FileMode.Open, isoStore))

        ' Read a line from the file and add it to sb.
        Dim sb As String = reader.ReadLine()

        ' Close the reader.
        reader.Close()

        ' Return the string.
        Return sb.ToString()
    End Function ' End of readFromFile.
End Class
using System;
using System.IO;
using System.IO.IsolatedStorage;

public class ReadingAndWritingToFiles
{
    public static int Main()
    {
        // Get an isolated store for this assembly and put it into an
        // IsolatedStoreFile object.
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
            IsolatedStorageScope.Assembly, null, null);

        // This code checks to see if the file already exists.

        string[] fileNames = isoStore.GetFileNames("TestStore.txt");
        foreach (string file in fileNames)
        {
            if (file == "TestStore.txt")
            {
                Console.WriteLine("The file already exists!");
                Console.Write("Type \"StoreAdm /REMOVE\" at the command line to delete all ");
                Console.WriteLine("Isolated Storage for this user.");

                // Exit the program.
                return 0;
            }
        }

        writeToFile(isoStore);

        Console.WriteLine("The file \"TestStore.txt\" contains:");

        // Call the readFromFile and write the returned string to the
        //console.
        Console.WriteLine(readFromFile(isoStore));

        // Exit the program.
        return 0;
    } // End of main.

    // This method writes "Hello Isolated Storage" to the file.
    private static void writeToFile(IsolatedStorageFile isoStore)
    {
        // Declare a new StreamWriter.
        StreamWriter writer = null;

        // Assign the writer to the store and the file TestStore.
        writer = new StreamWriter(new IsolatedStorageFileStream(
            "TestStore.txt", FileMode.CreateNew, isoStore));

        // Have the writer write "Hello Isolated Storage" to the store.
        writer.WriteLine("Hello Isolated Storage");

        writer.Close();

        Console.WriteLine("You have written to the file.");

    } // End of writeToFile.

    // This method reads the first line in the "TestStore.txt" file.
    private static string readFromFile(IsolatedStorageFile isoStore)
    {
        // This code opens the TestStore.txt file and reads the string.
        StreamReader reader = new StreamReader(new IsolatedStorageFileStream(
            "TestStore.txt", FileMode.Open, isoStore));

        // Read a line from the file and add it to sb.
        String sb = reader.ReadLine();

        // Close the reader.
        reader.Close();

        // Return the string.
        return sb.ToString();

    } // End of readFromFile.
}
using namespace System;
using namespace System::IO;
using namespace System::IO::IsolatedStorage;

public ref class ReadingAndWritingToFiles
{
public:
    static int Main()
    {
        // Get an isolated store for this assembly and put it into an
        // IsolatedStoreFile object.
        IsolatedStorageFile^ isoStore = IsolatedStorageFile::GetStore(IsolatedStorageScope::User |
            IsolatedStorageScope::Assembly, (Type ^)nullptr, (Type ^)nullptr);

        // This code checks to see if the file already exists.

        array<String^>^ fileNames = isoStore->GetFileNames("TestStore.txt");
        for each (String^ file in fileNames)
        {
            if (file == "TestStore.txt")
            {
                Console::WriteLine("The file already exists!");
                Console::Write("Type \"StoreAdm /REMOVE\" at the command line to delete all ");
                Console::WriteLine("Isolated Storage for this user.");

                // Exit the program.
                return 0;
            }
        }

        writeToFile(isoStore);

        Console::WriteLine("The file \"TestStore.txt\" contains:");

        // Call the readFromFile and write the returned string to the
        //console.
        Console::WriteLine(readFromFile(isoStore));

        // Exit the program.
        return 0;
    } // End of main.

private:
    // This method writes "Hello Isolated Storage" to the file.
    static void writeToFile(IsolatedStorageFile^ isoStore)
    {
        // Declare a new StreamWriter.
        StreamWriter^ writer = nullptr;

        // Assign the writer to the store and the file TestStore.
        writer = gcnew StreamWriter(gcnew IsolatedStorageFileStream(
            "TestStore.txt", FileMode::CreateNew, isoStore));

        // Have the writer write "Hello Isolated Storage" to the store.
        writer->WriteLine("Hello Isolated Storage");

        writer->Close();

        Console::WriteLine("You have written to the file.");

    } // End of writeToFile.

    // This method reads the first line in the "TestStore.txt" file.
    static String^ readFromFile(IsolatedStorageFile^ isoStore)
    {
        // This code opens the TestStore.txt file and reads the string.
        StreamReader^ reader = gcnew StreamReader(gcnew IsolatedStorageFileStream(
            "TestStore.txt", FileMode::Open, isoStore));

        // Read a line from the file and add it to sb.
        String^ sb = reader->ReadLine();

        // Close the reader.
        reader->Close();

        // Return the string.
        return sb->ToString();

    } // End of readFromFile.
};

int main()
{
    return ReadingAndWritingToFiles::Main();
}

참고 항목

참조

IsolatedStorageFile

IsolatedStorageFileStream

System.IO.FileMode

System.IO.FileAccess

System.IO.StreamReader

System.IO.StreamWriter

개념

격리된 저장소