0

My.Computer.Filesystem.WriteAllBytes を使用して、アプリケーションのリソースに保存されている実行可能ファイルをスタートアップ ディレクトリに書き出しています。実行可能ファイルを実行したら、それを削除します。すべて正常に動作します。ただし、理由もなく 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

私を困惑させているのは、文字通り実行可能ファイルを書き出しただけで、他に何も使用していない可能性があることです。ファイルの属性を確認しましたが、読み取り専用ではありません。私のアプリケーションは管理者としても実行されています。何が問題なのですか?

4

2 に答える 2

3

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

于 2010-11-27T23:46:36.263 に答える
1

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

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

于 2010-11-27T23:48:09.833 に答える