123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- string Firmware_str = "";
- private void button1_Click(object sender, EventArgs e)
- {
- //OpenFileDialog fileDialog = new OpenFileDialog();
- //fileDialog.Multiselect = true;
- //fileDialog.Title = "请选择文件";
- //fileDialog.Filter = "程序文件(*.bin)|*.bin|所有文件(*.*)|*.*";
- //if (fileDialog.ShowDialog() == DialogResult.OK)
- //{
- // string file = fileDialog.FileName;
- // File_Text.Text = file;
- //}
- string str = System.Windows.Forms.Application.StartupPath;
- //创建对象
- OpenFileDialog ofg = new OpenFileDialog();
- //判断保存的路径是否存在
- if (!Directory.Exists(@str))
- {
- //创建路径
- Directory.CreateDirectory(@str);
- }
- //设置默认打开路径(绝对路径)
- ofg.InitialDirectory = @str;
- //设置打开标题、后缀
- ofg.Title = "请选择导入dat文件";
- ofg.Filter = "dat文件|*.dat";
- string path = "";
- if (ofg.ShowDialog() == DialogResult.OK)
- {
- //得到打开的文件路径(包括文件名)
- path = ofg.FileName.ToString();
- Console.WriteLine("打开文件路径是:" + path);
-
- File_Text.Text = ofg.FileName.ToString();
- // File_Text.Text = ofg.FileNames[0].ToString();
- // file_open(@str+"/download.bat");
- Firmware_str = System.IO.Path.GetFileName(ofg.FileName);
- Console.WriteLine( System.IO.Path.GetFileName(ofg.FileName)); //得到文件
- }
- else if (ofg.ShowDialog() == DialogResult.Cancel)
- {
- MessageBox.Show("未选择打开文件!");
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- string str = System.Windows.Forms.Application.StartupPath;
- Console.WriteLine(str);
- Console.Write(bat_file_str);
- Console.WriteLine("十六进制 CC的十进制表示: " + Convert.ToInt16("CC", 16)+ Convert.ToInt16("CC", 16).ToString("X"));
- }
- string bat_file_str = "cls\n"+
- "e k&e p\n" +
- "e k&e p\n" +
- "e k&e p\n" +
- "e k&e p\n" +
- "e k&e p\n" +
- "e k&e p\n" +
- "e k&e p\n" +
- "e pu\n" +
- "e fr 0 10\n" +
- "e fe 10\n" +
- "e fp file.dat\n" +
- "e fr 0 20\n" +
- "e ku\n" +
- "e k\n" +
- "echo downloadOK\n" +
- "timeout /T 3 /NOBREAK";
- public void writeBATFile(string filePath,string fileContent)
- {
- // string filePath = "D:\\test\\testChange.bat";
- if (!File.Exists(filePath))
- {
- FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件
- StreamWriter sw = new StreamWriter(fs1);
- sw.WriteLine(fileContent);//开始写入值
- sw.Close();
- fs1.Close();
- }
- else
- {
- FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write);
- StreamWriter sr = new StreamWriter(fs);
- sr.WriteLine(fileContent);//开始写入值
- sr.Close();
- fs.Close();
- }
- }
- byte[] FileByteBuffer = new byte[1024 * 1024];
- private void file_open(string path)
- {
- //打开系统日志文件
- if (!File.Exists(path))
- {
- // 目录/文件不存在,建立目录/文件
- File.CreateText(path);
- }
- //打开文件
- //FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
- //通过指定字符编码方式可以实现对汉字的支持,否则在用记事本打开查看会出现乱码
- StreamReader m_streamReader = new StreamReader(path, Encoding.GetEncoding("GB2312"));
- //使用StreamReader类来读取文件
- m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
- // 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
- // this.textBox1.Text = "";
- //string strEnd = m_streamReader.ReadToEnd();
- //Console.WriteLine(strEnd);
-
- string strLine = m_streamReader.ReadLine();
- int Cnt = 0;
-
- while (strLine != null)
- {
- FileByteBuffer[Cnt] = Convert.ToByte(strLine, 16);
- //Console.WriteLine("dat:" + strLine + " len:" + strLine.Length+" "+FileByteBuffer[Cnt].ToString("X"));
- Cnt++;
- strLine = m_streamReader.ReadLine();
- }
- Console.WriteLine(Cnt);
- //C# 创建二进制文件并写入
- BinaryWriter bw = new BinaryWriter(new FileStream("mydata.bin", FileMode.Create));
- bw.Write(FileByteBuffer,0, Cnt);//写入一个字符串
- bw.Close();
- //关闭此StreamReader对象
- MessageBox.Show("转换完毕!");
- //关闭此StreamReader对象
- m_streamReader.Close();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if(!(Firmware_str!=""&& Firmware_str.IndexOf(".dat")!=-1))
- {
- MessageBox.Show("未选择打开dat文件!");
- return;
- }
- string app_path_str = System.Windows.Forms.Application.StartupPath;
- string writeStr= bat_file_str.Replace("file.dat", Firmware_str);
- Console.WriteLine(writeStr);
- writeBATFile(app_path_str+ "/user_download.bat", writeStr);
- Process proc = null;
- try
- {
- string targetDir = string.Format(@app_path_str);//this is where mybatch.bat lies
- proc = new Process();
- proc.StartInfo.WorkingDirectory = targetDir;
- proc.StartInfo.FileName = "user_download.bat";
- proc.StartInfo.Arguments = string.Format("10");//this is argument
- proc.StartInfo.CreateNoWindow = false;
- proc.Start();
- proc.WaitForExit();
- }
- catch (Exception ex)
- {
- Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
- }
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (File_Text.Text != "")
- {
- file_open(File_Text.Text);
- }
- else {
- MessageBox.Show("未选择打开dat文件!");
- }
-
- }
- string read_mac =
- "e pu\n" +
- "e fr 18c6 10\n" +
- "timeout /T 3 /NOBREAK";
- private void button4_Click(object sender, EventArgs e)
- {
- string app_path_str = System.Windows.Forms.Application.StartupPath;
- string writeStr = bat_file_str.Replace("file.dat", Firmware_str);
- Console.WriteLine(writeStr);
- writeBATFile(read_mac + "/readmac.bat", writeStr);
- Process proc = null;
- try
- {
- string targetDir = string.Format(@app_path_str);//this is where mybatch.bat lies
- proc = new Process();
- proc.StartInfo.WorkingDirectory = targetDir;
- proc.StartInfo.FileName = "user_download.bat";
- proc.StartInfo.Arguments = string.Format("10");//this is argument
- proc.StartInfo.CreateNoWindow = false;
- proc.Start();
- proc.WaitForExit();
- }
- catch (Exception ex)
- {
- Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString());
- }
- }
- private void button5_Click(object sender, EventArgs e)
- {
- }
- }
- }
|