2010-11-27 17 views
0

私はMy.Computer.Filesystem.WriteAllBytesを使用して、アプリケーションのリソースに格納されている実行可能ファイルを起動ディレクトリに書き出します。実行可能ファイルを実行した後、私はそれを削除します。すべてうまくいく。しかし、無作為にUnauthorizedAccessExceptionを取得します。例外を取得した後、問題なくファイルを手動で削除できます。ここでは、完全なコードがあります:IO.File.Delete Random UnauthorizedAccessException

' Convert MP3 
' First, copy out converter 
Dim Path = New IO.FileInfo(SoundPath) 
Try 
    My.Computer.FileSystem.WriteAllBytes(Application.StartupPath + "\converter.exe", My.Resources.madplay, False) 
Catch ex As Exception 
    MessageBox.Show(ex.ToString, "Report", MessageBoxButtons.OK) 
    Exit Sub 
End Try 
' Set up process 
Dim MAD As New Process 
' Set process info 
Dim output As String = IO.Path.GetFileNameWithoutExtension(Path.FullName) + ".wav" 
Dim input As String = Path.FullName 
Dim adjust As String = barVolumeAdjust.Value.ToString 
Dim hz As String = "15000" 
With (MAD.StartInfo) 
    .FileName = Application.StartupPath + "\converter.exe" 
    .Arguments = "-v -a " + adjust + " -R " + hz + " -o """ + output + """ """ + input + """" 
    .UseShellExecute = False 
    .RedirectStandardInput = True 
    .RedirectStandardError = True 
    .RedirectStandardOutput = True 
    .CreateNoWindow = True 
End With 
' Start 
MAD.Start() 
' Update title with output 
Dim Line As String = MAD.StandardError.ReadLine 
While Not Line Is Nothing 
    Me.Text = Line 
    Line = MAD.StandardError.ReadLine 
End While 
' Stop 
MAD.Close() 
' Delete MAD 
Try 
    IO.File.Delete(Application.StartupPath + "\converter.exe") 
Catch ex As Exception 
    MessageBox.Show(ex.ToString, "Report", MessageBoxButtons.OK) 
End Try 

私をperplexes何を、私は文字通りだけが実行を書き、他に何もおそらくそれを使用することはできなかったということです。私はファイルの属性をチェックしており、読み取り専用ではありません。私のアプリケーションも管理者として実行されています。何が問題なの?

答えて

3

プロセスを終了するのを待たずに、ファイルを削除しようとすると実行中です。プロセスを参照してください。 WaitForExit

+0

ああ、私はそれを見落として信じられない。私はMAD.Close()をMAD.WaitForExit()に置き換えました。ありがとう! – Steven

1

ファイルを書き出すために別のプロセスを使用しているようです。おそらく、削除しようとするとまだファイルを使用しているようです。

問題を回避するために例外をキャッチして処理することをお勧めします。