Program.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using Microsoft.Toolkit.Uwp.Notifications;
  2. using System;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Windows.Forms;
  6. using Windows.Networking;
  7. using 新一代CaptB点名器.Properties;
  8. namespace 新一代CaptB点名器
  9. {
  10. internal static class Program
  11. {
  12. /// <summary>
  13. /// 应用程序的主入口点。
  14. /// </summary>
  15. [STAThread]
  16. static void Main()
  17. {
  18. Application.EnableVisualStyles();
  19. Application.SetCompatibleTextRenderingDefault(false);
  20. string[] args = Environment.GetCommandLineArgs();
  21. if (args.Length == 2)
  22. Application.Run(new Form1(args[1]));
  23. else
  24. Application.Run(new Form1(null));
  25. }
  26. public static int num;
  27. public static int called_num = 0;
  28. public static string called_name = string.Empty;
  29. public static string ProgramData = "%PROGRAMDATA%";
  30. public static string ProgramDatadirectoryPath = Environment.ExpandEnvironmentVariables(ProgramData) + @"\CBRC";
  31. public static string cbcfpath = ProgramDatadirectoryPath + @"\cbcf.file";
  32. public static string cfpath = ProgramDatadirectoryPath + @"\cf.file";
  33. public static void Import(string ImportFilePath)
  34. {
  35. try
  36. {
  37. File.Copy(ImportFilePath, cbcfpath, true);
  38. File.Copy(ImportFilePath, cfpath, true);
  39. string[] strings = File.ReadAllLines(cfpath);
  40. int.TryParse(strings[0], out num);
  41. }
  42. catch (Exception ex)
  43. {
  44. MessageBox.Show(ex.Message, Resources.ImportErrTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
  45. }
  46. Form1 form1 = new Form1(null);
  47. form1.notifyIcon1.ShowBalloonTip(1000, Resources.ImportToastTitle, Resources.ImportToastContent, ToolTipIcon.Info);
  48. }
  49. public static void Save()
  50. {
  51. SaveFileDialog saveFileDialog = new SaveFileDialog();
  52. saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
  53. saveFileDialog.Filter = "CaptB点名器名单文件(*.cbcf)|*.cbcf|文本文档(*.txt)|*.txt";
  54. saveFileDialog.Title = Resources.SaveFileDialogTitle;
  55. if (saveFileDialog.ShowDialog() == DialogResult.OK)
  56. {
  57. string SaveFilePath = saveFileDialog.FileName;
  58. try
  59. {
  60. File.Copy(cbcfpath, SaveFilePath);
  61. Form1 form1 = new Form1(null);
  62. form1.notifyIcon1.ShowBalloonTip(1000, Resources.SaveToastTitle, Resources.SaveToastContent, ToolTipIcon.Info);
  63. }
  64. catch (Exception ex)
  65. {
  66. MessageBox.Show(ex.Message, Resources.SaveErrTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
  67. }
  68. }
  69. }
  70. public static void call()
  71. {
  72. try
  73. {
  74. Retry:
  75. Random random = new Random();
  76. int n = random.Next(1, num + 1);
  77. StreamReader streamReader = new StreamReader(cfpath);
  78. string str = streamReader.ReadToEnd();
  79. string[] strings = Regex.Split(str, "\r\n");
  80. string lastname = strings[strings.Length - n];
  81. streamReader.Close();
  82. if (called_num == num)
  83. {
  84. if (MessageBox.Show(Resources.OverflowContent, Resources.OverflowTitle, MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
  85. {
  86. Reset();
  87. }
  88. }
  89. else
  90. {
  91. if (lastname == "")
  92. goto Retry;
  93. called_name = lastname;
  94. if (Settings.Default.doDelete == true)
  95. {
  96. called_num++;
  97. string s = File.ReadAllText(cfpath);
  98. s = s.Replace(called_name, string.Empty);
  99. File.WriteAllText(cfpath, s);
  100. }
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. MessageBox.Show(ex.Message, Resources.OverflowTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
  106. called_num = 0;
  107. called_name = Settings.Default.InitialWord;
  108. }
  109. }
  110. public static void Reset()
  111. {
  112. try
  113. {
  114. File.Delete(cfpath);
  115. File.Copy(cbcfpath, cfpath, true);
  116. called_num = 0;
  117. called_name = Settings.Default.InitialWord;
  118. Form1 form1 = new Form1(null);
  119. form1.notifyIcon1.ShowBalloonTip(1000, Resources.ResetToastTitle, Resources.ResetToastContent, ToolTipIcon.Info);
  120. }
  121. catch (Exception ex)
  122. {
  123. MessageBox.Show(ex.Message, Resources.ResetErrTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
  124. }
  125. }
  126. }
  127. }