LicFileLicenseProvider.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // System.ComponentModel.LicFileLicenseProvider.cs
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. //
  9. // (C) 2003 Martin Willemoes Hansen
  10. // (C) 2003 Andreas Nahr
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System.IO;
  33. namespace System.ComponentModel
  34. {
  35. public class LicFileLicenseProvider : LicenseProvider
  36. {
  37. public LicFileLicenseProvider()
  38. {
  39. }
  40. public override License GetLicense (LicenseContext context,
  41. Type type,
  42. object instance,
  43. bool allowExceptions)
  44. {
  45. try
  46. {
  47. if (context == null || context.UsageMode != LicenseUsageMode.Designtime)
  48. return null;
  49. string path = Path.GetDirectoryName (type.Assembly.Location);
  50. path = Path.Combine (path, type.FullName + ".LIC");
  51. if (!File.Exists (path)) return null;
  52. StreamReader sr = new StreamReader (path);
  53. string key = sr.ReadLine ();
  54. sr.Close ();
  55. if (IsKeyValid (key, type))
  56. return new LicFileLicense (key);
  57. }
  58. catch
  59. {
  60. if (allowExceptions) throw;
  61. }
  62. return null;
  63. }
  64. protected virtual string GetKey (Type type)
  65. {
  66. return (type.FullName + " is a licensed component.");
  67. }
  68. protected virtual bool IsKeyValid (string key, Type type)
  69. {
  70. if (key == null)
  71. return false;
  72. return key.Equals (GetKey (type));
  73. }
  74. }
  75. internal class LicFileLicense: License
  76. {
  77. string _key;
  78. public LicFileLicense (string key)
  79. {
  80. _key = key;
  81. }
  82. public override string LicenseKey
  83. {
  84. get { return _key; }
  85. }
  86. public override void Dispose ()
  87. {
  88. }
  89. }
  90. }