/// <summary> /// 逐行读取输入流 /// </summary> /// <param name="s">输入流</param> /// <param name="cb">每读取一行的回调处理对象</param> /// <param name="encoding">字符编码</param> /// <param name="bz">读取缓冲区大小</param> /// <returns>返回读取的行数</returns> public static int ReadStreamByLine(Stream s, OnReadLine cb, Encoding encoding = null, int bz = 0) { StreamReader sr; if (encoding != null) { sr = bz > 0 ? new StreamReader(s, encoding, true, bz) : new StreamReader(s, encoding); } else { sr = new StreamReader(s); } int lc = 0; do { string line = sr.ReadLine(); if (line == null) { break; } lc++; if (!cb(line)) { break; } }while (!sr.EndOfStream); return(lc); }
public void InputTextBox_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.Key.Equals(Key.Return) || e.Key.Equals(Key.Enter)) { OnReadLine?.Invoke(input_TextBox.Text); terminalData.Add(input_TextBox.Text); input_TextBox.Text = ""; } }
/// <summary> /// 逐行读取文件 /// </summary> /// <param name="fileName">文件名</param> /// <param name="cb">每读取一行的回调处理对象</param> /// <param name="encoding">字符编码</param> /// <param name="bz">读取缓冲区大小</param> /// <returns>返回读取的行数</returns> public static int ReadFileByLine(string fileName, OnReadLine cb, Encoding encoding = null, int bz = 0) { int lc; FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read); try { lc = ReadStreamByLine(fs, cb, encoding, bz); } finally { fs.Close(); } return(lc); }
private void KeyEvent(object sender, KeyEventArgs e) { if (historyNext && e.KeyCode != Keys.Enter) { historyNext = false; } if (hideNext) { string chr = (new KeysConverter().ConvertToString(e.KeyCode)); hidden.SelectionStart = input.SelectionStart - prefix.Length; hidden.SelectionLength = input.SelectionLength; if (chr == "Back") { HiddenBackspace(); } else if (chr.Length == 1) { hidden.Text += chr; input.Text += "X"; e.SuppressKeyPress = true; input.Select(input.Text.Length, 0); } else { e.SuppressKeyPress = true; input.Select(input.Text.Length, 0); } } if (e.KeyCode == Keys.Enter) { string command = input.Text.Substring(prefix.Length); if (!hideNext && !choiceMode && !ignoreNext && !command.StartsWith("set pin ")) { history.Add(command); if (!historyNext) { hIndex = history.Count; } else { historyNext = false; } prevCommand = command; } if (hideNext) { tempText = hidden.Text; hidden = new TextBox(); hideNext = false; } else { tempText = command; } ReadLineEventArgs args = new ReadLineEventArgs(command); if (!choiceMode && !ignoreNext) { OnReadLine?.Invoke(this, args); } if (ignoreNext) { ignoreNext = false; } input.Clear(); input.Text = String.Empty; input.Text = prefix; input.Select(input.Text.Length, 0); input.Text = input.Text.Replace("\r\n", String.Empty); input.Text = input.Text.Trim(); e.SuppressKeyPress = true; } if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Left) { if (input.SelectionStart <= prefix.Length) { e.SuppressKeyPress = true; } } if (e.KeyCode == Keys.Home && !hideNext) { input.Select(prefix.Length, 0); e.SuppressKeyPress = true; } if (e.KeyCode == Keys.Up && !hideNext) { if (hIndex > 0) { hIndex -= 1; } if (hIndex != -1) { LoadHistory(); } } if (e.KeyCode == Keys.Down && !hideNext) { if ((hIndex + 1) == history.Count) { hIndex += 1; } if (hIndex >= history.Count) { input.Text = prefix; input.Select(input.Text.Length, 0); return; } hIndex += 1; LoadHistory(); } }