2014-09-15 1 views
0

FileStream을 만들 때 임의의 FileNotFoundExceptions에 문제가 있습니다. 파일 경로가 정확하고 파일이 존재하지만 임의의 시간에 FileNotFoundException이 나옵니다. 어떻게 자세히 디버깅 할 수 있습니까? ,debug FileNotFoundException depth C#

internal static FInstance ReadAndDecrypt(string key, string fiId) 
     { 
      FileInfo fileInfo = new FileInfo(FullFilePath(fiId)); 

      FileStream fileStream = null; 
      CryptoStream cryptoStream = null; 
      GZipInputStream zipStream = null; 

      try 
      { 
       int time = Environment.TickCount; 
       fileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read); 

       var serializer = new XmlSerializer(typeof(FInstance)); 
       var FInstance = (FInstance)serializer.Deserialize(fileStream); 

       return Instance; 
      } 
      catch (Exception e) 
      { 
       Log.Error(e); 
       return null; 
      } 
      finally 
      { 
       if (fileStream != null) 
        fileStream.Close(); 
      } 

     } 

나는이 발생할 수 아무것도 생각할 수 없다 : 나는 윈도우 모바일 6 프로젝트를 실행하고 있는데 이것은 예외 :이 예외를 throw하는 방법입니다

System.IO.FileNotFoundException: Could not find file '\Program Files\xxx\xxx\xxx-11133.bin'. 
    at System.IO.__Error.WinIOError(Int32 errorCode, String str) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) 

더 깊은 디버깅 방법이 있습니까?

+0

동일한 파일에 대해 여러 번 호출 하시겠습니까? 해당 파일을 수정하는 다른 프로그램/코드가 없다고 확신합니까? – Default

+0

@Hans, Windows CE에는 드라이브 문자가 없습니다. – ctacke

답변

1

디버깅은 간단해야합니다. 바로 try/catch 블록 전에 코드에 다음을 추가 : 파일이없는 경우

if((!fileInfo.Exists) && (Debugger.IsAttached)) 
{ 
    Debugger.Break(); 
} 

이것은 당신이 상태로 얻을 것이다. 어쨌든 프로덕션 코드에서 파일을 열기 전에 존재 여부를 확인하여 앱이 이미 실행 중일 때 파일이 삭제되거나 사용중인 것을 방지해야합니다.