2012-02-29 3 views
0

Windows phone 7에서 문제가 발생하여 답변을 찾을 수 없습니다. 텍스트 파일의 내용을 프로젝트 파일에있는 간단한 txt 파일에 쓰려고합니다. StreamWriter를 사용하려고했지만 StreamReader 만 작동하고 IsolatedStorage를 사용하려고 시도했지만 다시 작동하지 않았습니다. 제발, 어쩌면 당신이 나 에게이 문제에 대한 해결책을 찾을 수 있습니까?파일에 어떻게 쓰나요?

편집 : 내가 사용 코드 :

var myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
// Specify the file path and options. 
using (var isoFileStream = new IsolatedStorageFileStream("/Discount%20it;component/Tax.txt", FileMode.OpenOrCreate, myStore)) 
{ 
    //Write the data 
    using (var isoFileWriter = new StreamWriter(isoFileStream)) 
    { 
        isoFileWriter.WriteLine(textBox1.Text); 
    } 
} 

string fileName = "/Discount%20it;component/Tax.txt"; 

using (var isoStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    // we need to check to see if the file exists 
    if (!isoStorage.FileExists(fileName)) 
    { 
        // file doesn't exist...time to create it. 
        isoStorage.CreateFile(fileName); 
    } 

    // since we are appending to the file, we must use FileMode.Append 
    using (var isoStream = new IsolatedStorageFileStream(fileName, FileMode.Append, isoStorage)) 
    { 
        // opens the file and writes to it. 
        using (var fileStream = new StreamWriter(isoStream)) 
        { 
            fileStream.Write(textBox1.Text); 
        } 
    } 
} 
+0

사용 코드에서 "작동하지 않음"을 보여주십시오 - 충돌했거나 오류가 발생했거나 자동으로 실패 했습니까? – Jason

+0

@ Jason 때로는 Premmision, IsolatedStorage 예외, StreamWriter 예외가 없다고 말했습니다 ... – Yehonatan

+0

두 번째로 코드의 형식을 올바르게 지정하십시오. 답변을 얻는 데 도움이됩니다. – Will

답변

0

을 설명 MSDN here에 대한 섹션이있다. 작동하지 않습니까? 해당 페이지에 표시

예제 코드 :

private void btnWrite_Click(object sender, RoutedEventArgs e) 
{ 
    // Obtain the virtual store for the application. 
    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 

    // Create a new folder and call it "MyFolder". 
    myStore.CreateDirectory("MyFolder"); 

    // Specify the file path and options. 
    using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.OpenOrCreate, myStore)) 
    { 
     //Write the data 
     using (var isoFileWriter = new StreamWriter(isoFileStream)) 
     { 
      isoFileWriter.WriteLine(txtWrite.Text); 
     } 
    } 
} 

지금 당신이 그것을에 파일을 작성하기 전에 프로그래밍 방식으로 격리 된 저장소에 디렉토리를 생성 할 필요가 있습니다 것입니다.

개발을 위해 개발 컴퓨터의 디렉토리에 this page과 같이 디렉토리와 파일이 생성됩니다. Windows 7 컴퓨터에서 개발중인 경우 분리 된 디렉토리와 파일을 Vista와 동일한 위치에서 찾을 수있을 것으로 기대합니다. 로밍 사용자 프로필이 아닌 경우 Users<username>AppData\Local.

위 코드의 경우 파일은 <username>\AppData\Local\MyFolder이어야합니다.

코드에서이 줄 :

IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 

지도 당신의 dev에 컴퓨터에 <username>\AppData\Local에 IsolatedStorage과 myStore를 호출합니다.

이이 줄에 당신이 다음 쓸 수있다 디렉토리를 만듭니다이다

myStore.CreateDirectory("MyFolder"); 

응용 프로그램이 여기에이 파일에서 읽은하기 위해 예제 코드 : 당신이 얻을 얼마나

// This code opens and reads the contents of myFile.txt. 
private void btnRead_Click(object sender, RoutedEventArgs e) 
{ 
    // Obtain a virtual store for the application. 
    IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 

    try 
    { 
     // Specify the file path and options. 
     using (var isoFileStream = new IsolatedStorageFileStream("MyFolder\\myFile.txt", FileMode.Open, myStore)) 
     { 
      // Read the data. 
      using (var isoFileReader = new StreamReader(isoFileStream)) 
      { 
       txtRead.Text = isoFileReader.ReadLine(); 
      } 
     } 

    } 
    catch 
    { 
     // Handle the case when the user attempts to click the Read button first. 
     txtRead.Text = "Need to create directory and the file first."; 
    } 
} 

공지 사항 격리 저장소 파일의 실제 위치 및 파일을 열 때 파일 경로에 디렉토리 이름을 포함시킵니다.

추가 (2012년 1월 3일)

당신은 또한 FileAccess.Write를 사용하는 것이 FileMode.Append을 보장하여. 파일이 존재하는지 확인하는 대신 FileMode.OpenOrCreate을 사용하십시오. 그 격리 된 저장소를 확인 실버 사용하는 경우 이 활성화되어 있지 (쉽게) 수정있는 XAP의 일환으로 배포됩니다

Start -> All Programs -> Microsoft Silverlight -> Microsoft Silverlight -> Application Storage -> make sure the 'enable application storage' checkbox is checked. 
+0

내 프로젝트에 맞게 코드를 수정했습니다. using (var isoFileStream = new IsolatedStorageFileStream ("\\ Tax.txt", FileMode.Create, myStore)) isoFileWriter.Write (textBox1.Text); 그러나 여전히 아무 일도 일어나지 않았습니다. Tax.txt 파일은 프로젝트 파일 (mainpage.xaml은 어디에 있습니까?)에 있습니다. – Yehonatan

+0

데이터 파일이 응용 프로그램간에 격리되도록하려면 먼저 파일 용 디렉토리를 만들어야합니다. . – ChrisBD

+0

나는 "Files"라는 이름의 프로젝트 디렉토리에 새로운 디렉토리를 만들지 않고 내게 준 작업을 수행하려고 할 때 IsolatedStorageFileStream에서 허용되지 않는 연산이 발생한다. – Yehonatan

0

파일.

일반적으로 업데이트하고 파일에 보관하려는 일부 데이터가있는 경우 IsolatedStorage에서 수행해야합니다.

당신이 XAP의 일부로서 파일에 기본 데이터를 제공한다고 가정하면, 다음을 수행 할 것입니다 :

데이터를 읽을 싶은 : 이 - IsolatedStorage에서 파일이 존재하는지 확인합니다.
- 거기에서 읽으면.
- 데이터 쓰기를 원하는 경우는 다음 XAP

사용하여 파일에서 해당 선박을 읽을 수없는 경우 :
를 - IsolatedStorage에 기록하고 기존 파일/데이터를 덮어 씁니다.

+0

어떻게 액세스 할 수 있습니까? automaticlly 내 응용 프로그램에 연결되어? 그리고 다시 말해 항상 내게 말해 - IsolatedStorageFileStream에서 허용되지 않는 작업 here - var isoFileStream = new IsolatedStorageFileStream ("Files \\ Tax.txt", FileMode.Create, myStore – Yehonatan

관련 문제