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 替换成你项目实际的命名空间。

相关推荐
水果里面有苹果2 小时前
20-C#构造函数--虚方法
java·前端·c#
深盾科技3 小时前
.NET nupkg包的深度解析与安全防护指南
.net
Tomorrow'sThinker6 小时前
✍️ Python 批量设置 Word 文档多级字体样式(标题/正文/名称/小节)
python·自动化·word·excel
Xiao_zuo_ya6 小时前
SpringBoot-Freemarker导出word
spring boot·word
[纳川]6 小时前
把word中表格转成excle文件
开发语言·c#·word
追逐时光者6 小时前
一个 .NET 开源、免费、以社区为中心的单元测试框架
后端·.net
lph19727 小时前
快速分页wpf
c#
试行8 小时前
C#System.Runtime.InteropServices.ExternalException (0x80004005): GDI+ 中发生一般性错误。
c#
每日出拳老爷子11 小时前
[C#] 使用TextBox换行失败的原因与解决方案:换用RichTextBox的实战经验
开发语言·c#