我使用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
令我困惑的是,我实际上只是写出了可执行文件,其他任何东西都不可能使用它。我检查了文件属性,它不是只读的。我的应用程序也是作为管理员运行的。有什么问题吗?
发布于 2010-11-27 23:46:36
您不会等待进程退出,因此当您尝试删除该文件时,进程仍在运行。参见Process.WaitForExit
发布于 2010-11-27 23:48:09
看起来,您使用一个单独的进程来写出文件--也许在尝试删除时,这个进程仍然在使用该文件。
我建议抓住并处理异常以避开这个问题。
https://stackoverflow.com/questions/4294539
复制相似问题