Form1.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace WindowsFormsApp1
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. private void label1_Click(object sender, EventArgs e)
  21. {
  22. }
  23. string Firmware_str = "";
  24. private void button1_Click(object sender, EventArgs e)
  25. {
  26. //OpenFileDialog fileDialog = new OpenFileDialog();
  27. //fileDialog.Multiselect = true;
  28. //fileDialog.Title = "请选择文件";
  29. //fileDialog.Filter = "程序文件(*.bin)|*.bin|所有文件(*.*)|*.*";
  30. //if (fileDialog.ShowDialog() == DialogResult.OK)
  31. //{
  32. // string file = fileDialog.FileName;
  33. // File_Text.Text = file;
  34. //}
  35. string str = System.Windows.Forms.Application.StartupPath;
  36. //创建对象
  37. OpenFileDialog ofg = new OpenFileDialog();
  38. //判断保存的路径是否存在
  39. if (!Directory.Exists(@str))
  40. {
  41. //创建路径
  42. Directory.CreateDirectory(@str);
  43. }
  44. //设置默认打开路径(绝对路径)
  45. ofg.InitialDirectory = @str;
  46. //设置打开标题、后缀
  47. ofg.Title = "请选择导入dat文件";
  48. ofg.Filter = "dat文件|*.dat";
  49. string path = "";
  50. if (ofg.ShowDialog() == DialogResult.OK)
  51. {
  52. //得到打开的文件路径(包括文件名)
  53. path = ofg.FileName.ToString();
  54. Console.WriteLine("打开文件路径是:" + path);
  55. File_Text.Text = ofg.FileName.ToString();
  56. // File_Text.Text = ofg.FileNames[0].ToString();
  57. // file_open(@str+"/download.bat");
  58. Firmware_str = System.IO.Path.GetFileName(ofg.FileName);
  59. Console.WriteLine( System.IO.Path.GetFileName(ofg.FileName)); //得到文件
  60. }
  61. else if (ofg.ShowDialog() == DialogResult.Cancel)
  62. {
  63. MessageBox.Show("未选择打开文件!");
  64. }
  65. }
  66. private void Form1_Load(object sender, EventArgs e)
  67. {
  68. string str = System.Windows.Forms.Application.StartupPath;
  69. Console.WriteLine(str);
  70. Console.Write(bat_file_str);
  71. Console.WriteLine("十六进制 CC的十进制表示: " + Convert.ToInt16("CC", 16)+ Convert.ToInt16("CC", 16).ToString("X"));
  72. }
  73. string bat_file_str = "cls\n"+
  74. "e k&e p\n" +
  75. "e k&e p\n" +
  76. "e k&e p\n" +
  77. "e k&e p\n" +
  78. "e k&e p\n" +
  79. "e k&e p\n" +
  80. "e k&e p\n" +
  81. "e pu\n" +
  82. "e fr 0 10\n" +
  83. "e fe 10\n" +
  84. "e fp file.dat\n" +
  85. "e fr 0 20\n" +
  86. "e ku\n" +
  87. "e k\n" +
  88. "echo downloadOK\n" +
  89. "timeout /T 3 /NOBREAK";
  90. public void writeBATFile(string filePath,string fileContent)
  91. {
  92. // string filePath = "D:\\test\\testChange.bat";
  93. if (!File.Exists(filePath))
  94. {
  95. FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件
  96. StreamWriter sw = new StreamWriter(fs1);
  97. sw.WriteLine(fileContent);//开始写入值
  98. sw.Close();
  99. fs1.Close();
  100. }
  101. else
  102. {
  103. FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
  104. StreamWriter sr = new StreamWriter(fs);
  105. sr.WriteLine(fileContent);//开始写入值
  106. sr.Close();
  107. fs.Close();
  108. }
  109. }
  110. byte[] FileByteBuffer = new byte[1024 * 1024];
  111. private void file_open(string path)
  112. {
  113. //打开系统日志文件
  114. if (!File.Exists(path))
  115. {
  116. // 目录/文件不存在,建立目录/文件
  117. File.CreateText(path);
  118. }
  119. //打开文件
  120. //FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
  121. //通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
  122. StreamReader m_streamReader = new StreamReader(path, Encoding.GetEncoding("GB2312"));
  123. //使用StreamReader类来读取文件
  124. m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
  125. // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
  126. // this.textBox1.Text = "";
  127. //string strEnd = m_streamReader.ReadToEnd();
  128. //Console.WriteLine(strEnd);
  129. string strLine = m_streamReader.ReadLine();
  130. int Cnt = 0;
  131. while (strLine != null)
  132. {
  133. FileByteBuffer[Cnt] = Convert.ToByte(strLine, 16);
  134. //Console.WriteLine("dat:" + strLine + " len:" + strLine.Length+" "+FileByteBuffer[Cnt].ToString("X"));
  135. Cnt++;
  136. strLine = m_streamReader.ReadLine();
  137. }
  138. Console.WriteLine(Cnt);
  139. //C# 创建二进制文件并写入
  140. BinaryWriter bw = new BinaryWriter(new FileStream("mydata.bin", FileMode.Create));
  141. bw.Write(FileByteBuffer,0, Cnt);//写入一个字符串
  142. bw.Close();
  143. //关闭此StreamReader对象
  144. MessageBox.Show("转换完毕!");
  145. //关闭此StreamReader对象
  146. m_streamReader.Close();
  147. }
  148. private void button2_Click(object sender, EventArgs e)
  149. {
  150. if(!(Firmware_str!=""&& Firmware_str.IndexOf(".dat")!=-1))
  151. {
  152. MessageBox.Show("未选择打开dat文件!");
  153. return;
  154. }
  155. string app_path_str = System.Windows.Forms.Application.StartupPath;
  156. string writeStr= bat_file_str.Replace("file.dat", Firmware_str);
  157. Console.WriteLine(writeStr);
  158. writeBATFile(app_path_str+ "/user_download.bat", writeStr);
  159. Process proc = null;
  160. try
  161. {
  162. string targetDir = string.Format(@app_path_str);//this is where mybatch.bat lies
  163. proc = new Process();
  164. proc.StartInfo.WorkingDirectory = targetDir;
  165. proc.StartInfo.FileName = "user_download.bat";
  166. proc.StartInfo.Arguments = string.Format("10");//this is argument
  167. proc.StartInfo.CreateNoWindow = false;
  168. proc.Start();
  169. proc.WaitForExit();
  170. }
  171. catch (Exception ex)
  172. {
  173. Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
  174. }
  175. }
  176. private void button3_Click(object sender, EventArgs e)
  177. {
  178. if (File_Text.Text != "")
  179. {
  180. file_open(File_Text.Text);
  181. }
  182. else {
  183. MessageBox.Show("未选择打开dat文件!");
  184. }
  185. }
  186. string read_mac =
  187. "e pu\n" +
  188. "e fr 18c6 10\n" +
  189. "timeout /T 3 /NOBREAK";
  190. private void button4_Click(object sender, EventArgs e)
  191. {
  192. string app_path_str = System.Windows.Forms.Application.StartupPath;
  193. string writeStr = bat_file_str.Replace("file.dat", Firmware_str);
  194. Console.WriteLine(writeStr);
  195. writeBATFile(read_mac + "/readmac.bat", writeStr);
  196. Process proc = null;
  197. try
  198. {
  199. string targetDir = string.Format(@app_path_str);//this is where mybatch.bat lies
  200. proc = new Process();
  201. proc.StartInfo.WorkingDirectory = targetDir;
  202. proc.StartInfo.FileName = "user_download.bat";
  203. proc.StartInfo.Arguments = string.Format("10");//this is argument
  204. proc.StartInfo.CreateNoWindow = false;
  205. proc.Start();
  206. proc.WaitForExit();
  207. }
  208. catch (Exception ex)
  209. {
  210. Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
  211. }
  212. }
  213. private void button5_Click(object sender, EventArgs e)
  214. {
  215. }
  216. }
  217. }