终止应用程序,我要终止使用通过vb.net完整的文件路径的应用程序,所以我用这个代码片断使用完整路径
Public Sub forceCopy()
Try
'Dim strDatabasePath As String = My.Computer.FileSystem.CombinePath(Application.UserAppDataPath, "LIC.mdf")
'Dim strdbLogPath As String = My.Computer.FileSystem.CombinePath(Application.UserAppDataPath, "LIC_log.ldf")
Dim strDatabasePath As String = My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC.mdf")
Dim strdbLogPath As String = My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC_log.ldf")
Dim path As String = My.Computer.FileSystem.CombinePath(My.Application.Info.DirectoryPath, "LIC.mdf")
Dim matchingProcesses = New List(Of Process)
For Each process As Process In process.GetProcesses()
For Each m As ProcessModule In process.Modules
If String.Compare(m.FileName, path, StringComparison.InvariantCultureIgnoreCase) = 0 Then
matchingProcesses.Add(process)
Exit For
End If
Next
Next
For Each p As Process In matchingProcesses
p.Kill()
Next
My.Computer.FileSystem.CopyFile(strDatabasePath, "c:\backup\LIC.mdf", True)
My.Computer.FileSystem.CopyFile(strdbLogPath, "c:\backup\LIC_log.ldf", True)
MessageBox.Show("Backup taken successfully")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
我得到一个异常“访问被拒绝”。任何想法为什么?编辑:我得到在这一行的错误:For Each m As ProcessModule In process.Modules
我只需要复制这些文件..任何方式对我来说都是好的......我不想回到/恢复数据库..只是一个简单的副本..但由于“文件被其他进程使用”错误...我不能复制...任何解决方案? – user1150440 2012-01-15 21:16:31
提问并回答。除非你喜欢备份损坏的文件,否则不要*杀死进程。该文件被锁定是有原因的。 – 2012-01-15 21:24:55