首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >System.IO.FileNotFoundException和AutoCAD支持文件搜索路径

System.IO.FileNotFoundException和AutoCAD支持文件搜索路径
EN

Stack Overflow用户
提问于 2022-01-18 16:43:04
回答 1查看 257关注 0票数 -1

我正在创建一个具有“重新宿主工作流设计器”的程序(就像Autodesk SubAssembly Composer那样),它无法创建工具箱控件。

发生的错误: System.IO.FileNotFoundException:无法加载文件或程序集.

到目前为止,我理解,因为DLL不在GAC中,所以windows无法找到它。

因此,我检验了以下假设:

  1. NETLOAD第一次,在调用命令时,没有在“支持文件搜索路径”中设置DLL文件夹,结果:

  1. 将DLL文件夹添加到“支持文件搜索路径”,然后在调用“
  2. :WORKS

”命令时添加

  1. 通过"LOADCTRLS“键(在IExtensionApplication.Initialize中定义)启用自动加载,而无需在调用命令”

:ERROR“时在”支持文件搜索路径“中添加DLL。

  1. 现在dll是由注册表("LOADCTRLS“)加载的,在调用命令”

:ERROR“时仍然没有”支持文件搜索路径“。

  1. 在与步骤4相同的cad会话中,将DLL添加到“支持文件搜索路径”,在调用命令“

:ERROR”时编辑字段而不单击“浏览”按钮。

  1. 在与步骤5相同的cad会话中,将DLL添加到“支持文件搜索路径”,在调用命令“

:ERROR”时单击“浏览”按钮编辑字段。

  1. 在与步骤6相同的cad会话中,我调用NETLOAD命令(filedia=1),导航到DLL文件夹,不选择任何内容,并在调用命令“filedia=1: WORKS

”时单击“取消”。

  1. I关闭cad,再次打开它,在调用命令时设置FILEDIA=0:

在步骤8 ( NETLOAD )相同的cad会话中,我在调用命令时通知DLL的完整路径。

在与步骤9,,FILEDIA=1,NETLOAD相同的cad会话中,我导航到文件夹,但在调用命令“FILEDIA=1: WORKS”时,没有像步骤7那样选择任何内容。

因此,它的结论是,NETLOAD命令不只是执行"Assembly.LoadFrom“。

而且,它与FILEDIA=1一起工作的事实使我相信,正是代码的一部分显示了NETLOAD窗口,从而使DLL或其类型可见,并且没有发生System.IO.FileNotFoundException错误。

我添加了代码这样你就可以看到。如果你能帮我,谢谢

"MyCustomActivity.vb“

代码语言:javascript
运行
复制
Imports System.Activities
Imports System.Activities.Presentation
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows
Imports System.Windows.Media
Imports System.Windows.Media.Imaging

<ToolboxBitmap(GetType(MyCustomActivity), "arc.png"), Designer(GetType(MyCustomDesigner))>
Public Class MyCustomActivity
    Inherits CodeActivity

    Protected Overrides Sub Execute(context As CodeActivityContext)
        'faz alguma coisa
    End Sub
End Class

Public NotInheritable Class MyCustomDesigner
    Inherits MyActivityDesigner
    Public Sub New()
        MyBase.New(GetType(MyCustomActivity))
    End Sub
End Class


Public MustInherit Class MyActivityDesigner
    Inherits ActivityDesigner

    Public Sub New(customActivityType As Type)
        Dim toolAttrib As ToolboxBitmapAttribute = customActivityType.GetCustomAttributes(GetType(ToolboxBitmapAttribute), True)(0)
        Using bitmap As New Bitmap(toolAttrib.GetImage(customActivityType))
            Dim ImageDrawing As New ImageDrawing(System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap,
                                                                            IntPtr.Zero,
                                                                            Int32Rect.Empty,
                                                                            BitmapSizeOptions.FromEmptyOptions),
                                      New Rect(0, 0, 16, 16))
            Me.Icon = New DrawingBrush(ImageDrawing)
        End Using
    End Sub
End Class

"TestClass.VB“

代码语言:javascript
运行
复制
Imports System.Reflection
Imports Microsoft.Win32
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.DatabaseServices
Imports System.IO

Public NotInheritable Class TesteClass
    'precisa ser public, senao IExtensionApplication nao funciona
    Implements IExtensionApplication

    Public Shared ReadOnly PRODUCTKEY As String = HostApplicationServices.Current.UserRegistryProductRootKey
    Public Shared ReadOnly LOADER As String = Reflection.Assembly.GetExecutingAssembly.Location
    Public Shared ReadOnly PASTA As String = New FileInfo(LOADER).DirectoryName

    Public Sub Initialize() Implements IExtensionApplication.Initialize
        Try

            Dim cUserApp = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(PRODUCTKEY & "\Applications", True)

            With cUserApp.CreateSubKey(My.Application.Info.Title)
                .SetValue("DESCRIPTION", My.Application.Info.Description)

                .SetValue("MANAGED", 1, RegistryValueKind.DWord)

                .SetValue("LOADCTRLS", 2, RegistryValueKind.DWord)

                .SetValue("LOADER", System.Reflection.Assembly.GetExecutingAssembly.Location)

                Try
                    .DeleteSubKey("Commands")
                Catch
                End Try

                With .CreateSubKey("Commands")
                    For Each tp As Type In Assembly.GetExecutingAssembly.GetExportedTypes()
                        For Each meth As MethodInfo In tp.GetMethods()
                            For Each obj As CommandMethodAttribute In meth.GetCustomAttributes(GetType(CommandMethodAttribute), True)
                                .SetValue(obj.GlobalName, obj.GlobalName)
                            Next
                        Next
                    Next
                End With
            End With

        Catch
        End Try

    End Sub

    Public Sub Terminate() Implements IExtensionApplication.Terminate
    End Sub

    <CommandMethod("teste")>
    Public Sub teste()

        Dim f As New Form1
        f.ShowDialog()
    End Sub
End Class

"Form1.vb“

代码语言:javascript
运行
复制
Imports System.Activities
Imports System.Activities.Core.Presentation
Imports System.Activities.Presentation
Imports System.Activities.Presentation.Toolbox
Imports System.Activities.Statements
Imports System.Windows
Imports System.Windows.Forms



Public Class Form1
    Private Shared _firsload As Boolean = True

    Public Sub New()
        Dim dd As New DesignerMetadata()
        dd.Register()

        ' Esta chamada é requerida pelo designer.
        InitializeComponent()

        ' Adicione qualquer inicialização após a chamada InitializeComponent().

        Dim WorkflowDesigner As New WorkflowDesigner
        Dim ActivityBuilder As New ActivityBuilder With {.Implementation = New Flowchart}

        WorkflowDesigner.Load(ActivityBuilder)

        Dim ctrl As New ToolboxControl()
        'Dim item As New ToolboxItemWrapper("TesteCAD.MyCustomActivity", TesteClass.LOADER, "", "teste")
        Dim item As New ToolboxItemWrapper(GetType(MyCustomActivity), "teste")
        ctrl.Categories.Add(New ToolboxCategory("teste") From {item})

        ElementHost1.Child = ctrl
        ElementHost2.Child = WorkflowDesigner.View
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        Dim wd As New WorkflowDesigner

        Using lixo As New Form With {.Size = New System.Drawing.Size(1, 1), .ShowInTaskbar = False, .FormBorderStyle = FormBorderStyle.None}
            Dim elementHost As New Integration.ElementHost
            lixo.Controls.Add(elementHost)
            elementHost.Child = wd.View

            AddHandler lixo.Activated, Sub()
                                           Forms.Application.DoEvents()
                                           lixo.Close()
                                       End Sub
            lixo.ShowDialog()
        End Using

    End Sub
End Class

"Form1.Designer.vb“

代码语言:javascript
运行
复制
Imports System.Windows.Forms

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Descartar substituições de formulário para limpar a lista de componentes.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Exigido pelo Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'OBSERVAÇÃO: o procedimento a seguir é exigido pelo Windows Form Designer
    'Pode ser modificado usando o Windows Form Designer.  
    'Não o modifique usando o editor de códigos.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.SplitContainer1 = New System.Windows.Forms.SplitContainer()
        Me.ElementHost1 = New System.Windows.Forms.Integration.ElementHost()
        Me.ElementHost2 = New System.Windows.Forms.Integration.ElementHost()
        CType(Me.SplitContainer1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SplitContainer1.Panel1.SuspendLayout()
        Me.SplitContainer1.Panel2.SuspendLayout()
        Me.SplitContainer1.SuspendLayout()
        Me.SuspendLayout()
        '
        'SplitContainer1
        '
        Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.SplitContainer1.Location = New System.Drawing.Point(0, 0)
        Me.SplitContainer1.Name = "SplitContainer1"
        '
        'SplitContainer1.Panel1
        '
        Me.SplitContainer1.Panel1.Controls.Add(Me.ElementHost1)
        '
        'SplitContainer1.Panel2
        '
        Me.SplitContainer1.Panel2.Controls.Add(Me.ElementHost2)
        Me.SplitContainer1.Size = New System.Drawing.Size(800, 450)
        Me.SplitContainer1.SplitterDistance = 266
        Me.SplitContainer1.TabIndex = 0
        '
        'ElementHost1
        '
        Me.ElementHost1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ElementHost1.Location = New System.Drawing.Point(0, 0)
        Me.ElementHost1.Name = "ElementHost1"
        Me.ElementHost1.Size = New System.Drawing.Size(266, 450)
        Me.ElementHost1.TabIndex = 0
        Me.ElementHost1.Text = "ElementHost1"
        Me.ElementHost1.Child = Nothing
        '
        'ElementHost2
        '
        Me.ElementHost2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ElementHost2.Location = New System.Drawing.Point(0, 0)
        Me.ElementHost2.Name = "ElementHost2"
        Me.ElementHost2.Size = New System.Drawing.Size(530, 450)
        Me.ElementHost2.TabIndex = 0
        Me.ElementHost2.Text = "ElementHost2"
        Me.ElementHost2.Child = Nothing
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Controls.Add(Me.SplitContainer1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.SplitContainer1.Panel1.ResumeLayout(False)
        Me.SplitContainer1.Panel2.ResumeLayout(False)
        CType(Me.SplitContainer1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.SplitContainer1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents SplitContainer1 As SplitContainer
    Friend WithEvents ElementHost1 As Integration.ElementHost
    Friend WithEvents ElementHost2 As Integration.ElementHost
End Class
EN

回答 1

Stack Overflow用户

发布于 2022-01-20 12:47:15

通过将AssemblyResolve事件添加到当前的AppDomain中,我找到了一个解决方案:

代码语言:javascript
运行
复制
Private Shared Function MyHandler(ByVal source As Object, ByVal e As ResolveEventArgs) As Assembly
        Return If(e.Name = GetType(TesteClass).Assembly.FullName, Assembly.LoadFile(LOADER), Nothing)
    End Function

我在‘Initialize’中添加了以下一行:

代码语言:javascript
运行
复制
AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf MyHandler

工作得很好!

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

https://stackoverflow.com/questions/70759262

复制
相关文章

相似问题

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