在表单加载时将引用的DLL复制到本地目录

时间:2015-07-21 11:48:02

标签: .net vb.net dll

我有一个.exe程序,并且由于多种原因将其转换为DLL。这个DLL需要由3个独立的程序引用,在整个公司的各种机器上使用。在运行之前,将在本地复制将引用此DLL的程序。

我们有另一个具有类似设置的程序,尽管只有一个程序使用一个(单独的)DLL。此程序添加了DLL作为参考,CopyLocal设置为True。在表单加载中,程序查看DLL的新副本是否在Q:驱动器上,如果是,则将其复制到本地。这是它用于复制DLL的代码

Private Sub frmFullCasePicking_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        Dim LocalInfo As IO.FileInfo = My.Computer.FileSystem.GetFileInfo("C:\tmp\vbPrintLabelDLL.dll")
        Dim RemoteInfo As IO.FileInfo = My.Computer.FileSystem.GetFileInfo("Q:\vbPrintLabelDLL.dll")
        If LocalInfo.LastWriteTime < RemoteInfo.LastWriteTime Then My.Computer.FileSystem.CopyFile("Q:\vbPrintLabelDLL.dll", "C:\tmp\vbPrintLabelDLL.dll", True)
    Catch ex As Exception
        MsgBox(ex.ToString)
        If My.Computer.FileSystem.FileExists("Q:\vbPrintLabelDLL.dll") Then My.Computer.FileSystem.CopyFile("Q:\vbPrintLabelDLL.dll", "C:\tmp\vbPrintLabelDLL.dll")
    End Try
End Sub

我试图用新的DLL做同样的事情,但我得到一个例外,DLL已经被另一个进程使用了​​。是否有我不知道的环境?在我看来,这个程序所做的程序在运行时加载DLL或任何性质的任何东西都有什么特别之处,它只是复制最新版本(并在整个程序的其余部分使用新版本&#39) ; s run)。

任何建议都将不胜感激。

注意:我可以在这个过程有效的程序和我的&#34; new&#34;之间找到唯一的区别。它并不是为这些项目构建的.NET框架版本。它的工作设置是为.NET framework 2.0构建的,而较新的是3.5

编辑:以下是来自Catch块的确切错误消息。此外,请注意该程序也位于C:\ tmp \&#39;在每台机器上。

The process cannot access the file 'C:\Users\rbattle\Desktop\Documents\dotNet Source Code\vbFullCasePicking - Using DLL\vbFullCasePicking\bin\Release\vbPrintLabelDLL.dll' because it is being used by another process.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)     
    at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)     
    at Microsoft.VisualBasic.FileIO.FileSystem.CopyOrMoveFile(CopyOrMove operation, String sourceFileName, String destinationFileName, Boolean overwrite, UIOptionInternal showUI, UICancelOption onUserCancel)     
    at Microsoft.VisualBasic.MyServices.FileSystemProxy.CopyFile(String sourceFileName, String destinationFileName, Boolean overwrite)     
    at vbFullCasePicking.frmFullCasePicking.frmFullCasePicking_Load(Object sender, EventArgs e) in C:\Users\rbattle\Desktop\Documents\dotNet Source Code\vbFullCasePicking - Using DLL\vbFullCasePicking\frmFullCasePicking.vb:line 103

2 个答案:

答案 0 :(得分:1)

我建议您更改Catch ex As Exception,以便捕获特定类型的异常,而不是所有异常。

try块中的代码可能会抛出任意数量的异常,这些异常会指向您正确的问题(安全性,连接性等)。

答案 1 :(得分:1)

我明白了。在我做过的所有研究中,我没有看到过这一点(尽管我不确定我是在寻找正确的事情),但是this answer给了我解决方案。

他提到在需要之前不会引用DLL。我在表单上有一个类级别成员,它还包含上面的表单加载函数,并且在表单加载函数开始之前处理表单成员时,DLL会在上面的代码运行之前加载。我暂时删除了类级变量,它完全复制了DLL。

为了解决这个问题,我移动代码检查并将DLL复制到ApplicationEvents下的Startup事件中,因此它在加载表单之前复制,因此我的类级别变量仍然可以像我使用它一样使用