通常對於商業軟體來說都會給自己的軟體產品添加使用限制,如時間長度、使用次數等
許可中包括了使用限制的各種資訊,包括:
1、時間長度
2、使用次數
3、版本升級
4、線上註冊
這幾天在http://www.spextreme.com/網站看到其開源的OpenLicense。下載下來試用了一下。覺得不錯。
它的主要特點:圖形化介面建立許可,支援產品許可,512位加密,許可鑰匙,各種許可限制及設計/啟動並執行支援.
這裡下載Open License。
這裡我講一下最簡單的設定許可的方法。
使用VS建立一個Windows工程LicenseDemo,預設裡面有一個Form1.先將其編譯一次。然後使用下載下來的圖形化介面許可建立器來建立許可檔案OpenLicenseBuilder.exe .
這裡添加剛才編譯的LicenseDemo.exe
這樣就添加一個Beta Constraint.然後在屬性視窗中就可以設定其許可的限制
做完之後將其儲存起來.取名規則為namespace.className.lic 這裡我取為LicenseDemo.Form1.lic ,將此檔案添加到VS工程中.
在Form1中我們要對許可進行檢驗:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using OpenLicense;
namespace LicenseDemo
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
[LicenseProvider( typeof( OpenLicense.OpenLicenseProvider ) )]
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;
private License license = null;
public Form1()
{
//
// Windows 表單設計器支援所必需的
//
if(LicenseManager.IsValid( typeof( Form1 ), this, out license ))
{
if( license == null )
{
throw new System.ApplicationException( "A sutable license file couldn't be found" );
}
else if( ((OpenLicenseFile)license).FailureReason != String.Empty )
{
throw new System.ApplicationException( ((OpenLicenseFile)license).FailureReason );
}
}
else
{
MessageBox.Show("沒有正確的許可");
}
InitializeComponent();
//
// TODO: 在 InitializeComponent 調用後添加任何建構函式代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 表單設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text = "Form1";
}
#endregion
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
同時注意許可檔案(lic)是可以加密碼的,上面的許可檔案我沒有進行加密,如果加密碼的話,要在 AssemblyInfo.cs檔案中加上一行代碼,告訴解密碼KEY. [assembly: OpenLicense.AssemblyOpenLicenseKey( "xxxxxx" )]
對於LIC檔案可以作為內嵌資源來使用.但在我正式運行時,我發覺對於執行路徑必須有許可檔案的存在.(如果運行程式需要將上面產生的許可檔案複製到BIN目錄下.).