前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >VB.NET 把引用的DLL打包到exe里面,制作绿色软件

VB.NET 把引用的DLL打包到exe里面,制作绿色软件

作者头像
一线编程
发布2021-05-17 15:11:52
2.8K0
发布2021-05-17 15:11:52
举报
文章被收录于专栏:办公魔盒
VB.NET 把引用的DLL打包到exe里面,制作绿色软件

  今天以大家常用DLL"Newtonsoft.Json"为例给大家做个示范;

  • 1、第一步新建项目
  • 2、第二步,新建项目后,在窗体添加一个按钮Button和两个富文本框RichTextBox
  • 3、第三步,VS->工具->NuGet包管理器->管理解决方案的NuGet 程序包->浏览->搜索(Newtonsoft)->安装第1个即可
  • 4、第四步,在项目引用里面找到Newtonsoft引用然后把属性“复制到本地改为False”
  • 5、第五步,在项目上面右击鼠标打开项目属性
  • 6、第六步,在项目属性->资源->添加资源->添加现有文件;然后在项目目录packages->Newtonsoft.Json.13.0.1->lib->net45(根据项目框架选择)->Newtonsoft.Json.dll选择对应的dll文件添加到资源里面
  • 7、第七步,编写测试代码

添加要格式化的json字符串

代码语言:javascript
复制
{“status”:1,“totalcount”:2,“list”:[{“id”:“2305b1e2-4e31-4fd3-8eb6-db57641914df”,“code”:“8147056167227050270”,“title”:“testing”,“type”:“产品”,“status”:“已处理”,“datetime”:“2014-07-12T21:16:46”,“replycontent”:“好的,只是测试”},
{“id”:“3a6546f6-49a7-4a17-b679-b3812b12b27e”,“code”:“8147056167227050269”,“title”:“我建议龙头有多种选配方式”,“type”:“产品”,“status”:“未处理”,“datetime”:“2014-07-12T18:49:08.933”,“replycontent”:""},
{“id”:“f735e461-ca72-4b44-8d7b-cd97ac09802f”,“code”:“8147056167227050268”,“title”:“这个产品不怎么好,不好用”,“type”:“产品”,“status”:“未处理”,“datetime”:“2014-07-12T15:06:19.1”,“replycontent”:""},
{“id”:“15926d9d-f469-4921-b01d-4b48ef8bd93d”,“code”:“7141054273018032465”,“title”:“jdjbcn”,“type”:“服务”,“status”:“未处理”,“datetime”:“2014-05-27T01:03:46.477”,“replycontent”:""},
{“id”:“1debf78f-42b3-4037-b71f-34075eed92bc”,“code”:“4141051277003536211”,“title”:“jdjbxn.x”,“type”:“服务”,“status”:“未处理”,“datetime”:“2014-05-27T00:53:21.18”,“replycontent”:""},
{“id”:“27593c52-b327-4557-8106-b9156df53909”,“code”:“1143051276001357050”,“title”:“ghggghh”,“type”:“服务”,“status”:“未处理”,“datetime”:“2014-05-27T00:35:05.933”,“replycontent”:""},
{“id”:“040198fc-b466-46c1-89d8-0514fbde9480”,“code”:“4142053251166372433”,“title”:“你好,你知道啦,我不喜欢白色浴缸”,“type”:“服务”,“status”:“未处理”,“datetime”:“2014-05-25T16:37:43.853”,“replycontent”:""},
{“id”:“16185418-d461-4e98-83c3-824eb7e344d6”,“code”:“4145058213013197148”,“title”:“hdjbchh”,“type”:“服务”,“status”:“未处理”,“datetime”:“2014-05-21T01:19:14.903”,“replycontent”:""},
{“id”:“6c043404-c1db-42e8-adeb-d4880fa7d1b5”,“code”:“0142051185128085372”,“title”:“ghhjdhd”,“type”:“服务”,“status”:“未处理”,“datetime”:“2014-05-18T12:08:37.997”,“replycontent”:""},
{“id”:“2dca1a38-a32b-4955-a99c-2ed7d6de60fa”,“code”:“3146050186122030382”,“title”:“hsibcn”,“type”:“服务”,“status”:“未处理”,“datetime”:“2014-05-18T12:03:38.913”,“replycontent”:""}]}

json格式化vb代码

代码语言:javascript
复制
    ''' <summary>
    ''' 格式化JSON字符串
    ''' </summary>
    ''' <param name="str"></param>
    ''' <returns></returns>
    Private Function ConvertJsonString(str As String) As String
        Dim serializer As New JsonSerializer()
        Dim tr As TextReader = New StringReader(str)
        Dim jtr As New JsonTextReader(tr)
        Dim obj As Object = serializer.Deserialize(jtr)
        If obj IsNot Nothing Then
            Dim textWriter As New StringWriter()
            Dim jsonWriter As New JsonTextWriter(textWriter) With {
                .Formatting = Formatting.Indented,
                .Indentation = 4,
                .IndentChar = " "c
            }
            serializer.Serialize(jsonWriter, obj)
            Return textWriter.ToString()
        Else
            Return str
        End If
    End Function
  • 8、第八步,加载资源中DLL代码重点来咯
代码语言:javascript
复制
    Public Sub New()
        ''加载DLL到exe的事件
        AddHandler AppDomain.CurrentDomain.AssemblyResolve, New ResolveEventHandler(AddressOf CurrentDomain_AssemblyResolve)
        InitializeComponent()
    End Sub
    
    
 ''' <summary>
  ''' 把DLL加载到EXE中
  ''' </summary>
  ''' <param name="sender"></param>
  ''' <param name="args"></param>
  ''' <returns></returns>
  Private Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
    Dim dllName As String = If(args.Name.Contains(","), args.Name.Substring(0, args.Name.IndexOf(","c)), args.Name.Replace(".dll", ""))
    dllName = dllName.Replace(".", "_")
    If dllName.EndsWith("_resources") Then
      Return Nothing
    End If
    Dim rm As New System.Resources.ResourceManager([GetType].Namespace & ".Resources", System.Reflection.Assembly.GetExecutingAssembly())
    Dim bytes As Byte() = DirectCast(rm.GetObject(dllName), Byte())
    Return System.Reflection.Assembly.Load(bytes)
  End Function
  • 9、完整实现代码
代码语言:javascript
复制
Imports System.IO
Imports Newtonsoft.Json

Public Class Form1

    Public Sub New()
        ''加载DLL到exe的事件
        AddHandler AppDomain.CurrentDomain.AssemblyResolve, New ResolveEventHandler(AddressOf CurrentDomain_AssemblyResolve)
        InitializeComponent()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        RichTextBox2.Text = ConvertJsonString(RichTextBox1.Text)
    End Sub
    ''' <summary>
    ''' 把DLL加载到EXE中
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="args"></param>
    ''' <returns></returns>
    Private Function CurrentDomain_AssemblyResolve(sender As Object, args As ResolveEventArgs) As System.Reflection.Assembly
        Dim dllName As String = If(args.Name.Contains(","), args.Name.Substring(0, args.Name.IndexOf(","c)), args.Name.Replace(".dll", ""))
        dllName = dllName.Replace(".", "_")
        If dllName.EndsWith("_resources") Then
            Return Nothing
        End If
        Dim rm As New System.Resources.ResourceManager([GetType].Namespace & ".Resources", System.Reflection.Assembly.GetExecutingAssembly())
        Dim bytes As Byte() = DirectCast(rm.GetObject(dllName), Byte())
        Return System.Reflection.Assembly.Load(bytes)
    End Function

    ''' <summary>
    ''' 格式化JSON字符串
    ''' </summary>
    ''' <param name="str"></param>
    ''' <returns></returns>
    Private Function ConvertJsonString(str As String) As String
        Dim serializer As New JsonSerializer()
        Dim tr As TextReader = New StringReader(str)
        Dim jtr As New JsonTextReader(tr)
        Dim obj As Object = serializer.Deserialize(jtr)
        If obj IsNot Nothing Then
            Dim textWriter As New StringWriter()
            Dim jsonWriter As New JsonTextWriter(textWriter) With {
                .Formatting = Formatting.Indented,
                .Indentation = 4,
                .IndentChar = " "c
            }
            serializer.Serialize(jsonWriter, obj)
            Return textWriter.ToString()
        Else
            Return str
        End If
    End Function
End Class
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 办公魔盒 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • VB.NET 把引用的DLL打包到exe里面,制作绿色软件
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档