Vb.net IO.File.Delete随机未授权访问异常
我正在使用My.Computer.Filesystem.writealBytes将存储在应用程序资源中的可执行文件写入其启动目录。运行可执行文件后,我将其删除。一切正常;但是,我会无缘无故地随机获得一个UnauthorizedAccessException。得到异常后,我可以毫无问题地手动删除该文件。以下是完整的代码:Vb.net IO.File.Delete随机未授权访问异常,vb.net,file,exception,delete-file,unauthorized,Vb.net,File,Exception,Delete File,Unauthorized,我正在使用My.Computer.Filesystem.writealBytes将存储在应用程序资源中的可执行文件写入其启动目录。运行可执行文件后,我将其删除。一切正常;但是,我会无缘无故地随机获得一个UnauthorizedAccessException。得到异常后,我可以毫无问题地手动删除该文件。以下是完整的代码: ' Convert MP3 ' First, copy out converter Dim Path = New IO.FileInfo(SoundPath) Try M
' 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
让我困惑的是,我实际上只是写出了可执行文件,其他任何东西都不可能使用它。我已经检查了文件属性,它不是只读的。我的应用程序也以管理员身份运行。可能是什么问题?您没有等待进程退出,因此当您尝试删除文件时,进程仍在运行。请参阅进程。您没有等待进程退出,因此当您尝试删除文件时,进程仍在运行。请参阅进程。看起来您正在使用一个单独的进程来写入文件-当您尝试删除时,可能仍在使用该文件
我建议捕获并处理异常以避开问题 看起来您正在使用一个单独的进程来写入文件-可能在您尝试删除时,该进程仍在使用该文件
我建议捕获并处理异常以避开问题 啊,很好,我不敢相信我忽略了这一点。我用MAD.WaitForExit()替换了MAD.Close(),一切似乎都很好!谢谢啊,很好,我不敢相信我忽略了这一点。我用MAD.WaitForExit()替换了MAD.Close(),一切似乎都很好!谢谢