Hello.
I made my own implementation of form designer (like the one in Visual Studio) for my company's product, so that user of that product could design his own forms. Of course, common users of our product should not be able to use infragistics components in that form designer, because it would be a license violation, but that product could also be used not only by common users, but by our developers too. Because our developers do have proper license, we would like to determine if there is a developer license on a certain computer or not, so that we could add/remove infragistics components to/from form designer toolbox.
Is there any way to do this programmatically?
Hello Nikolay,I am going to provide a simple way to check to see if the NetAdvantage for Windows Forms controls are licensed to be used at design time on the computer. For this I have made the following assumptions:
If these assumptions are correct then you can use the LicenseManager to check the license of a NetAdvantage Control after setting the CurrentContext to a LicenseContext that has a UsageMode of DesignTime. If this doesn't throw any exceptions and UltraLicense is returned that has ProductInfo.Status set to LicenseStatus.Licensed then the machine has a license to use the NetAdvantage controls at design time. After this check the CurrentContext needs to be resotred to the correct context. The following code is an example of what you might use to do this:
class LicenseChecker{ public static bool IsInfragisticsWindowsFormsDesignTimeAllowed() { UltraButton button = new UltraButton(); Type ultraButtonType = typeof(Infragistics.Win.Misc.UltraButton); LicenseContext context = new TestLicenseContext(); LicenseContext currentContext = LicenseManager.CurrentContext; License license = null; LicenseManager.CurrentContext = context; try { LicenseManager.IsValid(ultraButtonType, button, out license); } catch (Exception) { return false; } LicenseManager.CurrentContext = currentContext; UltraLicense ultraLicense = license as UltraLicense; button.Dispose(); return ultraLicense != null && ultraLicense.ProductInfo.Status == LicenseStatus.Licensed; }}public class TestLicenseContext : LicenseContext{ public override LicenseUsageMode UsageMode { get { return LicenseUsageMode.Designtime; } }}
Thanks, that's seems to work fine.
But I'm sure, you had to put "LicenseManager.CurrentContext = currentContext;" in "finally" block, if restoring of the current context is important (and I suppose it is).
Also, there is System.ComponentModel.Design.DesigntimeLicenseContext class in FCL, which, I'm sure, can be used instead of your TestLicenseContext class.
And I hope it's not important to dispose licenses manually, 'cause they seems to be disposable, but you aren't disposing them in your example.
Nikolay,You are correct that the existing System.ComponentModel.Design.DesigntimeLicenseContext from the Framework Class Library should be used rather than creating a new class. Also since the catch of the try catch is returning a value, the restoring of the context should be moved to a finally block as you have suggested as the context wouldn't be restored if it isn't. Alternatively the logic could be refactored so that the catch doesn't return a value and then the context would always be restored if it was restored after the try catch block. Thank you for pointing out these mistakes in my code.Regarding the Dispose method, in the License class Dispose is an abstract method and in the UltraLicense that derives from it there isn't any implementation in the method so it will not make a difference if it is called or not.This is the updated IsInfragisticsWindowsFormsDesignTimeAllowed with these changes:
public static bool IsInfragisticsWindowsFormsDesignTimeAllowed(){ UltraButton button = new UltraButton(); Type ultraButtonType = typeof(Infragistics.Win.Misc.UltraButton); LicenseContext context = new DesigntimeLicenseContext(); LicenseContext currentContext = LicenseManager.CurrentContext; License license = null; LicenseManager.CurrentContext = context; try { LicenseManager.IsValid(ultraButtonType, button, out license); } catch (Exception) { return false; } finally { LicenseManager.CurrentContext = currentContext; } UltraLicense ultraLicense = license as UltraLicense; button.Dispose(); return ultraLicense != null && ultraLicense.ProductInfo.Status == LicenseStatus.Licensed;}