Aspose.words,Aspose.cells,vb.net,c#加载许可证,生成操作选择:嵌入的资源

当你已经将许可证文件的生成操作设置为 "嵌入的资源" 后,可在代码里从嵌入资源加载该许可证。以下分别给出 C# 和 VB.NET 的示例代码。

C# 示例

在 C# 的 WinForm 项目中,可在 Form1.cs 文件里添加如下代码来加载嵌入资源中的许可证:

aspose-license-loading-csharpC# 加载嵌入资源中的 Aspose 许可证

cs 复制代码
using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Aspose.Cells;

namespace YourWinFormApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // 加载许可证
            LoadAsposeLicense();
        }

        private void LoadAsposeLicense()
        {
            try
            {
                // 获取当前执行的程序集
                Assembly assembly = Assembly.GetExecutingAssembly();
                // 替换为实际的命名空间和许可证文件名
                string resourceName = "YourNamespace.Aspose.Total.NET.lic";
                // 从程序集获取嵌入资源的流
                using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                {
                    if (stream == null)
                    {
                        MessageBox.Show("未找到嵌入的许可证资源,请检查资源名称。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    // 创建许可证对象
                    License license = new License();
                    // 设置许可证
                    license.SetLicense(stream);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载 Aspose 许可证时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}    

VB.NET 示例

VB.NET 的 WinForm 项目中,可在 Form1.vb 文件里添加如下代码来加载嵌入资源中的许可证:

aspose-license-loading-vbnetVB.NET 加载嵌入资源中的 Aspose 许可证

vbnet 复制代码
Imports System.IO
Imports System.Reflection
Imports System.Windows.Forms
Imports Aspose.Cells

Public Class Form1
    Public Sub New()
        InitializeComponent()
        ' 加载许可证
        LoadAsposeLicense()
    End Sub

    Private Sub LoadAsposeLicense()
        Try
            ' 获取当前执行的程序集
            Dim assembly As Assembly = Assembly.GetExecutingAssembly()
            ' 替换为实际的命名空间和许可证文件名
            Dim resourceName As String = "YourNamespace.Aspose.Total.NET.lic"
            ' 从程序集获取嵌入资源的流
            Using stream As Stream = assembly.GetManifestResourceStream(resourceName)
                If stream Is Nothing Then
                    MessageBox.Show("未找到嵌入的许可证资源,请检查资源名称。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Return
                End If
                ' 创建许可证对象
                Dim license As New License()
                ' 设置许可证
                license.SetLicense(stream)
            End Using
        Catch ex As Exception
            MessageBox.Show($"加载 Aspose 许可证时出错: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
End Class    

代码解释

  1. 获取程序集 :借助 Assembly.GetExecutingAssembly() 方法获取当前执行的程序集。
  2. 获取资源流 :使用 assembly.GetManifestResourceStream(resourceName) 方法从程序集中获取嵌入资源的流。
  3. 检查流是否为空 :要确保成功获取到资源流,若流为 null,则显示错误消息。
  4. 加载许可证 :创建 License 对象,调用 SetLicense 方法并传入资源流来加载许可证。
  5. 异常处理:添加了异常处理代码,若加载许可证时出现错误,会弹出消息框提示用户。

请把 YourNamespace 替换成你项目实际的命名空间。

相关推荐
u***276139 分钟前
C#数据库操作系列---SqlSugar完结篇
网络·数据库·c#
笑非不退2 小时前
C# c++ 实现程序开机自启动
开发语言·c++·c#
许泽宇的技术分享5 小时前
当AI Agent遇上.NET:微软Agent Framework的架构奥秘与实战启示
人工智能·microsoft·.net
周杰伦fans5 小时前
[特殊字符] 代理模式超详细讲解 ——.NET
数据库·c#·代理模式
用户8356290780516 小时前
C# 高效生成 Word 表格:复杂表格创建实战指南
后端·c#
屠夫6 小时前
C# LINQ
c#
SEO-狼术7 小时前
DevExpress DXperience Crack
.net
光头闪亮亮8 小时前
电子发票解析工具-c#桌面应用开发-DataGridView表格控件使用详解
c#
周杰伦fans9 小时前
C# 中的 `Hashtable`
开发语言·c#
lingggggaaaa9 小时前
免杀对抗——C2远控篇&PowerShell&有无文件落地&C#参数调用&绕AMSI&ETW&去混淆特征
c语言·开发语言·笔记·学习·安全·microsoft·c#