void auto_load_file() { if (ftp.Server == "" || !(File.Exists(ftp.Server)))//之前没有加载过文件或者加载文件但文件找不到了 { return; } StreamReader myStream; cmd_list.Clear(); myStream = new StreamReader(ftp.Server); //初始化myStream并将打开的文件指针传给 string strline; //取每行的字符串 while ((strline = myStream.ReadLine()) != null) //只要不是空的继续读 { try { Match match0 = Regex.Match(strline, @"(^[$].*)[:]([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?");//将所有需要的数据匹配出来存放到相应空间中去-以$开头 最多分20组 if (match0.Success) { if (match0.Groups[1].Value == Project_Name) { this.Text = match0.Groups[2].Value; } else if (match0.Groups[1].Value == Tx_ID) { //driverTrans.tx_id_load = int.Parse(match0.Groups[2].Value.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber); } else if (match0.Groups[1].Value == Rx_ID) { //driverTrans.rx_id_load = int.Parse(match0.Groups[2].Value.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber); } else if (match0.Groups[1].Value == Cmd_ID) { int stringlength = 0; for (int i = 0; i < 20; i++) { if (match0.Groups[i + 2].Value == "") { stringlength = i - 1; break; } } string[] cmd = new string[stringlength]; string cmd_name = ""; for (int i = 0; i < 20; i++) { Match match_cmd = Regex.Match(match0.Groups[i + 2].Value, @"^#"); if (match_cmd.Success) { cmd_name = match0.Groups[i + 2].Value; break; } else { cmd[i] = match0.Groups[i + 2].Value; } } cmd_shortkey = new cmd(cmd, cmd_name); cmd_list.Add(cmd_shortkey); } else if (match0.Groups[1].Value == ENDOFLINE) { endofline_config endofline = new endofline_config(); endofline_list.Add(endofline); int canid = int.Parse(match0.Groups[2].Value, System.Globalization.NumberStyles.HexNumber); string name = match0.Groups[3].Value; endofline.id = canid; endofline.name = name; while (true) { strline = myStream.ReadLine(); Match match2 = Regex.Match(strline, @"(^[@].*)[:]([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?"); if (match2.Success && match2.Groups[1].Value == "@Signal") { endofline_config.signal_config signal = new endofline_config.signal_config("", 0, 0); byte startbitpos = byte.Parse(DecimalStringToHexString(match2.Groups[2].Value), System.Globalization.NumberStyles.HexNumber); string sname = match2.Groups[3].Value; byte value = byte.Parse(match2.Groups[4].Value, System.Globalization.NumberStyles.HexNumber); signal.start_bitpos = startbitpos; signal.name = sname; signal.value = value; signal.signal_open = false; endofline.signal_list.Add(signal); } else { break; } } } } } catch { } } myStream.Close(); if (cmd_list.Count != 0) { shortkeycomboBox.Items.Clear(); foreach (cmd s in cmd_list) { shortkeycomboBox.Items.Add(s.cmd_name); } shortkeycomboBox.SelectedIndex = 0; } }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openFileToolStripMenuItem_Click(object sender, EventArgs e) { StreamReader myStream;// openFileDialog.Title = "打开文件"; openFileDialog.Filter = " (*.ini)|*.ini|(*.dbc)|*.dbc"; //ini格式 或者dbc文本 openFileDialog.FilterIndex = 0; //选择第一个 openFileDialog.RestoreDirectory = true; //存储路径 if (openFileDialog.ShowDialog() == DialogResult.OK) //选择打开该文件 { myStream = new StreamReader(openFileDialog.FileName); //初始化myStream并将打开的文件指针传给 if (System.IO.Path.GetExtension(openFileDialog.FileName) == ".ini") //加载的是ini文件 { cmd_list.Clear(); endofline_list.Clear(); ftp.Server = openFileDialog.FileName; ftp.Save(); string strline; //取每行的字符串 while ((strline = myStream.ReadLine()) != null) //只要不是空的继续读 { try { Match match0 = Regex.Match(strline, @"(^[$].*)[:]([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?");//将所有需要的数据匹配出来存放到相应空间中去-以$开头 最多分20组 if (match0.Success) { if (match0.Groups[1].Value == Project_Name) { this.Text = match0.Groups[2].Value; } else if (match0.Groups[1].Value == Tx_ID) { //driverTrans.tx_id_load = int.Parse(match0.Groups[2].Value.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber); } else if (match0.Groups[1].Value == Rx_ID) { //driverTrans.rx_id_load = int.Parse(match0.Groups[2].Value.Replace("0x", ""), System.Globalization.NumberStyles.HexNumber); } else if (match0.Groups[1].Value == Cmd_ID) { int stringlength = 0; for (int i = 0; i < 20; i++) { if (match0.Groups[i + 2].Value == "") { stringlength = i - 1; break; } } string[] cmd = new string[stringlength]; string cmd_name = ""; for (int i = 0; i < 20; i++) { Match match_cmd = Regex.Match(match0.Groups[i + 2].Value, @"^#"); if (match_cmd.Success) { cmd_name = match0.Groups[i + 2].Value; break; } else { cmd[i] = match0.Groups[i + 2].Value; } } cmd_shortkey = new cmd(cmd, cmd_name); cmd_list.Add(cmd_shortkey); } else if (match0.Groups[1].Value == ENDOFLINE) { endofline_config endofline = new endofline_config(); endofline_list.Add(endofline); int canid = int.Parse(match0.Groups[2].Value, System.Globalization.NumberStyles.HexNumber); string name = match0.Groups[3].Value; endofline.id = canid; endofline.name = name; while (true) { strline = myStream.ReadLine(); Match match2 = Regex.Match(strline, @"(^[@].*)[:]([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?([^,]*)[,]?"); if (match2.Success && match2.Groups[1].Value == "@Signal") { endofline_config.signal_config signal = new endofline_config.signal_config("", 0, 0); byte startbitpos = byte.Parse(DecimalStringToHexString(match2.Groups[2].Value), System.Globalization.NumberStyles.HexNumber); string sname = match2.Groups[3].Value; byte value = byte.Parse(match2.Groups[4].Value, System.Globalization.NumberStyles.HexNumber); signal.start_bitpos = startbitpos; signal.name = sname; signal.value = value; signal.signal_open = false; endofline.signal_list.Add(signal); } else { break; } } } } } catch { } } } myStream.Close(); //重新更新service pid if (cmd_list.Count != 0) { shortkeycomboBox.Items.Clear(); foreach (cmd s in cmd_list) { shortkeycomboBox.Items.Add(s.cmd_name); } shortkeycomboBox.SelectedIndex = 0; } } }