8

격리 된 저장소에 문제가 있습니다.IsolatedStorageFileStream에서 작업이 허용되지 않습니다. 오류

List<Notes> data = new List<Notes>(); 

using (IsolatedStorageFile isoStore = 
     IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    using (IsolatedStorageFileStream isoStream = 
      isoStore.OpenFile("Notes.xml", FileMode.OpenOrCreate)) 
    { 
    XmlSerializer serializer = new XmlSerializer(typeof(List<Notes>)); 
    data = (List<Notes>)serializer.Deserialize(isoStream);    
    } 
} 

data.Add(new Notes() { Note = "hai", DT = "Friday" }); 

return data; 

실수 : : 작업 IsolatedStorageFileStream에 허용되지

내 코드입니다.

using (IsolatedStorageFileStream isoStream = 
     isoStore.OpenFile("Notes.xml", FileMode.OpenOrCreate)) 

답변

16

대개 이러한 코드 블록을 여러 번 동시에 실행할 때 발생합니다. 파일 잠금이 끝납니다. 그래서, 당신은이 같은 생성자에서 FileAccess 및 파일 공유 모드를 포함 확인해야합니다 : 당신은 다른 사람이 읽고있는 동안 파일에 기록하려면

using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.Open, FileAccess.Read, FileShare.Read, isolatedStorage) 
{ 
//... 
} 

를, 당신은 다음과 같이 잠금 동기화해야합니다

private readonly object _readLock = new object(); 

lock(_readLock) 
{ 
    using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isolatedStorage) 
    { 
     //... 
    } 
} 
+0

이 답변은 Windows Phone 8에도 유효합니다. 고맙습니다. –

0

에 IsolatedStorageFileStream 생성자와 내부 사용하여 문을 바꾸기 :

또한
using (var isoStream = new IsolatedStorageFileStream("Notes.xml", FileMode.Open, isoStore)) 

, 당신은 파일에서 읽고 있기 때문에, 나는 당신이 원하는 파일 모드가 열기,하지 OpenOrCreate 가정.

그리고 '데이터'지정되는 경우, 대신

serializer.Deserialize(isoStream) as List<Notes> 

을 사용하는 것이 좋습니다. Effective C#, 2nd Ed.의 항목 3을 참조하십시오.

+0

를 사용하여 미디어를 재생할 수 있습니다 난 것으로 변경 (0, 0)이다. – yozawiratama

+0

이전에이 IsolatedStorageFile에 작성하지 않은 것 같습니다. 맞습니까? 응용 프로그램의 비즈니스 논리에서 파일을 읽으려고 시도하기 전에 해당 파일이 이미 기록되었는지 또는 파일의 존재 여부를 확인해야합니다. 읽은 파일과 비슷한 구조를 사용하여 파일을 작성하십시오. 단, Deserialize 대신 Serialize를 호출하고 FileMode.OpenOrCreate를 사용하십시오. –

-1

IsolatedStorageFileStream에서 작업 할 수 없습니다. 공유 파일에서 대상으로 파일을 이동할 때 오류가 발생했습니다. 그

이 네임 스페이스

using BackgroundProcess.Resources; 
using Microsoft.Phone.BackgroundTransfer; 
using System.IO.IsolatedStorage; 

추가 작업하는 격리 된 저장소

BackgroundTransferRequest transfer; 
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication()) 

{ 

     if (isoStore.GetDirectoryNames("DestinationFolder").Length == 0) 
      isoStore.CreateDirectory("DestinationFolder"); 

     storage.MoveFile("/shared/transfers/xyzFileName.mp3", "DestinationFolder"); 

} 

에서 하나의 대상 디렉토리를 만들거나 대신 대상의 파일 이름은 폴더 이름을 추가 추가하는

isoStore.MoveFile(transfer.DownloadLocation.OriginalString, "DestinationFolder"); 

를 사용합니다. 목록 같은 데이터 = serializer.Deserializer (isoStream을),하지만 난 XML 문서에 오류가 새로운 문제를 가지고 :

당신은 당신의 제안으로 다음 코드

try 
{ 
     string isoFileName = "DestinationFolder//xyzFileName.mp3"; 

     StorageFile file = null; 

     try 
     { 
      file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/" + isoFileName)); 
     } 
     catch (Exception ex) 
     { 
     } 
     if (file != null) 
      await Windows.System.Launcher.LaunchFileAsync(file); 
    } 
    catch (Exception ex) 
    { 
    }