首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用完全路径终止应用程序

使用完全路径终止应用程序
EN

Stack Overflow用户
提问于 2012-01-15 20:43:49
回答 2查看 879关注 0票数 1

我想通过vb.net使用完整的文件路径终止应用程序,所以我使用以下代码片段

代码语言:javascript
复制
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

我得到一个例外“访问被拒绝” .Any的想法为什么?编辑:我得到了这一行的错误:For Each m As ProcessModule In process.Modules

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-01-15 21:10:56

您需要用Try/Catch包装If String.Compare(m.FileName, ...)块。有几个您无法访问其FileName属性的假进程和特权进程。

否则,杀死这样的SQL Server是一个非常糟糕的主意。用ServiceController类友好地询问。您需要UAC elevation才能做到这一点。

票数 1
EN

Stack Overflow用户

发布于 2012-01-15 21:04:02

只有提升的进程才能枚举由您不拥有的进程加载的模块。

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

https://stackoverflow.com/questions/8873226

复制
相关文章

相似问题

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