AppManifest.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4. using System.Runtime.Serialization.Formatters.Soap;
  5. namespace Microsoft.Samples.AppUpdater
  6. {
  7. [Serializable]
  8. public class AppManifest
  9. {
  10. [NonSerialized]
  11. private string _FilePath;
  12. private ResourceFiles _Resources;
  13. private AppVersionInfo _VersionInfo;
  14. private UpdateState _State;
  15. public string FilePath
  16. {
  17. get
  18. {
  19. return _FilePath;
  20. }
  21. set
  22. {
  23. _FilePath = value;
  24. }
  25. }
  26. public ResourceFiles Resources
  27. {
  28. get
  29. {
  30. return _Resources;
  31. }
  32. set
  33. {
  34. _Resources = value;
  35. }
  36. }
  37. public AppVersionInfo VersionInfo
  38. {
  39. get
  40. {
  41. return _VersionInfo;
  42. }
  43. set
  44. {
  45. _VersionInfo = value;
  46. }
  47. }
  48. public UpdateState State
  49. {
  50. get
  51. {
  52. return _State;
  53. }
  54. set
  55. {
  56. _State = value;
  57. }
  58. }
  59. public AppManifest(string manifestFilePath)
  60. {
  61. FilePath = manifestFilePath;
  62. Resources = new ResourceFiles();
  63. State = new UpdateState();
  64. VersionInfo = new AppVersionInfo();
  65. }
  66. public static AppManifest Load(string manifestFilePath)
  67. {
  68. Stream stream = null;
  69. try
  70. {
  71. if (!File.Exists(manifestFilePath))
  72. {
  73. return new AppManifest(manifestFilePath);
  74. }
  75. IFormatter formatter = new SoapFormatter();
  76. stream = new FileStream(manifestFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
  77. AppManifest appManifest = (AppManifest)formatter.Deserialize(stream);
  78. appManifest.FilePath = manifestFilePath;
  79. stream.Close();
  80. return appManifest;
  81. }
  82. catch (Exception)
  83. {
  84. stream?.Close();
  85. return new AppManifest(manifestFilePath);
  86. }
  87. }
  88. public void Update()
  89. {
  90. Stream stream = null;
  91. try
  92. {
  93. IFormatter formatter = new SoapFormatter();
  94. stream = new FileStream(FilePath, FileMode.Create, FileAccess.Write, FileShare.None);
  95. formatter.Serialize(stream, this);
  96. stream.Close();
  97. }
  98. catch (Exception ex)
  99. {
  100. stream?.Close();
  101. throw ex;
  102. }
  103. }
  104. }
  105. }