0

사용하여 기존 텍스트 파일에 데이터를 추가 버튼을 누를 때 다시 모두가 잘 작동 코드를 따르는 IsolatedStorageFile

private void locChoice(object sender, RoutedEventArgs e) 
     { 
      IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); 
      string filePath = "locations.txt"; 

      if (store.FileExists(filePath)) 
      { 
       Debug.WriteLine("Files Exists"); 
       try 
       { 
        string fileData; 
        using (IsolatedStorageFileStream isoStream = 
         new IsolatedStorageFileStream("locations.txt", FileMode.Open, store)) 
        { 
         using (StreamReader reader = new StreamReader(isoStream)) 
         { 
          fileData = reader.ReadToEnd(); 
         } 
        } 
        testLocationPicker.ItemsSource = fileData.Split(';'); 
       } 
       catch (Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 

      } 

      testLocationPicker.Open(); 
     } 

을 사용자가 텍스트 블록을 채워 일단 지금, 내가 그렇게 좋아하고있어 프로그래밍 텍스트 파일에 추가 할 :

StringBuilder sb = new StringBuilder();         // Use a StringBuilder to construct output. 
      var store = IsolatedStorageFile.GetUserStoreForApplication();   // Create a store 
      string[] filesInTheRoot = store.GetFileNames();       // Store all files names in an array 
      Debug.WriteLine(filesInTheRoot[0]);          // Show first file name retrieved (only one stored at the moment) 

      string filePath = "locations.txt"; 

      if (store.FileExists(filePath)) 
      { 

       Debug.WriteLine("Files Exists"); 
       StreamWriter sw = 
         new StreamWriter(store.OpenFile(filePath, 
          FileMode.Open, FileAccess.Write)); 

       Debug.WriteLine("Writing..."); 
       sw.WriteLine(locationName + ";"); // Semi Colon required for location separation in text file 
       sw.Close(); 
       Debug.WriteLine(locationName + "; added"); 
       Debug.WriteLine("Writing complete"); 

      } 

그러나 이전 페이지로 돌아가서 ListPicker에서 항목을 보려면 단추를 클릭하면 이상하게 보입니다. 최신 항목이 맨 위에 표시되어 목록의 두 번째 자리와 큰 차이가납니다. 그 위에 다른 항목을 추가하면 마지막 항목을 숨기는 것 같습니다.

아래의 스크린 샷은 첫 번째 '시카고'항목 위에 입력해야합니다. 실종 된 것으로 보이는 두 번째 시카고 작품이 있어야합니다.

전화로 작성된 텍스트 파일을 탐색하고 데이터 배치 방법을 정확하게 볼 수 있습니까? 이것은 단서를 줄 수 있습니다. 나는 오히려 새로운 라인을 추가하는 대신, 기존 텍스트를 덮어 또 다른 라인을 작성할 때 추가 조사시

enter image description here

가 나타납니다. 어떤 생각을 어떻게 그만 두겠습니까?

답변

0

파일을 추가하는 것과 관련하여 문서를 찾았습니다.이 문서는 데이터가 덮어 쓴 것으로 의심됩니다.

이제 데이터를 추가 할 때 내가 사용하는 방법은 다음과 같습니다

Does IsolatedStorage File.Append mode has an error with the the '\n'?

: 다른 스택 오버플로 포스트 내 첫 번째 검색시 나타나지 않았다 솔루션을 나에게 도움이
// ---------------------------------------------------------------- 
      // Write the friend name to the locations text file upon submission 
      // ---------------------------------------------------------------- 

      StringBuilder sb = new StringBuilder();         // Use a StringBuilder to construct output. 
      var store = IsolatedStorageFile.GetUserStoreForApplication();   // Create a store 
      string[] filesInTheRoot = store.GetFileNames();       // Store all files names in an array 
      Debug.WriteLine(filesInTheRoot[0]);          // Show first file name retrieved (only one stored at the moment) 
      byte[] data = Encoding.UTF8.GetBytes(locationName + ";"); 
      string filePath = "locations.txt"; 

      if (store.FileExists(filePath)) 
      { 
       using (var stream = new IsolatedStorageFileStream(filePath, FileMode.Append, store)) 
       { 

        Debug.WriteLine("Writing..."); 
        stream.Write(data, 0, data.Length); // Semi Colon required for location separation in text file 
        stream.Close(); 
        Debug.WriteLine(locationName + "; added"); 
        Debug.WriteLine("Writing complete"); 
       } 


      }