2012-07-01 7 views
-1

텍스트 파일에서 각 행의 이름을 읽어서 List<Image>을 채우려고합니다. 텍스트 파일은 다음과 같습니다.텍스트 파일에서 읽을 때 응용 프로그램이 작동을 멈 춥니 다

image0 
image1 
image2 
image 
... 

다음 코드는 프로그램이 완전히 중단되어 Visual Studio가 정지됩니다.

int counter = 0; 
string line = string.Empty; 
StreamReader file = new StreamReader("ItemFile.txt"); 

while ((line = file.ReadLine()) != null) 
{ 
    imageCollection.Add(new Image()); 
    imageCollection[counter].Source = new BitmapImage(new Uri("Images/" + line + ".png", UriKind.Relative)); 
} 
+3

내가 생각을 WP7, 당신은 파일을 읽고 쓰려면 IsolatedStorage 메커니즘을 사용해야합니다. – ZafarYousafi

+1

예외가 발생 했습니까? 큰 이미지의 큰 목록을로드하는 것이 느릴 것으로 예상됩니다. – Vlad

+1

파일이 격리 된 저장소에 있으면 격리 된 저장소에서 파일을 읽어야합니다. [해당 가이드는 도움이 될 수 있습니다.] (http://www.windowsphonegeek.com/tips/all-about-wp7-isolated-storage-read-and-save-text-files) –

답변

1

WP7에서 표준 읽기/쓰기 메커니즘을 사용할 수 없습니다. 당신은 그렇게 IsolatedStorage 클래스를 사용해야합니다 :

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("ItemFile.txt", FileMode.Open, FileAccess.Read); 
using (StreamReader reader = new StreamReader(fileStream)) 
{ //Visualize the text data in a TextBlock text 
    while ((line = reader .ReadLine()) != null) 
    { 
     imageCollection.Add(new Image()); 
     imageCollection[counter].Source = new BitmapImage(new Uri("Images/" + line + ".png", UriKind.Relative)); 
    } 
} 

당신이이 질문에 체크 아웃을 설치하는 동안 프로젝트의 일환으로 장치에 추가됩니다 파일에서 텍스트를 읽을합니다 : How to read files from project folders?

+0

IsolatedStorageFileStream에서 허용되지 않는 작업이 발생할 때 위의 코드를 사용합니다. – Subby

관련 문제