首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >IO.File.Delete随机UnauthorizedAccessException

IO.File.Delete随机UnauthorizedAccessException
EN

Stack Overflow用户
提问于 2010-11-27 23:41:04
回答 2查看 1.5K关注 0票数 0

我使用My.Computer.Filesystem.WriteAllBytes将存储在应用程序资源中的可执行文件写入到它的启动目录中。运行可执行文件之后,我会删除它。一切都很好;但是,我会无缘无故地随机获得一个UnauthorizedAccessException。获得异常后,我可以手动删除该文件,没有问题。下面是完整的代码:

代码语言:javascript
代码运行次数:0
运行
复制
' 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

令我困惑的是,我实际上只是写出了可执行文件,其他任何东西都不可能使用它。我检查了文件属性,它不是只读的。我的应用程序也是作为管理员运行的。有什么问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-11-27 23:46:36

您不会等待进程退出,因此当您尝试删除该文件时,进程仍在运行。参见Process.WaitForExit

票数 3
EN

Stack Overflow用户

发布于 2010-11-27 23:48:09

看起来,您使用一个单独的进程来写出文件--也许在尝试删除时,这个进程仍然在使用该文件。

我建议抓住并处理异常以避开这个问题。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4294539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档