2013-09-02 2 views
0

응용 프로그램에서 생성되지 않은 파일을 읽으려고합니다. Windows phone 7 응용 프로그램에서 IsolatedStorageException이 발생했습니다.

string FileName = "stops.txt"; 
string FolderName = "data"; 
string FilePath = System.IO.Path.Combine(FolderName, FileName); 

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(FilePath, FileMode.Open, FileAccess.Read); 
using (StreamReader reader = new StreamReader(fileStream)) 
{ 
    MessageBox.Show(reader.ReadLine()); 
} 

가 나는 "isolatedstorageexception"을 던져 : link to exception

System.IO.IsolatedStorage.IsolatedStorageException: [IsolatedStorage_Operation_ISFS] 
Arguments: 
Debugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=4.0.50829.0&File=mscorlib.dll&Key=IsolatedStorage_Operation_ISFS 
    at System.IO.IsolatedStorage.IsolatedStorageFileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, IsolatedStorageFile isf) 
    at System.IO.IsolatedStorage.IsolatedStorageFile.OpenFile(String path, FileMode mode, FileAccess access) 
    at HorairesCTS.MainPage.test() 
    at HorairesCTS.MainPage..ctor() 

누군가가 나에게이 파일을 읽을 수 있도록 할 수

여기에 내가 시도 샘플입니다?

감사합니다.

+0

"응용 프로그램에서 생성되지 않았다"고 말할 때 - 파일을 만든 이유는 무엇입니까? 응용 프로그램에서 만든 것이 아니라면 응용 프로그램의 사용자 저장소에있는 이유는 무엇입니까? –

+0

격리 된 저장소 기능으로 생성되지 않은 프로젝트에서 복사/붙여 넣기 한 파일 –

+0

격리 된 저장소에 있지 않은 것으로 의심됩니다. 적어도 격리 된 저장소의 사용자 부분에는 없습니다. 프로젝트에있는 경우 리소스 URL로 액세스하거나 어셈블리에 포함 할 수 있습니다. –

답변

1

프로젝트에 포함 된 파일을 읽으려고하면 IsolatedStorage에 없습니다. Application.GetResourceStream을 통해 액세스해야합니다.

private string ReadTextFile(string filePath) 
{ 
    var resourceStream = Application.GetResourceStream(new Uri(filePath, UriKind.Relative)); 
    Stream myFileStream = resourceStream.Stream; 
    StreamReader myStreamReader = new StreamReader(myFileStream); 
    return myStreamReader.ReadToEnd(); 
} 

Visual Studio에서 파일의 속성에 Build action Content에 설정하는 것을 잊지 마세요 : 여기

로컬 텍스트 파일을 읽을 수있는 샘플 코드입니다.

ReadTextFile("data/stops.txt") 
+0

완벽하게 작동합니다. 나는 왜이 링크로 실패했는지 이해하지 못한다 : http://www.geekchamp.com/tips/all-about-wp7-isolated-storage-read-and-save-text-files 그러나 샘플을위한 Thx! –

+0

파일이 IsolatedStorage에 없기 때문에 파일이 응용 프로그램 폴더에 포함되어 있기 때문에 응용 프로그램 폴더에 있습니다. IsolatedStorage는 런타임에 생성 된 파일을 저장하는 곳입니다 (기본적으로 비어 있음). –

0

당신이 개체의 목록을 저장 지팡이합니다. 이 작업을 수행 할 수 있습니다

IsolatedStorageFileStream outStream = new IsolatedStorageFileStream("MyData.bin", FileMode.Create, myStore); 
     DataContractSerializer ser = new DataContractSerializer(typeof(List<ClsUser>)); 

     ser.WriteObject(outStream, valutaTyperListe); 
     outStream.Close(); 

데이터 가져 오기 : 추가 기억 클래스는 다음과 같이

IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication(); 
if (myStore.FileExists("MyData.bin")) 
      { 

       IsolatedStorageFileStream inStream = new IsolatedStorageFileStream("MyData.bin", FileMode.Open, myStore); 

       DataContractSerializer Serializ = new DataContractSerializer(typeof(List<ClsUser>)); 
       myUserList = Serializ.ReadObject(inStream) as List<ClsUser>; 
       inStream.Close(); 
      } 

그리고 "를들은 System.Runtime.Serialization를 사용하여,"

[DataContract] 
public class ClsUser 
{ 
    string name; 
    string lastname; 

    public ClsUser(string name, string lastname) 
    { 
     this.name = name; 
     this.lastname = lastname; 
    } 

    [DataMember] 
    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 

    [DataMember] 
    public string Lastname 
    { 
     get { return lastname; } 
     set { lastname = value; } 
    } 
} 
관련 문제