2012-08-23 2 views
0

격리 된 저장소의 작성된 디렉토리에서 작성된 파일을 읽으려고합니다. 파일이 실제로 작성 중입니다. 이 예외가 그것을 읽을 때 그러나 "작업 IsolatedStorageFileStream에 허용되지 않습니다."... 격리 된 저장소에서 작성된 디렉토리의 파일 읽기

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    if (!storage.DirectoryExists("CourseworkDirectory")) 
     storage.CreateDirectory("CourseworkDirectory"); 

    XElement Coursework = new XElement(CourseworkID); 
    XDocument _doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), Coursework); 
    IsolatedStorageFileStream location = new IsolatedStorageFileStream("CourseworkDirectory\\"+CourseworkID, System.IO.FileMode.Create, storage); 
    StreamWriter file = new StreamWriter(location); 

    _doc.Save(file);//saving the XML document as the file 
    file.Close(); 
    file.Dispose();//disposing the file 
    location.Dispose(); 
} 

파일을 읽기 ....

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    string searchpath = System.IO.Path.Combine("CourseworkDirectory", "*.*"); 

    foreach (string filename in storage.GetFileNames(searchpath)) 
    { 
     XElement _xml; 
     IsolatedStorageFileStream location = new IsolatedStorageFileStream(filename, System.IO.FileMode.Open, storage); 

그것은 실제로 파일 이름을 얻고있다 그러나이 시점에서는 예외가 있습니다.

+1

당신은 storage.OpenFile (fileName, FileMode.Open, FileAccess.Read)을 시도 했습니까? –

+0

예 ... 실제로 파일 이름을보고 있지만 동일한 문제가 있습니다. – user1619553

+0

문제는 내가 만든 디렉토리와 관련이 있습니다 ... IsolatedStorageFile을 사용하려고 시도했을 때 매우 효과적이었습니다. – user1619553

답변

0

당신이 일을함으로써이를 달성 할 수 ---

using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    IsolatedStorageFileStream fileStream = storage.OpenFile(app.getSettingsPath,FileMode.Open, FileAccess.Read); 
    using (StreamReader reader = new StreamReader(fileStream)) 
    { 
     string line; 
     while((line = reader.ReadLine()) != null) 
     { 
     // do your work here 
     } 
    } 
} 
0

이 시도, 그것은 나를 위해 작동 : 다음이 작동하지 않는 경우가

 String sb; 

     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      if (myIsolatedStorage.FileExists(fileName)) 
      { 
       StreamReader reader = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.Open, myIsolatedStorage)); 

       sb = reader.ReadToEnd(); 

       reader.Close(); 
      } 

      if(!String.IsNullOrEmpty(sb)) 
      { 
       MessageBox.Show(sb); 
      } 
     } 

너무 당신을 위해 일하는 희망 파일이 존재하지 않을 수도 있습니다.

+0

안녕하세요 Milani 나는 이것을 시도하고 나를 위해 작동하지만 XML 파일을'var xml = XDocument.Load ("path") 내부에서 사용하려면 원한다면 하나의 질문이 있습니다. ToString();'나는 메서드 "Load"는 Stream을 매개 변수로 취하지 만, 당신이 나를 도울 수 있다면 사용법을 모르겠습니다! –

관련 문제