Program.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Mail;
  8. using System.Net.Mime;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace Fuel01
  12. {
  13. static class Program
  14. {
  15. #pragma warning disable S2223 // Non-constant static fields should not be visible
  16. public static int site = 1;
  17. public static string auteur = "Test";
  18. public static bool okSignalR = false;
  19. public static string key_sta = "";
  20. public static string key_ope = "";
  21. public static string abrev_ope = "";
  22. public static string nom_ope = "";
  23. public static string folder = "";
  24. public static string subfolder = "";
  25. public static bool debug = false;
  26. public static bool isExcel = false;
  27. #pragma warning restore S2223 // Non-constant static fields should not be visible
  28. /// <summary>
  29. /// Point d'entrée principal de l'application.
  30. /// </summary>
  31. [STAThread]
  32. static void Main()
  33. {
  34. debug = (Environment.MachineName == "PCPAT");
  35. var culture = CultureInfo.GetCultureInfo("en-US");
  36. // this may fail sometimes: (see Drachenkatze's comment below)
  37. // var culture = new CultureInfo("en-US");
  38. //Culture for any thread
  39. CultureInfo.DefaultThreadCurrentCulture = culture;
  40. //Culture for UI in any thread
  41. CultureInfo.DefaultThreadCurrentUICulture = culture;
  42. Application.EnableVisualStyles();
  43. Application.SetCompatibleTextRenderingDefault(false);
  44. Application.Run(new Main());
  45. }
  46. public static bool sendMail(string fromEmail, string passwd, string smtpsrv, string toEmail, string subject, string body, string pj)
  47. {
  48. try
  49. {
  50. MailMessage message = new MailMessage();
  51. message.From = new MailAddress(fromEmail);
  52. var seps = new Char[] { ',', ';' };
  53. string[] emails = toEmail.Split(seps);
  54. for (int i = 0; i < emails.Length; i++)
  55. {
  56. message.To.Add(new MailAddress(emails[i]));
  57. }
  58. message.Subject = subject;
  59. ContentType ct = new ContentType();
  60. ct.MediaType = MediaTypeNames.Application.Octet;
  61. if (pj != null)
  62. {
  63. Attachment att = new Attachment(pj, ct);
  64. ContentDisposition cd = att.ContentDisposition;
  65. cd.FileName = pj.Substring(pj.LastIndexOf(@"\") + 1);
  66. message.Attachments.Add(att);
  67. }
  68. message.Body = body;
  69. message.IsBodyHtml = false;
  70. SmtpClient smtp = null;
  71. smtp = new SmtpClient(smtpsrv);
  72. smtp.Credentials = new NetworkCredential(fromEmail, passwd);
  73. smtp.EnableSsl = false;
  74. smtp.Port = 587;
  75. smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  76. smtp.Send(message);
  77. return true;
  78. }
  79. catch (Exception ex)
  80. {
  81. MessageBox.Show(ex.Message, "E-Mail");
  82. return false;
  83. }
  84. }
  85. public static DialogResult ShowInputDialog(string title, string message, ref string input, bool hidden)
  86. {
  87. System.Drawing.Size size = new System.Drawing.Size(220, 70 + 25);
  88. Form inputBox = new Form();
  89. inputBox.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  90. inputBox.ClientSize = size;
  91. inputBox.StartPosition = FormStartPosition.CenterParent;
  92. inputBox.Text = title;
  93. System.Windows.Forms.Label label = new Label();
  94. label.Size = new System.Drawing.Size(size.Width - 10, 23);
  95. label.Location = new System.Drawing.Point(5, 5);
  96. label.Text = message;
  97. inputBox.Controls.Add(label);
  98. System.Windows.Forms.TextBox textBox = new TextBox();
  99. textBox.Size = new System.Drawing.Size(size.Width - 10, 23);
  100. textBox.Location = new System.Drawing.Point(5, 5 + 25);
  101. textBox.Text = input;
  102. if (hidden) textBox.PasswordChar = '*';
  103. inputBox.Controls.Add(textBox);
  104. Button okButton = new Button();
  105. okButton.DialogResult = System.Windows.Forms.DialogResult.OK;
  106. okButton.Name = "okButton";
  107. okButton.Size = new System.Drawing.Size(95, 23);
  108. okButton.Text = "&OK";
  109. okButton.Location = new System.Drawing.Point(5, 39 + 25);
  110. inputBox.Controls.Add(okButton);
  111. Button cancelButton = new Button();
  112. cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
  113. cancelButton.Name = "cancelButton";
  114. cancelButton.Size = new System.Drawing.Size(75, 23);
  115. cancelButton.Text = "&Cancel";
  116. cancelButton.Location = new System.Drawing.Point(5 + 95 + 10, 39 + 25);
  117. inputBox.Controls.Add(cancelButton);
  118. inputBox.AcceptButton = okButton;
  119. inputBox.CancelButton = cancelButton;
  120. DialogResult result = inputBox.ShowDialog();
  121. input = textBox.Text;
  122. return result;
  123. }
  124. public static void traces(string fic, string datas)
  125. {
  126. bool done = false;
  127. int retry = 0;
  128. while (!done && retry < 5)
  129. {
  130. try
  131. {
  132. File.AppendAllText(fic, datas + "\r\n");
  133. done = true;
  134. }
  135. catch (Exception ex)
  136. {
  137. datas += ("\r\n Retry " + retry++.ToString() + " Ex:" + ex.Message);
  138. }
  139. }
  140. }
  141. }
  142. }