2013-02-12 5 views
0

기본적으로 IsolatedStorage에 새 파일을 만드는 코드를 작성하고 있습니다. 스트림을 올바르게 닫을 수 있지만 누락 된 부분이 있어야합니다. 내가 뭔가 확실한 것을 놓치지 않았 음을 확인하기 위해서? 여기IsolatedStorageFileStream IsolatedStorageExeption이 처리되지 않았습니다.

는 내 사용자가 그/그녀의 이름을 입력하면 오류가 발생하고, 게임의 끝에서 호출되는 경우, 그것의 기능을 저장할 것 : 내 읽기 기능을

public void SaveHighScores(string NewName, int NewScore) 
{ 
    SortHighScores(NewName, NewScore); 

    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 
    try 
    { 
     using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("HighScores.txt", FileMode.CreateNew, isoStore)) 
     { 
      using (StreamWriter writer = new StreamWriter(isoStream)) 
      { 
       for (int i = 0; i < 5; i++) 
       { 
        writer.WriteLine(MyScores[i].Name); 
        writer.WriteLine(MyScores[i].Score); 
       } 
       writer.Close(); 
      } 
      isoStream.Close(); 
     } 
    } 
    catch (IsolatedStorageException e) 
    { 
     throw e; // "IsolatedStorageException was unhandled" error now occurs here 
    } 
} 

을 그리고 여기에, 그것은이라고합니다 초기화하는 동안 게임 시작시 한 번 :

public void ReadHighScores() 
{ 
    IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 

    if (isoStore.FileExists("HighScores.txt")) 
    { 
     try 
     { 
      using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("HighScores.txt", FileMode.Open, isoStore)) 
      { 
       using (StreamReader reader = new StreamReader(isoStream)) 
       { 
        int i = 0; 
        while (!reader.EndOfStream) 
        { 
         MyScores[i].Name = reader.ReadLine(); 
         string scoreString = reader.ReadLine(); 
         MyScores[i].Score = Convert.ToInt32(scoreString); 
         i++; 
        } 
        reader.Close(); 
       } 
       isoStream.Close(); 
      } 
     } 
     catch (IsolatedStorageException e) 
     { 
      throw e; 
     } 
    } 
    else 
    { 
     if (!failedRead) 
     { 
      failedRead = true; 
      ReadHighScores(); 
     } 
    } 
} 

누구든지이 문제에 대해 의견을 개진 할 수 있습니까?

어떤 이유로이 지금 내가이 게임 플레이를 다시 시작할 때, 응용 프로그램의 I 재생 그러나 다음 번에 새로 설치에 게임 기능을 저장하거나 전화를 처음 작동 있도록

확인 [편집] 이상하게도 저장하려고 할 때 다시 충돌합니다. FileMode.CreateNew 어디서 잘못 될까요?

[편집]

그래는 FileMode.CreateNew는 파일이 이미 나는 새 파일을 작성하기 전에 내가 isoStore.DeleteFile("HighScores.txt")를 추가 그래서 존재하는 경우에 지금, exeption를 throw 꿈 :

[SOLVED]

+0

시도 해보려고 시도해 봤나? 런타임 또는 컴파일 할 때 오류가 발생합니까? –

+0

런타임, 저녁 식사를 마치 자마자 try catch를 추가해 보겠습니다. – TotalJargon

+0

내 게시물을 업데이트하면 예외가 발생했을 때 오류가 발생합니다. – TotalJargon

답변

1
처럼 작동

FileMode.CreateNew은 파일이 이미 존재할 때 예외를 throw하므로 새 파일을 만들기 전에 isoStore.DeleteFile(string FileName)에 전화해야합니다. 또는 FileMode.CreateNew을 반드시 사용해야하는 경우 FileMode.Create을 대신 사용하십시오.

+0

대신 FileMode.Create를 사용하십시오. –

관련 문제