J'utilise My.Computer.Filesystem.WriteAllBytes pour écrire un fichier exécutable stocké dans les ressources de mon application dans ce répertoire est de démarrage. Après avoir exécuté l'exécutable, je l'ai ensuite supprimé. Tout fonctionne bien. cependant, je vais obtenir au hasard une exception UnauthorizedAccessException sans raison. Après avoir obtenu l'exception, je peux supprimer manuellement le fichier sans problème. Voici le code complet:IO.File.Delete aléatoire 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
Ce qui me embarrasse est que je littéralement juste écrit sur l'exécutable, et rien d'autre pourrait être l'utiliser. J'ai vérifié les attributs du fichier et ce n'est pas en lecture seule. Mon application fonctionne également en tant qu'administrateur. Quel pourrait être le problème?
Ah, je ne peux pas croire que j'ai oublié ça. J'ai remplacé MAD.Close() avec MAD.WaitForExit() et tout semble bien! Merci! – Steven