我想通过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
答案 0 :(得分:1)
您需要使用Try / Catch包装If String.Compare(m.FileName, ...)
块。有几个假的和特权进程,其FileName属性是您无法访问的。
像这样杀死SQL Server是一个非常糟糕的想法。使用ServiceController类很好地问。您需要UAC elevation才能这样做。
答案 1 :(得分:0)
只有提升的进程才能枚举您不拥有的进程加载的模块。