2017-08-26 4 views
0

vb.netでプログラムを作成しようとしているが、ファイルを開くときにファイルが16進数のコードに変換され、問題は、大きなファイルを開くと、通常はOutOfMemory例外が発生するということです。大きなファイルを開いて16進数に変換するとOutOfMemory例外が発生するVB.NET

私はいくつかのことを試みましたが、何も機能しませんでした。

 ToolStripLabel1.Text = "Status: Loading" 
    Cursor = Cursors.WaitCursor 

    If OpenFileDialog1.ShowDialog <> DialogResult.Cancel Then 
     Dim infoReader As IO.FileInfo 
     infoReader = My.Computer.FileSystem.GetFileInfo(OpenFileDialog1.FileName) 
     If Math.Truncate(infoReader.Length/1024/1024) > 15 Then 
      Dim x As MsgBoxResult 
      x = MsgBox("Opening files bigger than 15MB can cause the program to be unresponsive for long periods of time, crash or throw a OutOfMemory exception, depending on the size of the file." & vbCrLf & vbCrLf & "Are you sure you want to continue loading this file?", MsgBoxStyle.Exclamation + MsgBoxStyle.YesNo, "Warning - HEX EDITOR") 
      If x = MsgBoxResult.Yes Then 
        RichTextBox1.AppendText(String.Join(" ", File.ReadAllBytes(OpenFileDialog1.FileName).Select(Function(b) b.ToString("X2")))) 'This is where the exception would happen. 
       ToolStripLabel1.Text = "Status: Idle" 
      End If 
     Else 
      RichTextBox1.Text = String.Join(" ", IO.File.ReadAllBytes(OpenFileDialog1.FileName).Select(Function(b) b.ToString("X2"))) 
      ToolStripLabel1.Text = "Status: Idle" 
     End If 
    End If 
    RichTextBox2.Text = RichTextBox1.Text 
    NumericUpDown3.Maximum = RichTextBox1.Text.Length 
    NumericUpDown2.Maximum = RichTextBox1.Text.Length 
    NumericUpDown3.Value = RichTextBox1.Text.Length 
    Cursor = Cursors.Default 
    Label4.Text = "File Opened: " & OpenFileDialog1.FileName 

答えて

0

代わりに、メモリ内のすべてのバイトをロードして、小さな2つの文字列の結合enourmousを実行しようとすると、ファイル・データのブロックを読み込むことができ、Hexに単一のブロックを変換し、リッチテキストボックスにその変換を追加します。非同期のすべて/ファッション

Async Sub FillWithHex(rtb As RichTextBox, name As String) 

    ' Arbitrary block size. Experiment with different sizes to 
    ' see the difference in loading times vs smooth scrolling in richtextbox   
    Dim buff(1000000) As Byte 

    Using fs = New FileStream(name, FileMode.Open) 
     Using br = New BinaryReader(fs) 
      While True 
       Dim text = String.Empty 
       buff = br.ReadBytes(1000000) 
       Await Task.Run(Sub() text = String.Join(" ", buff. 
           Select(Function(b) b.ToString("X2")))). 
           ConfigureAwait(True) 
       rtb.AppendText(text) 
       If buff.Length < 1000000 Then 
        Exit While 
       End If 
      End While 

     End Using 
    End Using 
End Sub 

待つと

If OpenFileDialog1.ShowDialog <> DialogResult.Cancel Then 
    FillWithHex(RichTextBox1, OpenFileDialog1.FileName) 
End Sub 
+0

のおかげで、あなたのコードからそれを呼び出す、私はそのことについて考えていたが、私はそれをクラッシュしたりせずにそれを行う方法を知りませんでしたエラー。 – Coolvideos73

+0

SaveFileDialog1.ShowDialog <> DialogResult.Cancel Then Dim b As Byte()= RichTextBox1.Text.Split( "" c).Select(Function My.Computer.FileSystem.WriteAllBytes(SaveFileDialog1.FileName、b、False) 終了場合 – Coolvideos73

+0

オリジナルの質問と共通するこの新しい問題は何ですか?新しい質問を投稿してください。 – Steve

関連する問題