使用完整路径终止应用程序 [英] terminate application using full path
问题描述
我想通过 vb.net 使用完整文件路径终止应用程序,所以我使用此代码片段
I want to terminate an application using the full file path via vb.net,so i am using this code snippet
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
I get an exception "Access Is Denied" .Any idea why?
I get the error at this line : For Each m As ProcessModule In process.Modules
推荐答案
您需要使用 Try/Catch 包装 If String.Compare(m.FileName, ...)
块.有几个虚假的特权进程,其 FileName 属性您无法访问.
You'll need to wrap the If String.Compare(m.FileName, ...)
block with Try/Catch. There are several fake and privileged processes whose FileName property you cannot access.
像这样杀死 SQL Server 否则是一个非常糟糕的主意.用 ServiceController 类很好地询问.您将需要 UAC 提升这样做.
Killing SQL Server like this is otherwise a Really Bad Idea. Ask nicely with the ServiceController class. You will need UAC elevation to do so.
这篇关于使用完整路径终止应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!