【问题标题】:terminate application using full path使用完整路径终止应用程序
【发布时间】:2012-01-16 06:40:02
【问题描述】:

我想通过 vb.net 使用完整文件路径终止应用程序,所以我正在使用此代码 sn-p

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

【问题讨论】:

    标签: vb.net


    【解决方案1】:

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

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

    【讨论】:

    • 我只需要复制这些文件..任何方式对我都有好处...我不想备份/恢复数据库..只是一个简单的副本..但是因为“文件正在被另一个进程使用”错误...我无法复制...任何解决方案??
    • 问和回答。除非您喜欢备份损坏的文件,否则不要不要杀死进程。文件因某种原因被锁定。
    【解决方案2】:

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

    【讨论】:

    • 那么有没有一种方法可以让我知道哪个进程正在使用 .mdf 文件并终止该进程??
    • 使用卷影副本可能会更好。这至少具有处于一致状态的优势。
    • 什么是卷影复制??给我一些好的链接...谢谢。
    猜你喜欢
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 2010-11-26
    • 2020-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多