关闭后删除文件

时间:2017-11-21 19:54:22

标签: vb.net

我试图让我的应用程序在Acrobat Reader(带有此文件)关闭后删除pdf文件。 如果我这样做:

    Dim MyProc = Process.Start(path)
    MyProc.WaitForExit()
    My.Computer.FileSystem.DeleteFile(path)

Acrobat给我一个错误:"打开此文档时出错。无法找到此文件。"

任何帮助将不胜感激,谢谢

修改

我稍微修改了一下代码......

{{1}}

它会在关闭后删除PDF,但如果还有其他PDF打开则不会删除...

2 个答案:

答案 0 :(得分:1)

您可以使用流程中的事件来完成工作。

    Dim MyProc = System.Diagnostics.Process.Start("d:\test.pdf")
    AddHandler MyProc.Exited, Sub() My.Computer.FileSystem.DeleteFile("d:\test.pdf")

答案 1 :(得分:0)

你可以添加一个计时器并设置计时器来检查acrobart.exe是否正在运行:

 Public sub Btn1_click

  Process.Start("file path here")

  Timer1.start

  Public sub Timer1_tick

  Dim pr As Process() = Process.GetProcessesByName("acrobat") 'change acrobat with necessary exe name as i can't remember the exact name of acrobat.exe , it could be acrobat32 or acrobat64 or just acrobat as mentioned
  If pr.Length = 0 Then
  timer1.stop
  My.Computer.FileSystem.DeleteFile("d:\test.pdf")
   Else   
  End If

这将完成工作!

相关问题