2011-04-21 5 views
0

현재 Windows Phone 7 응용 프로그램을 만들고 있습니다. 배열의 모든 값을 텍스트 파일로 변환하여 다른 페이지 목록 상자 안에있는 특정 배열에 저장된 모든 값을 인쇄하고 격리 된 저장소에 저장할 수 있습니다. 감사합니다. :(배열을 텍스트 파일로 변환하는 방법은 무엇입니까?

나는이 방법을 시도했지만 작동하지 않습니다. ViewList.xaml.cs를 들어

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

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


     //Create a new file and assign a StreamWriter to the store and this new file (myFile.txt) 
     //Also take the text contents from the txtWrite control and write it to myFile.txt 

     StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.OpenOrCreate, myStore)); 
     writeFile.WriteLine(retrieveDrinksListBox); 
     writeFile.Close(); 
    } 

FavouriteList.xaml.cs

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e) 
    { 


     //Obtain a virtual store for application 
     IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 

     //This code will open and read the contents of retrieveDrinksListBox 
     //Add exception in case the user attempts to click “Read button first. 

     StreamReader readFile = null; 

     try 
     { 
      readFile = new StreamReader(new IsolatedStorageFileStream("ListFolder\\myFile.txt", FileMode.Open, myStore)); 
      string fileText = readFile.ReadLine(); 

      if (fileText != "") 
      { 

       listNumberListBox.Items.Add("List 1"); 
      } 

      //The control txtRead will display the text entered in the file 
      listNumberListBox.Items.Add(fileText); 
      readFile.Close(); 

     } 

     catch 
     { 

      MessageBox.Show("Need to create directory and the file first."); 

     } 

답변

0

당신은 작성해야 목록 상자 자체가 아닌 목록 상자의 항목/데이터를 멀리 둡니다.

읽을 때 그들을 다시 deserialize해야합니다. MSDN의 BinaryFormatter 예제에는 (파일) 스트림에 객체를 쓰는 방법과 동일한 스트림에서 객체를 다시 복구하는 방법을 보여주는 코드 샘플이 있습니다.

0
당신이 목록 상자에서 파일의 내용 (예를 들어, 선)를 표시 할 경우, 한 번에 전체 파일 내용을 읽을

split 라인 :

string fileText = readFile.ReadToEnd(); 
string[] lines = fileText.Split(Enviroment.NewLine.ToCharArray(), 
           StringSplitOptions.RemoveEmptyEntries); 
listNumberListBox.Items.AddRange(lines); 

다른 방법은 주위에 당신의 목록 상자에서 항목을 유사한 페치 , joining에 의해 새 줄이 표시되고 한 번에 파일로 덤프됩니다.

string fileContents = string.Join(Environment.NewLine, 
            retrieveDrinksListBox.Items.Cast<string>()); 
writeFile.WriteLine(fileContents); 
관련 문제