前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >怎么使用Snet工业物联网简易高效的通信库来进行组态使用

怎么使用Snet工业物联网简易高效的通信库来进行组态使用

作者头像
Shunnet
发布2025-02-20 14:29:13
发布2025-02-20 14:29:13
7500
代码可运行
举报
运行总次数:0
代码可运行

以 OpcUa 采集 Mqtt 数据转发,以及脚本解析,反射解析功能的一一讲解,包含代码示例,引用的NUGET库请自行更新到最新版本

以下模拟的是:一个采集对象把数据动态发送至两个传输对象

  • 准备MQTT服务端与客户端使用的配置(配置数据的路径下 config / relay 文件夹下)

两个配置文件里面的内容如下(配置文件名称也有规定的格式,不然组态时无法得到配置)

代码语言:javascript
代码运行次数:0
复制
//Snet.Mqtt.client.MqttClientOperate.mqtt1.Relay.Config.json
{
  "SN": "mqtt1",
  "IpAddress": "127.0.0.1",
  "Port": 8111,
  "UserName": "samples",
  "Password": "samples",
  "ClientID": null,
  "MessageExpirationTime": 10000,
  "QualityOfServiceLevel": "AtMostOnce",
  "RT": "Content"
}

//Snet.Mqtt.client.MqttClientOperate.mqtt2.Relay.Config.json
{
  "SN": "mqtt2",
  "IpAddress": "127.0.0.1",
  "Port": 8222,
  "UserName": "samples",
  "Password": "samples",
  "ClientID": null,
  "MessageExpirationTime": 10000,
  "QualityOfServiceLevel": "AtMostOnce",
  "RT": "Content"
}


Snet.Mqtt.client.MqttClientOperate 也就是程序集的名称
mqtt2 也就是与配置数据中的SN对应的名称
Relay.Config.json  是固定的后缀格式

配置文件规定的格式数据

代码语言:javascript
代码运行次数:0
复制
/// <summary>
/// 库文件统一存放文件夹,绝对路径
/// </summary>
public string? LibFolder { get; set; } = $"{AppDomain.CurrentDomain.BaseDirectory}lib\\relay";

/// <summary>
/// 库配置文件夹,绝对路径
/// </summary>
public string? LibConfigFolder { get; set; } = $"{AppDomain.CurrentDomain.BaseDirectory}config\\relay";

/// <summary>
/// 动态库监控格式
/// </summary>
public string? DllWatcherFormat { get; set; } = "Snet.*.dll";

/// <summary>
/// 配置监控格式
/// </summary>
public string? ConfigWatcherFormat { get; set; } = "*.Relay.Config.json";

/// <summary>
/// 配置文件名称的格式 * 与配置数据中的SN一致
/// 库配置:命名空间 + 类名.SN.Config.json
/// </summary>
public string? ConfigFileNameFormat { get; set; } = "{0}.*.Relay.Config.json";

/// <summary>
/// 配置替换格式
/// </summary>
public string? ConfigReplaceFormat { get; set; } = ".Relay.Config.json";

/// <summary>
/// 接口名称
/// </summary>
public string? InterfaceFullName { get; set; } = "Snet.Model.interface.IRelay";

Mqtt 服务端启动的代码如下

代码语言:javascript
代码运行次数:0
复制
using Snet.Mqtt.service;

//启动 OPCUA 服务端
OpcUaServiceOperate opcUaServiceOperate = OpcUaServiceOperate.Instance(new OpcUaServiceData.Basics
{
    AType = Snet.Opc.core.Data.AuType.UserName,
    UserName = "samples",
    Password = "samples"
});
//输出日志
LogHelper.Info(opcUaServiceOperate.On().ToJson(true));

//启动MQTT服务端 1
MqttServiceOperate mqttServiceOperate1 = MqttServiceOperate.Instance(new MqttServiceData.Basics
{
    MaxNumber = 1000,
    Password = "samples",
    UserName = "samples",
    Port = 8111
});
//输出日志
LogHelper.Info(mqttServiceOperate1.On().ToJson(true));
  • 准备Snet.Mqtt的所有Dll(打包代码在下方)(Dll的路径必须是在 lib / relay 文件夹下)
  • 准备OpcUa服务端来进行数据模拟
代码语言:javascript
代码运行次数:0
复制
//启动 OPCUA 服务端
OpcUaServiceOperate opcUaServiceOperate = OpcUaServiceOperate.Instance(new OpcUaServiceData.Basics
{
    AType = Snet.Opc.core.Data.AuType.UserName,
    UserName = "samples",
    Password = "samples"
});
//输出日志
LogHelper.Info(opcUaServiceOperate.On().ToJson(true));
  • 准备地址的配置数据(也是一个JSON配置文件,在什么路径下都无所谓,因为得自己主动去获取配置,名称也可以自己随便定义,我这就直接放在 config / addressList.json目录下)
代码语言:javascript
代码运行次数:0
复制
{
  "SN": "6CD0DD98C2764375A6DE39BE4B2C5497",
  "AddressArray": [
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Boolean_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Bool",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Byte_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.ByteString_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.DateTime_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Double_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Double",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Duration_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Double",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Float_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Float",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Guid_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Int16_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Short",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Int32_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Int",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Int64_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Long",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Integer_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Short",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.LocaleId_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.LocalizedText_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.NodeId_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.Number_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "Double",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.QualifiedName_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.SByte_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.String_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.UInt16_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "UInt16",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.UInt32_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "UInt32",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.UInt64_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "UInt64",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.UInteger_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.UtcTime_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    },
    {
      "SN": null,
      "AddressName": "ns=2;s=Snet.测试数据.动态常量.XmlElement_Dynamic",
      "AddressAnotherName": null,
      "AddressPropertyName": null,
      "AddressDescribe": "动态常量",
      "AddressExtendParam": null,
      "IsEnable": true,
      "AddressRelayParam": null,
      "AddressParseParam": null,
      "AddressDataType": "String",
      "AddressType": "Reality"
    }
  ],
  "CreationTime": "2025-01-02T02:23:26.4435289+08:00"
}
  • 以上所有的准备工作都已完成就可以开始正式的代码了
代码语言:javascript
代码运行次数:0
复制
//获取地址
string data = FileHandler.FileToString("config\\addressList.json");
Address? address = data.ToJsonEntity<Address>();

//注意,不管是脚本或反射解析,函数入参必须是两个参数,地址名称与地址值,处理完后返回值

#region 动态地址 + 动态单主题转发
foreach (var item in address.AddressArray)
{
    //转发参数 一个主题多个数据
    item.AddressRelayParam = new AddressRelay
    {
        ISns = new List<string> { "Snet.Mqtt.client.MqttClientOperate.mqtt1", "Snet.Mqtt.client.MqttClientOperate.mqtt2" },
        Topic = $"TEST/AddressValue",
        ContentFormat = item.AddressName + " ---- {0}"
    };
}
#endregion

#region 动态地址 + 动态多主题转发 + 脚本解析
//foreach (var item in address.AddressArray)
//{
//    //解析参数
//    item.AddressParseParam = new AddressParse
//    {
//        ScriptParam = new ScriptData.Basics()
//        {
//            ScriptCode = @"function Convert(addressname,value) { return '【这是调用脚本解析】传入的地址是:'+ addressname + '----传入的参数是:' + value; }",
//            ScriptFunction = "Convert",
//            ScriptType = ScriptData.ScriptType.JavaScript
//        }
//    };
//    //转发参数 多个主题
//    item.AddressRelayParam = new AddressRelay
//    {
//        ISns = new List<string> { "Snet.Mqtt.client.MqttClientOperate.mqtt1", "Snet.Mqtt.client.MqttClientOperate.mqtt2" },
//        Topic = $"TEST/AddressValue/{item.AddressName}",
//    };
//}
#endregion

#region 动态地址 + 动态单主题转发 + 反射解析

//foreach (var item in address.AddressArray)
//{
//    //解析参数
//    item.AddressParseParam = new AddressParse
//    {
//        ReflectionParam = new object[]
//                {
//                            new ReflectionData.Basics
//                            {
//                                DllDatas = new List<ReflectionData.DllData>
//                                {
//                                    new ReflectionData.DllData
//                                    {
//                                        DllPath="Snet.DAQ.Samples.DLL.dll",
//                                        IsAbsolutePath=false,
//                                        NamespaceDatas=new List<ReflectionData.NamespaceData>
//                                        {
//                                            new ReflectionData.NamespaceData
//                                            {
//                                                Namespace="Snet.DAQ.Samples.DLL",
//                                                ClassDatas=new List<ReflectionData.ClassData>
//                                                {
//                                                    new ReflectionData.ClassData
//                                                    {
//                                                        ClassName="Class1",
//                                                        SN="Snet.DAQ.Samples.DLL.Class1[Instance]",
//                                                        MethodDatas=new List<ReflectionData.MethodData>
//                                                        {
//                                                            new ReflectionData.MethodData
//                                                            {
//                                                                MethodName="R1",
//                                                                SN="[R1]"
//                                                            },
//                                                             new ReflectionData.MethodData
//                                                            {
//                                                                MethodName="R2",
//                                                                SN="[R2]"
//                                                            },
//                                                              new ReflectionData.MethodData
//                                                            {
//                                                                MethodName="R3",
//                                                                SN="[R3]"
//                                                            }
//                                                        }
//                                                    }
//                                                }
//                                            }
//                                        }

//                                    }
//                                }
//                            },
//                            "Snet.DAQ.Samples.DLL.Class1[Instance][R2]"
//                }
//    };
//    //转发参数 多个主题
//    item.AddressRelayParam = new AddressRelay
//    {
//        ISns = new List<string> { "Snet.Mqtt.client.MqttClientOperate.mqtt1", "Snet.Mqtt.client.MqttClientOperate.mqtt2" },
//        Topic = $"TEST/AddressValue",
//        ContentFormat = item.AddressName + " ---- {0}"
//    };
//}

#endregion

//实例化daq对象
OpcUaClientOperate opcUaClientOperate = OpcUaClientOperate.Instance(new OpcUaClientData.Basics
{
    ServerUrl = "opc.tcp://127.0.0.1:6688/Opc.Ua.Service",
    Password = "samples",
    UserName = "samples",
    CustomName = $"samples-{Guid.NewGuid().ToUpperNString()}",
});
//打开
LogHelper.Info(opcUaClientOperate.On().ToJson(true));
//事件注册
opcUaClientOperate.OnDataEventAsync += OpcUaClientOperate_OnEvent;
opcUaClientOperate.OnInfoEventAsync += OpcUaClientOperate_OnInfoEventAsync;

Console.WriteLine("回车开始订阅数据");
Console.ReadLine();
//订阅数据
LogHelper.Info(opcUaClientOperate.Subscribe(address).ToJson(true));

while (true)
{
    Console.WriteLine("回车开始读取数据");
    Console.ReadLine();
    //读取一次数据
    LogHelper.Info(opcUaClientOperate.Read(address).ToJson(true));
}


//事件消息
Task OpcUaClientOperate_OnEvent(object? sender, EventDataResult e)
{
    //IEnumerable<AddressValueSimplify>? addressValueSimplifies = e.GetSource<ConcurrentDictionary<string, AddressValue>>()?.GetSimplifyArray();
    //if (addressValueSimplifies != null)
    //{
    //    foreach (var item in addressValueSimplifies)
    //    {
    //        Console.WriteLine($"{item.ToJson(true)}\r\n\r\n");
    //    }
    //}

    return Task.CompletedTask;
}

Task OpcUaClientOperate_OnInfoEventAsync(object? sender, EventInfoResult e)
{
    return Task.CompletedTask;
}

动态地址 + 动态多主题转发 + 脚本解析 [效果图]

动态地址 + 动态单主题转发 + 反射解析 [效果图]

动态地址 + 动态单主题转发 [效果图]

Dll打包代码

代码语言:javascript
代码运行次数:0
复制
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
	  <Authors>Shun</Authors>
	  <Product>Tool</Product>
	  <PackageProjectUrl>https://shunnet.top</PackageProjectUrl>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Snet.AllenBradley" Version="25.2.3" />
		<PackageReference Include="Snet.Beckhoff" Version="25.2.3" />
		<PackageReference Include="Snet.DB" Version="25.2.3" />
		<PackageReference Include="Snet.Delta" Version="25.2.3" />
		<PackageReference Include="Snet.Fatek" Version="25.2.3" />
		<PackageReference Include="Snet.Fuji" Version="25.2.3" />
		<PackageReference Include="Snet.GE" Version="25.2.3" />
		<PackageReference Include="Snet.Inovance" Version="25.2.3" />
		<PackageReference Include="Snet.Invt" Version="25.2.3" />
		<PackageReference Include="Snet.Kafka" Version="25.2.3" />
		<PackageReference Include="Snet.Keyence" Version="25.2.3" />
		<PackageReference Include="Snet.LSis" Version="25.2.3" />
		<PackageReference Include="Snet.MegMeet" Version="25.2.3" />
		<PackageReference Include="Snet.Mitsubishi" Version="25.2.3" />
		<PackageReference Include="Snet.Modbus" Version="25.2.3" />
		<PackageReference Include="Snet.Mqtt" Version="25.2.3" />
		<PackageReference Include="Snet.NetMQ" Version="25.2.3" />
		<PackageReference Include="Snet.Netty" Version="25.2.3" />
		<PackageReference Include="Snet.Opc" Version="25.2.3" />
		<PackageReference Include="Snet.Panasonic" Version="25.2.3" />
		<PackageReference Include="Snet.PQDIF" Version="25.2.3" />
		<PackageReference Include="Snet.RabbitMQ" Version="25.2.3" />
		<PackageReference Include="Snet.Siemens" Version="25.2.3" />
		<PackageReference Include="Snet.Omron" Version="25.2.3" />
		<PackageReference Include="Snet.Sim" Version="25.2.3" />
		<PackageReference Include="Snet.TEP" Version="25.2.3" />
		<PackageReference Include="Snet.Toyota" Version="25.2.3" />
		<PackageReference Include="Snet.Vigor" Version="25.2.3" />
		<PackageReference Include="Snet.WeCon" Version="25.2.3" />
		<PackageReference Include="Snet.XinJE" Version="25.2.3" />
		<PackageReference Include="Snet.Yamatake" Version="25.2.3" />
		<PackageReference Include="Snet.Yaskawa" Version="25.2.3" />
		<PackageReference Include="Snet.Yokogawa" Version="25.2.3" />
	</ItemGroup>
</Project>
代码语言:javascript
代码运行次数:0
复制
using System.Diagnostics;

namespace Snet.Pack.Manage.Tool
{
    internal class Program
    {
        public class Details
        {
            public Details(string packName, List<string> libNames)
            {
                this.PackName = packName;
                this.LibNames.AddRange(libNames);
                //创建主目录
                Directory.CreateDirectory(this.PublicPath);
                //创建这个文件夹
                Directory.CreateDirectory(Path.Combine(this.PublicPath, this.PackName));
            }

            /// <summary>
            /// 获取存储路径
            /// </summary>
            /// <returns></returns>
            public string GetPath()
            {
                return Path.Combine(this.PublicPath, this.PackName);
            }

            public string GetFuPath()
            {
                return this.PublicPath;
            }
            private string PublicPath = Path.Combine(Directory.GetParent(Directory.GetParent(Directory.GetParent(Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName).FullName).FullName).FullName).FullName, "pack\\Release");

            /// <summary>
            /// dll 名称 ,默认就包含的
            /// </summary>
            public List<string> LibNames = new List<string>()
            {
                "ClearScript.Core.dll",
                "ClearScript.V8.dll",
                "ClearScript.V8.ICUData.dll",
                "ClearScript.Windows.Core.dll",
                "ClearScript.Windows.dll",
                "KellermanSoftware.Compare-NET-Objects.dll",
                "Microsoft.Win32.SystemEvents.dll",
                "Newtonsoft.Json.dll",
                "Serilog.dll",
                "Serilog.Sinks.Console.dll",
                "Serilog.Sinks.File.dll",
                "System.Configuration.ConfigurationManager.dll",
                "System.Diagnostics.EventLog.dll",
                "System.Drawing.Common.dll",
                "System.IO.Ports.dll",
                "System.Security.Cryptography.ProtectedData.dll",
                "System.Security.Permissions.dll",
                "System.Windows.Extensions.dll",
                "Snet.Core.dll",
                "Snet.Log.dll",
                "Snet.Model.dll",
                "Snet.Unility.dll",
            };

            /// <summary>
            /// 包的名称
            /// </summary>
            public string PackName { get; set; }
        }

        static void Main(string[] args)
        {
            Console.WriteLine("是否需要打包ZIP?(y/n)");
            bool zipPack = Console.ReadLine() == "y";

            //公共库名称
            List<Details> publicLibNames = new List<Details>();
            publicLibNames.Add(new Details("Snet.Kafka.Pack", new List<string>
            {
                "Confluent.Kafka.dll",
                "Snet.Kafka.dll",
            }));

            publicLibNames.Add(new Details("Snet.Opc.Pack", new List<string>
            {
                "BouncyCastle.Cryptography.dll",
                "Microsoft.Extensions.Logging.Abstractions.dll",
                "Opc.Ua.Bindings.Https.dll",
                "Opc.Ua.Client.dll",
                "Opc.Ua.Configuration.dll",
                "Opc.Ua.Core.dll",
                "Opc.Ua.Gds.Client.Common.dll",
                "Opc.Ua.Gds.Server.Common.dll",
                "Opc.Ua.Security.Certificates.dll",
                "Opc.Ua.Server.dll",
                "System.Formats.Asn1.dll",
                "Snet.Opc.dll",
            }));

            publicLibNames.Add(new Details("Snet.Netty.Pack", new List<string>
            {
                "DotNetty.Buffers.dll",
                "DotNetty.Codecs.dll",
                "DotNetty.Common.dll",
                "DotNetty.Handlers.dll",
                "DotNetty.Transport.dll",
                "Microsoft.Extensions.DependencyInjection.Abstractions.dll",
                "Microsoft.Extensions.DependencyInjection.dll",
                "Microsoft.Extensions.Logging.Abstractions.dll",
                "Microsoft.Extensions.Logging.dll",
                "Microsoft.Extensions.Options.dll",
                "Microsoft.Extensions.Primitives.dll",
                "Snet.Netty.dll",
            }));

            publicLibNames.Add(new Details("Snet.NetMQ.Pack", new List<string>
            {
                "AsyncIO.dll",
                "Microsoft.Bcl.AsyncInterfaces.dll",
                "Microsoft.Extensions.ObjectPool.dll",
                "NaCl.dll",
                "NetMQ.dll",
                "System.Private.ServiceModel.dll",
                "System.Security.Cryptography.Pkcs.dll",
                "System.Security.Cryptography.Xml.dll",
                "System.ServiceModel.dll",
                "System.ServiceModel.Primitives.dll",
                "Snet.NetMQ.dll",
            }));


            publicLibNames.Add(new Details("Snet.Mqtt.Pack", new List<string>
            {
                "MQTTnet.AspNetCore.dll",
                "MQTTnet.dll",
                "Snet.Mqtt.dll",
            }));

            publicLibNames.Add(new Details("Snet.DB.Pack", new List<string>
            {
                "BouncyCastle.Cryptography.dll",
                "Dapper.dll",
                "EntityFramework.dll",
                "EntityFramework.SqlServer.dll",
                "Google.Protobuf.dll",
                "K4os.Compression.LZ4.dll",
                "K4os.Compression.LZ4.Streams.dll",
                "K4os.Hash.xxHash.dll",
                "MySql.Data.dll",
                "System.CodeDom.dll",
                "System.Data.OracleClient.dll",
                "System.Data.SqlClient.dll",
                "System.Data.SQLite.dll",
                "System.Data.SQLite.EF6.dll",
                "System.Diagnostics.DiagnosticSource.dll",
                "System.IO.Pipelines.dll",
                "System.Text.Encodings.Web.dll",
                "System.Text.Json.dll",
                "Snet.DB.dll",
                "ZstdSharp.dll",
            }));

            publicLibNames.Add(new Details("Snet.Sim.Pack", new List<string>
            {
                "Snet.Sim.dll",
            }));
            publicLibNames.Add(new Details("Snet.TEP.Pack", new List<string>
            {
                "Snet.TEP.dll",
            }));
            publicLibNames.Add(new Details("Snet.RabbitMQ.Pack", new List<string>
            {
                "RabbitMQ.Client.dll",
                "System.Threading.Channels.dll",
                "Snet.RabbitMQ.dll",
            }));

            publicLibNames.Add(new Details("Snet.AllenBradley.Pack", new List<string> { "Snet.AllenBradley.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Beckhoff.Pack", new List<string> { "Snet.Beckhoff.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Delta.Pack", new List<string> { "Snet.Delta.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Fatek.Pack", new List<string> { "Snet.Fatek.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Fuji.Pack", new List<string> { "Snet.Fuji.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.GE.Pack", new List<string> { "Snet.GE.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Inovance.Pack", new List<string> { "Snet.Inovance.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Invt.Pack", new List<string> { "Snet.Invt.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Keyence.Pack", new List<string> { "Snet.Keyence.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.LSis.Pack", new List<string> { "Snet.LSis.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.MegMeet.Pack", new List<string> { "Snet.MegMeet.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Mitsubishi.Pack", new List<string> { "Snet.Mitsubishi.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Modbus.Pack", new List<string> { "Snet.Modbus.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Panasonic.Pack", new List<string> { "Snet.Panasonic.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.PQDIF.Pack", new List<string> { "Snet.PQDIF.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Siemens.Pack", new List<string> { "Snet.Siemens.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Omron.Pack", new List<string> { "Snet.Omron.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Toyota.Pack", new List<string> { "Snet.Toyota.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Vigor.Pack", new List<string> { "Snet.Vigor.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.WeCon.Pack", new List<string> { "Snet.WeCon.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.XinJE.Pack", new List<string> { "Snet.XinJE.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Yamatake.Pack", new List<string> { "Snet.Yamatake.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Yaskawa.Pack", new List<string> { "Snet.Yaskawa.dll", "Snet.Driver.dll" }));
            publicLibNames.Add(new Details("Snet.Yokogawa.Pack", new List<string> { "Snet.Yokogawa.dll", "Snet.Driver.dll" }));


            foreach (var item in publicLibNames)
            {
                string currentDirectory = Directory.GetCurrentDirectory();
                string[] files = Directory.GetFiles(currentDirectory);
                foreach (string file in files)
                {
                    string fileName = Path.GetFileName(file);
                    if (item.LibNames.FirstOrDefault(c => c == fileName) != null)
                    {
                        File.Copy(file, Path.Combine(item.GetPath(), fileName), true);
                    }
                }

                Console.WriteLine($"{item.PackName} ---- DLL 复制成功 [ √ ]");

            }

            if (zipPack)
            {
                Console.WriteLine("复制完成,正在打包Zip中,请等待...");
                foreach (var item in publicLibNames)
                {
                    string path = item.GetPath();
                    string pathZip = $"{path}.zip";
                    if (File.Exists(pathZip))
                    {
                        File.Delete(pathZip);
                    }

                    System.IO.Compression.ZipFile.CreateFromDirectory(path, pathZip);

                    Console.WriteLine($"{item.PackName}.zip ---- 打包成功 [ √ ]");

                }
            }

            Process.Start("explorer.exe", Path.Combine(publicLibNames[0].GetFuPath()));

        }
    }
}

示例与打包工具下载地址

https://shunnet.lanzouv.com/b0mb86bcb 密码:1r85

感谢观看

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档