Program.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* Copyright (C) 2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Windows.Forms;
  13. namespace ISCSIConsole
  14. {
  15. public partial class Program
  16. {
  17. /// <summary>
  18. /// The main entry point for the application.
  19. /// </summary>
  20. [STAThread]
  21. static void Main(string[] args)
  22. {
  23. Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
  24. AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
  25. if (args.Length > 0)
  26. {
  27. if (args[0] == "/help")
  28. {
  29. }
  30. if (args[0] == "/log")
  31. {
  32. string path = Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
  33. if (!path.EndsWith(@"\"))
  34. {
  35. path += @"\";
  36. }
  37. path += String.Format("Log {0}.txt", DateTime.Now.ToString("yyyy-MM-dd HH-mm"));
  38. bool success = OpenLogFile(path);
  39. if (!success)
  40. {
  41. MessageBox.Show("Cannot open log file", "Error");
  42. }
  43. }
  44. else
  45. {
  46. StringBuilder builder = new StringBuilder();
  47. builder.AppendLine("Command line arguments:");
  48. builder.AppendLine("/log - will write log file to executable directory");
  49. MessageBox.Show(builder.ToString(), "Error");
  50. return;
  51. }
  52. }
  53. Application.EnableVisualStyles();
  54. Application.SetCompatibleTextRenderingDefault(false);
  55. Application.Run(new MainForm());
  56. }
  57. public static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
  58. {
  59. HandleUnhandledException(e.Exception);
  60. }
  61. private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
  62. {
  63. if (e.ExceptionObject != null)
  64. {
  65. Exception ex = (Exception)e.ExceptionObject;
  66. HandleUnhandledException(ex);
  67. }
  68. }
  69. private static void HandleUnhandledException(Exception ex)
  70. {
  71. string message = String.Format("Exception: {0}: {1} Source: {2} {3}", ex.GetType(), ex.Message, ex.Source, ex.StackTrace);
  72. MessageBox.Show(message, "Error");
  73. Application.Exit();
  74. }
  75. }
  76. }