PdnInfo.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using System;
  2. using System.Drawing;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace PaintDotNet
  9. {
  10. /// <summary>
  11. /// 特定的本应用程序相关的功能
  12. /// </summary>
  13. public static class PdnInfo
  14. {
  15. private static Icon appIcon;
  16. public static Icon AppIcon
  17. {
  18. get
  19. {
  20. if (appIcon == null)
  21. {
  22. Stream stream = PdnResources.GetResourceStream("Icons.Metis.ico");
  23. appIcon = new Icon(stream);
  24. stream.Close();
  25. }
  26. return appIcon;
  27. }
  28. }
  29. /// <summary>
  30. /// Gets the full path to where user customization files should be stored.
  31. /// </summary>
  32. /// <returns>
  33. /// User data files should include settings or customizations that don't go into data files such as *.PDN.
  34. /// An example of a user data file is a color palette.
  35. /// </returns>
  36. public static string UserDataPath
  37. {
  38. get
  39. {
  40. string myDocsPath = SystemLayer.Shell.GetVirtualPath(PaintDotNet.SystemLayer.VirtualFolderName.UserDocuments, true);
  41. string userDataDirName = PdnResources.GetString("SystemLayer.UserDataDirName");
  42. string userDataPath = Path.Combine(myDocsPath, userDataDirName);
  43. return userDataPath;
  44. }
  45. }
  46. private static StartupTestType startupTest = StartupTestType.None;
  47. public static StartupTestType StartupTest
  48. {
  49. get
  50. {
  51. return startupTest;
  52. }
  53. set
  54. {
  55. startupTest = value;
  56. }
  57. }
  58. private static bool isTestMode = false;
  59. public static bool IsTestMode
  60. {
  61. get
  62. {
  63. return isTestMode;
  64. }
  65. set
  66. {
  67. isTestMode = value;
  68. }
  69. }
  70. public static DateTime BuildTime
  71. {
  72. get
  73. {
  74. Version version = GetVersion();
  75. DateTime time = new DateTime(2000, 1, 1, 0, 0, 0);
  76. time = time.AddDays(version.Build);
  77. time = time.AddSeconds(version.Revision * 2);
  78. return time;
  79. }
  80. }
  81. private static string GetAppConfig()
  82. {
  83. object[] attributes = typeof(PdnInfo).Assembly.GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
  84. AssemblyConfigurationAttribute aca = (AssemblyConfigurationAttribute)attributes[0];
  85. return aca.Configuration;
  86. }
  87. private static readonly bool isFinalBuild = GetIsFinalBuild();
  88. private static bool GetIsFinalBuild()
  89. {
  90. return !(GetAppConfig().IndexOf("Final") == -1);
  91. }
  92. public static bool IsFinalBuild
  93. {
  94. get
  95. {
  96. return isFinalBuild;
  97. }
  98. }
  99. public static bool IsDebugBuild
  100. {
  101. get
  102. {
  103. #if DEBUG
  104. return true;
  105. #else
  106. return false;
  107. #endif
  108. }
  109. }
  110. // Pre-release builds expire after this many days. (debug+"final" also equals expiration)
  111. public const int BetaExpireTimeDays = 60;
  112. public static DateTime ExpirationDate
  113. {
  114. get
  115. {
  116. if (PdnInfo.IsFinalBuild && !IsDebugBuild)
  117. {
  118. return DateTime.MaxValue;
  119. }
  120. else
  121. {
  122. return PdnInfo.BuildTime + new TimeSpan(BetaExpireTimeDays, 0, 0, 0);
  123. }
  124. }
  125. }
  126. public static bool IsExpired
  127. {
  128. get
  129. {
  130. if (!PdnInfo.IsFinalBuild || PdnInfo.IsDebugBuild)
  131. {
  132. if (DateTime.Now > PdnInfo.ExpirationDate)
  133. {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. }
  140. public static bool HandleExpiration(IWin32Window owner)
  141. {
  142. if (IsExpired)
  143. {
  144. string expiredMessage = PdnResources.GetString("ExpiredDialog.Message");
  145. DialogResult result = MessageBox.Show(expiredMessage, PdnInfo.GetProductName(true),
  146. MessageBoxButtons.OKCancel);
  147. if (result == DialogResult.OK)
  148. {
  149. string expiredRedirect = "http://www.baidu.com"; //InvariantStrings.ExpiredPage;
  150. PdnInfo.LaunchWebSite(owner, expiredRedirect);
  151. }
  152. return false;
  153. }
  154. return true;
  155. }
  156. public static string GetApplicationDir()
  157. {
  158. string appPath = Application.StartupPath;
  159. return appPath;
  160. }
  161. public static string GetProductName()
  162. {
  163. return GetProductName(!IsFinalBuild);
  164. }
  165. public static string GetProductName(bool withTag)
  166. {
  167. string bareProductName = GetBareProductName();
  168. string productNameFormat = PdnResources.GetString("Application.ProductName.Format");
  169. string tag;
  170. if (withTag)
  171. {
  172. string tagFormat = PdnResources.GetString("Application.ProductName.Tag.Format");
  173. tag = string.Format(tagFormat, GetAppConfig());
  174. }
  175. else
  176. {
  177. tag = string.Empty;
  178. }
  179. string version = GetVersionNumberString(GetVersion(), 3);
  180. string productName = string.Format(
  181. productNameFormat,
  182. bareProductName,
  183. version,
  184. tag);
  185. return productName;
  186. }
  187. public static string GetBareProductName()
  188. {
  189. return "Metis Vision";//PdnResources.GetString("Application.ProductName.Bare");
  190. }
  191. private static string copyrightString = null;
  192. public static string GetCopyrightString()
  193. {
  194. /*if (copyrightString == null)
  195. {
  196. string format = " Corporation. {0}";
  197. string allRightsReserved = PdnResources.GetString("Application.Copyright.AllRightsReserved");
  198. copyrightString = string.Format(CultureInfo.CurrentCulture, format, allRightsReserved);
  199. }
  200. return copyrightString;*/
  201. copyrightString = "Copyright 2020 HuiHong. All rights reserved.";
  202. return copyrightString;
  203. }
  204. public static Version GetVersion()
  205. {
  206. return new Version(Application.ProductVersion);
  207. }
  208. private static string GetConfigurationString()
  209. {
  210. object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyConfigurationAttribute), false);
  211. AssemblyConfigurationAttribute aca = (AssemblyConfigurationAttribute)attributes[0];
  212. return aca.Configuration;
  213. }
  214. /// <summary>
  215. /// Returns a full version string of the form: ApplicationConfiguration + BuildType + BuildVersion
  216. /// i.e.: "Beta 2 Debug build 1.0.*.*"
  217. /// </summary>
  218. /// <returns></returns>
  219. public static string GetVersionString()
  220. {
  221. string buildType =
  222. #if DEBUG
  223. "Debug";
  224. #else
  225. "Release";
  226. #endif
  227. string versionFormat = PdnResources.GetString("PdnInfo.VersionString.Format");
  228. string versionText = string.Format(
  229. versionFormat,
  230. GetConfigurationString(),
  231. buildType,
  232. GetVersionNumberString(GetVersion(), 4));
  233. //return versionText;
  234. return "";
  235. }
  236. /// <summary>
  237. /// Returns a string for just the version number, i.e. "3.01"
  238. /// </summary>
  239. /// <returns></returns>
  240. public static string GetVersionNumberString(Version version, int fieldCount)
  241. {
  242. if (fieldCount < 1 || fieldCount > 4)
  243. {
  244. throw new ArgumentOutOfRangeException("fieldCount", "must be in the range [1, 4]");
  245. }
  246. StringBuilder sb = new StringBuilder();
  247. sb.Append(version.Major.ToString());
  248. if (fieldCount >= 2)
  249. {
  250. //sb.AppendFormat(".{0}", version.Minor.ToString("D2"));
  251. }
  252. if (fieldCount >= 3)
  253. {
  254. sb.AppendFormat(".{0}", version.Build.ToString());
  255. }
  256. if (fieldCount == 4)
  257. {
  258. sb.AppendFormat(".{0}", version.Revision.ToString());
  259. }
  260. return sb.ToString();
  261. }
  262. public static string GetFriendlyVersionString()
  263. {
  264. Version version = PdnInfo.GetVersion();
  265. string versionFormat = PdnResources.GetString("PdnInfo.FriendlyVersionString.Format");
  266. string configFormat = PdnResources.GetString("PdnInfo.FriendlyVersionString.ConfigWithSpace.Format");
  267. string config = string.Format(configFormat, GetConfigurationString());
  268. string configText;
  269. if (PdnInfo.IsFinalBuild)
  270. {
  271. configText = string.Empty;
  272. }
  273. else
  274. {
  275. configText = config;
  276. }
  277. string versionText = string.Format(versionFormat, GetVersionNumberString(version, 2), configText);
  278. return versionText;
  279. }
  280. public static string GetFullAppName()
  281. {
  282. return PdnInfo.GetProductName(false);
  283. //string fullAppNameFormat = PdnResources.GetString("PdnInfo.FullAppName.Format");
  284. //string fullAppName = string.Format(fullAppNameFormat, PdnInfo.GetProductName(false), GetVersionString());
  285. //return fullAppName;
  286. }
  287. public static string GetAppName()
  288. {
  289. if (PdnInfo.IsFinalBuild && !PdnInfo.IsDebugBuild)
  290. {
  291. return PdnInfo.GetProductName(false);
  292. }
  293. else
  294. {
  295. return GetFullAppName();
  296. }
  297. }
  298. public static void LaunchWebSite(IWin32Window owner)
  299. {
  300. LaunchWebSite(owner, null);
  301. }
  302. public static void LaunchWebSite(IWin32Window owner, string page)
  303. {
  304. string webSite = "http://www.baidu.com";// InvariantStrings.WebsiteUrl;
  305. Uri baseUri = new Uri(webSite);
  306. Uri uri;
  307. if (page == null)
  308. {
  309. uri = baseUri;
  310. }
  311. else
  312. {
  313. uri = new Uri(baseUri, page);
  314. }
  315. string url = uri.ToString();
  316. if (url.IndexOf("@") == -1)
  317. {
  318. OpenUrl(owner, url);
  319. }
  320. }
  321. public static bool OpenUrl(IWin32Window owner, string url)
  322. {
  323. bool result = SystemLayer.Shell.LaunchUrl(owner, url);
  324. if (!result)
  325. {
  326. string messageFormat = PdnResources.GetString("LaunchLink.Error.Format");
  327. string message = string.Format(messageFormat, url);
  328. MessageBox.Show(owner, message, PdnInfo.GetBareProductName(), MessageBoxButtons.OK, MessageBoxIcon.Error);
  329. }
  330. return result;
  331. }
  332. public static string GetNgenPath()
  333. {
  334. return GetNgenPath(false);
  335. }
  336. public static string GetNgenPath(bool force32bit)
  337. {
  338. string fxDir;
  339. if (UIntPtr.Size == 8 && !force32bit)
  340. {
  341. fxDir = "Framework64";
  342. }
  343. else
  344. {
  345. fxDir = "Framework";
  346. }
  347. string fxPathBase = @"%WINDIR%\Microsoft.NET\" + fxDir + @"\v";
  348. string fxPath = fxPathBase + Environment.Version.ToString(3) + @"\";
  349. string fxPathExp = System.Environment.ExpandEnvironmentVariables(fxPath);
  350. string ngenExe = Path.Combine(fxPathExp, "ngen.exe");
  351. return ngenExe;
  352. }
  353. }
  354. }