2013-06-04 8 views
9

私はフォーマットされていないSDカードから8ギガバイトの16進データを読み込むためのC#ツールを開発しています。FileNotFoundExceptionランダムにスローされました

これは可能ですが、File Not Found Exceptionをランダムにスローします。例えば、それは1ギガバイトまたは2ギガバイトを読み、その後それをスローするでしょう。それ以外の場合は、8ギガバイトを数回連続して読み込み、例外をスローします。つまり、完全にランダムにポップアップするように見えます。

私は何が原因か分かりません。

編集:フィードバックを使用していくつかの調整を行いました。以下に貼り付けられているのは、更新されたコードです。

まだランダムにfilenotfoundexceptionをスローしますが、これは、ギグ8のmb 432を読み込もうとすると、常に引数の例外をスローします(無作為にfilenotfoundをスローすることなくそれを取得した場合)。

ファイルハンドルが同期操作をサポートしていないというエラーが表示されます。

メッセージ:指定されたファイルが見つかりませんにFileNotFoundExceptionのために示されている私は、再書いエラー以下

class Program 
{ 
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
    static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess, 
     uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, 
     uint dwFlagsAndAttributes, IntPtr hTemplateFile); 

    static void Main(string[] args) 
    { 
     string testOutputDirectory = @"C:\\Users\\aiovanna\\Desktop\\out1.txt"; //Specifies where to write the results of the read. 
     try 
     { 
      SafeFileHandle fileHandle = CreateFile("\\\\.\\E:", 0x80000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero); 
      FileStream readStream = new FileStream(fileHandle, FileAccess.Read); //The stream to be read. Is converted to binary. 
      BufferedStream bufStream = new BufferedStream(readStream, 1048576); 
      FileStream writeStream = File.OpenWrite(testOutputDirectory); //Writing stream opened at the specified directory of output. 
      //BinaryReader reader = new BinaryReader(readStream); //Changes the read stream to binary. Has more powerful methods. 

      long gigsRead; //Loop counter that specifies the number of gigabytes read thus far. 
      long megsRead; //Loop counter that specifies the number of megabytes read thus far within the current gigabyte. 

      Stopwatch totalStopwatch = new Stopwatch(); //Stopwatch to time the total execution of the card read. 
      Stopwatch megStopwatch = new Stopwatch(); //Stopwatch to time the execution of reading the current megabyte. 
      Stopwatch gigStopwatch = new Stopwatch(); //Stopwatch to time the executation of reading the current gigabyte. 
      totalStopwatch.Start(); //Start timing the program. 
      int bytesRead; 

      for (gigsRead = 0; gigsRead < 8; gigsRead++) //Gigabyte loop 
      { 
       gigStopwatch.Start(); //Start timer for current gigabyte. 
       for (megsRead = 0; megsRead < 1024; megsRead++) //Megabyte loop 
       { 
        megStopwatch.Start(); //Start timer for current megabyte. 

        try 
        { 
         byte[] buffer = new byte[1048576]; //Buffer to be read into from card 
         long test = gigsRead * 1073741824 + megsRead * 1048576; 
         bufStream.Position = test; 
         bytesRead = bufStream.Read(buffer, 0, 1048576); //Read from SD card to buffer 
         if (bytesRead < 1048576) 
         { 
          Console.WriteLine("Didn't read whole chunk!"); 
         } 
         writeStream.Write(buffer, 0, 1048576); //Write from buffer to output text file. 
         megStopwatch.Stop(); //Stop timer for current megabyte. 
         Console.WriteLine("Finished mb {0} of gig {1} in {2}", megsRead + 1, gigsRead + 1, megStopwatch.Elapsed); 
         megStopwatch.Reset(); //Reset for next megabyte. 
        } 

        catch (System.IO.FileNotFoundException ex) 
        { 
         System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error()); 
         System.Console.WriteLine("Message: {0}", ex.Message); 
         System.Console.WriteLine("Source: {0}", ex.Source); 
         System.Console.WriteLine("Stack Trace: {0}", ex.StackTrace); 
         System.Console.WriteLine("Target Site: {0}", ex.TargetSite); 
         System.Console.WriteLine(ex.ToString()); 
         writeStream.Close(); //Close writing stream. 
         //reader.Close(); //Close the binary reader stream. 
         bufStream.Close(); 
         fileHandle.Close(); //Close the SD card file. 
         readStream.Close(); //Close the filestream reader. 
         System.Console.WriteLine("You will need to turn off your computer, take out the card, turn the computer back on, put the SD card back in, and re-run the program."); 
         System.Console.WriteLine("Press any key to terminate."); 
         System.Console.ReadKey(); 
         System.Environment.Exit(1); 
        } 

        catch (System.ArgumentException ex) 
        { 
         System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error()); 
         System.Console.WriteLine("Message: {0}", ex.Message); 
         System.Console.WriteLine("Param Name: {0}", ex.ParamName); 
         System.Console.WriteLine("Source: {0}", ex.Source); 
         System.Console.WriteLine("Stack Trace: {0}", ex.StackTrace); 
         System.Console.WriteLine("Target Site: {0}", ex.TargetSite); 
         System.Console.WriteLine(ex.ToString()); 
         writeStream.Close(); //Close writing stream. 
         //reader.Close(); //Close the binary reader stream. 
         fileHandle.Close(); //Close the SD card file. 
         readStream.Close(); //Close the filestream reader. 
         System.Console.WriteLine("You will need to turn off your computer, take out the card, turn the computer back on, put the SD card back in, and re-run the program."); 
         System.Console.WriteLine("Press any key to terminate."); 
         System.Console.ReadKey(); 
         System.Environment.Exit(1); 
        } 
       } 
       gigStopwatch.Stop(); //Stop timer for current gigabyte. 
       Console.WriteLine("Finished gig {0} in {1}", gigsRead + 1, gigStopwatch.Elapsed); 
       gigStopwatch.Reset(); //Reset for next gigabyte. 
      } 
      totalStopwatch.Stop(); //Stop total execution timer. 
      Console.WriteLine(totalStopwatch.Elapsed); //Print total execution timer. 
      writeStream.Close(); //Close writing stream. 
      //reader.Close(); //Close the binary reader stream. 
      writeStream.Close(); //Close writing stream. 
      fileHandle.Close(); //Close the SD card file. 
      readStream.Close(); //Close the filestream reader. 
      bufStream.Close(); 
     } 

     catch (System.IO.IsolatedStorage.IsolatedStorageException ex) 
     { 
      System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error()); 
      System.Console.WriteLine("Isolated Storage Exception"); 
      System.Console.WriteLine("Data: {0}", ex.Data); 
      System.Console.WriteLine("Help Link: {0}", ex.HelpLink); 
      System.Console.WriteLine("Inner Exception: {0}", ex.InnerException); 
      System.Console.WriteLine("Message: {0}", ex.Message); 
      System.Console.WriteLine("Source: {0}", ex.Source); 
      System.Console.WriteLine("Stack Trace {0}", ex.StackTrace); 
      System.Console.WriteLine("Target Site: {0}", ex.TargetSite); 
      Console.ReadKey(); 
     } 

     catch (System.ArgumentException ex) 
     { 
      System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error()); 
      System.Console.WriteLine("Argument Exception"); 
      System.Console.WriteLine("Data: {0}", ex.Data); 
      System.Console.WriteLine("Help Link: {0}", ex.HelpLink); 
      System.Console.WriteLine("Inner Exception: {0}", ex.InnerException); 
      System.Console.WriteLine("Message: {0}", ex.Message); 
      System.Console.WriteLine("Param Name: {0}", ex.ParamName); 
      System.Console.WriteLine("Source: {0}", ex.Source); 
      System.Console.WriteLine("Stack Trace {0}", ex.StackTrace); 
      System.Console.WriteLine("Target Site: {0}", ex.TargetSite); 
      Console.ReadKey(); 
     } 

     catch (System.IO.DirectoryNotFoundException ex) 
     { 
      System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error()); 
      System.Console.WriteLine("Directory Not Found Exception"); 
      System.Console.WriteLine("Data: {0}", ex.Data); 
      System.Console.WriteLine("Help Link: {0}", ex.HelpLink); 
      System.Console.WriteLine("Inner Exception: {0}", ex.InnerException); 
      System.Console.WriteLine("Message: {0}", ex.Message); 
      System.Console.WriteLine("Source: {0}", ex.Source); 
      System.Console.WriteLine("Stack Trace {0}", ex.StackTrace); 
      System.Console.WriteLine("Target Site: {0}", ex.TargetSite); 
      System.Console.ReadKey(); 
     } 

     catch (System.ObjectDisposedException ex) 
     { 
      System.Console.WriteLine("The error was: {0}", Marshal.GetLastWin32Error()); 
      System.Console.WriteLine("Object Disposed Exception"); 
      System.Console.WriteLine("Data: {0}", ex.Data); 
      System.Console.WriteLine("Help Link: {0}", ex.HelpLink); 
      System.Console.WriteLine("Inner Exception: {0}", ex.InnerException); 
      System.Console.WriteLine("Message: {0}", ex.Message); 
      System.Console.WriteLine("Object Name {0}", ex.ObjectName); 
      System.Console.WriteLine("Source: {0}", ex.Source); 
      System.Console.WriteLine("Stack Trace {0}", ex.StackTrace); 
      System.Console.WriteLine("Target Site: {0}", ex.TargetSite); 
      Console.ReadKey(); 
     } 
    } 
} 

。 ソース:mscorlib スタックトレース:System.IO.FileStream.ReadCoreで (バイト[]バッファは、オフセットINT32、INT32カウント)System.IO .__ Error.WinIOError(INT32のエラーコード、ストリングmaybeFullPath)で でSystem.IO.FileStream.Read(Byte [] array、Int32 offset、Int32 count) at System.IO.BinaryReader.Read(Byte []バッファ、Int32インデックス、Int32カウント) at RawSDAccessTest.Program.Main文字列{} args)C:\ Users \ etc ...行67 対象サイト:無効WinIOError(Int32、System.String) System.IO.FileNotFoundException:できません指定されたファイルを探します。 行67は、 reader.Read(buffer、0、1048576)です。

ここで私が本当に奇妙なのは、プログラムが完全に正常で、リーダーオブジェクトも使用する65行目です。何らかの形で行65と67の実行の間に、ファイルが存在しなくなったと判断します。私はそれがそれを解決するかどうかを見るために待っていた。それはしませんでした。

何が原因でこの例外がランダムに発生するか、またはそれを解決する方法についてのアイデアはありますか?

EDIT:40:26.1077157 AM SDCardReadAttempt3.vshost.exe 2432のReadFile E:プロセス・モニターは、次の

8を表示SUCCESSオフセット:3228565504、長さ:1,048,576、I/Oフラグ:非キャッシュされ、優先度:通常

8:40:26.1745974 AM SDCardReadAttempt3.vshost.exe 2432のReadFile E:NOようなデバイスは、オフセットません:3229614080、長さ:131072、I/Oフラグ:非キャッシュされ、優先度:通常

ので、間に読み取り、デバイスが存在しなくなります。ファイルの作成と削除を内部ループに移動し、ファイルを読み込もうとするたびにファイルを作成します。問題は解決しません。ハードウェアのような匂いがする。

EDIT 2:時々、非同期読み取り例外がスローされます。

9:16:16.1129926 AM SDCardReadAttempt3.vshost.exe 3752のReadFile E:無効なパラメータオフセット:7969177600、長さ:1,048,576、I/Oフラグ:非キャッシュされ、優先度:通常

私にはわかりませんどのようにネットが深く働くか。これは、ファイルが複数のスレッドで読み取られるように開かれていないときに、これをスレッドプロセスにしている可能性があります。私はそこに待ってから、このエラーを排除して元のものに戻すことができるかどうかを確認します。

+3

Process Monitor(http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx)に詳しい情報があるかどうか確認してみましたか? –

+0

このような断続的な問題は、通常、ハードウェアの問題を示しています。障害が発生した後にマシンを再起動する必要が本当に必要な場合は、ハードウェアを非常に強く疑うでしょう。 COPYまたはROBOCOPYまたはXCOPYを使用してファイルをコピーしようとするとどうなりますか? –

+0

@NicoleDesRosiers便利なツールのようです。推奨していただきありがとうございます。 Jim:これらのメソッドはすべて、ファイルパス文字列を必要とします。私は文字列を返すフォーマットされていないドライブのファイルを作る方法を見つけることができません。 –

答えて

1

私はスキャナからの読み込みにも同様の問題がありました。私は例外をキャッチしなければならなくなり、しばらく待っていました(私の目的のためにはうまくいきました)。そして同じデータをもう一度読み直そうとします。私はしきい値を定義しました(私にとってはうまくいった6)、私はあきらめてユーザーにエラーを起こしました。

これは、ほとんどの場合、ハードウェアに追いつくのに十分な時間を与えるように見えました。

また、問題を引き起こしているブロックだけを読んでみてください。特定のブロックを読み込む際に一貫してエラーが発生すると、明らかにハードウェアの問題が発生します。

関連する問題