如何使额外的文件成为我构建的程序集的依赖项?

问题描述 投票:0回答:1

在 Visual Studio 2017 中,我构建了一个类库程序集。程序集需要同一目录中的某些外部文件(rdlc 报告),因此对于项目中的 rdlc 文件,我指定“复制到输出目录”=“始终复制”或“如果较新则复制”,并且它可以工作:构建 rdlc 文件使其进入 bin 文件夹。但我也想让rdlc文件成为dll的依赖项,所以如果我在另一个应用程序中指定dll作为引用,我希望RDLC文件与dll一起自动拾取。如何实现? 我的汇编项目是用 VB 编写的,但在 C# 中可能是一样的。

c# vb.net visual-studio msbuild .net-assembly
1个回答
0
投票

您提到“将报告文件作为单独的文件”,我认为有两种方法可以实现您的要求。

比如我这边有两个项目,VB_dll和VB_usedll:

1、MSBuild方式。

像这样制作VB_usedll的.vbproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <RootNamespace>VB_usedll</RootNamespace>
    <TargetFramework>net6.0</TargetFramework>
    <!-- Define the path to the PNG file you want to check for -->
    <RequiredRDLCFile>path_to_your_rdlc_file.rdlc</RequiredRDLCFile>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="VB_dll">
      <HintPath>..\VB_dll\bin\Debug\net6.0\VB_dll.dll</HintPath>
    </Reference>
  </ItemGroup>

  <Target Name="CheckRequiredRdlcFile" BeforeTargets="Build">
    <Error Text="Required RDLC file is missing: $(RequiredRDLCFile)" Condition="!Exists('$(RequiredRDLCFile)')" />
  </Target>

</Project>

结果:

2、代码方式。

比如VB_dll中的类是这样的:

Imports System.IO

Public Class Class1

    Private Shared requiredRdlcPath As String = "C:\path\to\required.rdlc"

    ' Constructor
    Public Sub New()
        If Not File.Exists(requiredRdlcPath) Then
            Throw New FileNotFoundException($"The required RDLC file is not found at {requiredRdlcPath}.")
        End If
    End Sub

    ' Other methods of your class...

End Class

然后如果你尝试实例化并使用其中的某些方法,当所需的RDLC文件不存在时,会弹出问题:

Imports System
Imports VB_dll
Module Program
    Sub Main(args As String())
        Console.WriteLine("Hello World!")
        Dim libraryInstance As New Class1
    End Sub
End Module

结果:

© www.soinside.com 2019 - 2024. All rights reserved.