//private const char END = '\0'; //private StreamReader _reader = null; public FileSourceReader(string fileName) { Encoding encoding = TxtFileEncoding.GetEncoding(fileName); _sourceText = File.ReadAllText(fileName, encoding);//Encoding.Default /*, Encoding.UTF8*/); pointer = 0; }
public void ReadFile(string fileName) { //_reader = File.OpenText(fileName); Encoding encoding = TxtFileEncoding.GetEncoding(fileName); _sourceText = File.ReadAllText(fileName, encoding);//Encoding.Default /*, Encoding.UTF8*/); pointer = 0; }
/// <summary> /// 读取文件内容 /// </summary> /// <param name="fileName"></param> /// <returns></returns> public static string ReadText(string fileName) { try { if (!File.Exists(fileName)) { return(string.Empty); } Encoding fileEncoding = TxtFileEncoding.GetEncoding(fileName, Encoding.UTF8); StreamReader sr = new StreamReader(fileName, fileEncoding); string str = sr.ReadToEnd(); sr.Close(); sr.Dispose(); return(str); } catch { return(string.Empty); } }
/// <summary> /// 获取文本格式 /// </summary> /// <param name="path"></param> /// <returns></returns> public static Encoding GetEncoding(string path) { //模型文本格式 return(TxtFileEncoding.GetEncoding(path)); }
private void btnReadMultTxt_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog { RestoreDirectory = true, Filter = @"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*", Multiselect = true }; if (ofd.ShowDialog() != DialogResult.OK) { return; } _errorMsg = @"失败文件名:"; pbCount.Maximum = ofd.FileNames.Length; pbCount.Value = 0; lblTotal.Text = ofd.FileNames.Length.ToString(CultureInfo.InvariantCulture); foreach (var fileName in ofd.FileNames) { var encoder = TxtFileEncoding.GetEncoding(fileName, Encoding.GetEncoding("GB2312")); var sr = new StreamReader(fileName, encoder); string duqu; while ((duqu = sr.ReadLine()) != null) { try { var str = duqu.Split('|'); var borehole = Borehole.FindOneByBoreholeNum(str[0]) ?? new Borehole { BindingId = IDGenerator.NewBindingID() }; borehole.BoreholeNumber = str[0]; borehole.GroundElevation = Convert.ToDouble(str[3]); borehole.CoordinateX = Convert.ToDouble(str[1].Split(',')[0]); borehole.CoordinateY = Convert.ToDouble(str[1].Split(',')[1]); borehole.CoordinateZ = 0; borehole.CoalSeamsTexture = String.Empty; // 创建钻孔岩性实体 var boreholeLithology = new BoreholeLithology { Borehole = borehole, Lithology = Lithology.FindOneByCoal(), FloorElevation = Convert.ToDouble(str[4]), CoalSeamsName = CoalSeams.FindAll().First().CoalSeamsName, Thickness = Convert.ToDouble(str[2]), CoordinateX = Convert.ToDouble(str[1].Split(',')[0]), CoordinateY = Convert.ToDouble(str[1].Split(',')[1]), CoordinateZ = 0 }; borehole.BoreholeLithologys = new[] { boreholeLithology }; DrawZuanKong(borehole, boreholeLithology); borehole.Save(); } catch (Exception) { lblError.Text = (Convert.ToInt32(lblError.Text) + 1).ToString(CultureInfo.InvariantCulture); lblSuccessed.Text = (Convert.ToInt32(lblSuccessed.Text) - 1).ToString(CultureInfo.InvariantCulture); _errorMsg += fileName.Substring(fileName.LastIndexOf(@"\", StringComparison.Ordinal) + 1) + "\n"; btnDetails.Enabled = true; } } lblSuccessed.Text = (Convert.ToInt32(lblSuccessed.Text) + 1).ToString(CultureInfo.InvariantCulture); pbCount.Value++; } Alert.alert("导入成功!"); }
private void btnReadMultTxt_Click(object sender, EventArgs e) { var ofd = new OpenFileDialog { RestoreDirectory = true, Filter = @"文本文件(*.txt)|*.txt|所有文件(*.*)|*.*", Multiselect = true }; if (ofd.ShowDialog() != DialogResult.OK) { return; } _errorMsg = @"失败文件名:"; pbCount.Maximum = ofd.FileNames.Length; pbCount.Value = 0; lblTotal.Text = ofd.FileNames.Length.ToString(CultureInfo.InvariantCulture); foreach (var fileName in ofd.FileNames) { var encoder = TxtFileEncoding.GetEncoding(fileName, Encoding.GetEncoding("GB2312")); var sr = new StreamReader(fileName, encoder); string duqu; while ((duqu = sr.ReadLine()) != null) { try { var str = duqu.Split('|'); var borehole = Borehole.FindAllByProperty("name", str[0]).FirstOrDefault() ?? new Borehole { bid = IdGenerator.NewBindingId() }; borehole.name = str[0]; borehole.ground_elevation = Convert.ToDouble(str[3]); borehole.coordinate_x = Convert.ToDouble(str[1].Split(',')[0]); borehole.coordinate_y = Convert.ToDouble(str[1].Split(',')[1]); borehole.coordinate_z = 0; borehole.coal_seam_texture = String.Empty; // 创建钻孔岩性实体 var boreholeLithology = new SubBorehole { borehole = borehole, lithology = "煤层", floor_elevation = Convert.ToDouble(str[4]), coal_seam = ConfigHelper.current_seam.name, thickness = Convert.ToDouble(str[2]), coordinate_x = Convert.ToDouble(str[1].Split(',')[0]), coordinate_y = Convert.ToDouble(str[1].Split(',')[1]), coordinate_z = 0 }; borehole.sub_boreholes = new[] { boreholeLithology }; DrawZuanKong(borehole, boreholeLithology); borehole.Save(); } catch (Exception) { lblError.Text = (Convert.ToInt32(lblError.Text) + 1).ToString(CultureInfo.InvariantCulture); lblSuccessed.Text = (Convert.ToInt32(lblSuccessed.Text) - 1).ToString(CultureInfo.InvariantCulture); _errorMsg += fileName.Substring(fileName.LastIndexOf(@"\", StringComparison.Ordinal) + 1) + "\n"; btnDetails.Enabled = true; } } lblSuccessed.Text = (Convert.ToInt32(lblSuccessed.Text) + 1).ToString(CultureInfo.InvariantCulture); pbCount.Value++; } Alert.AlertMsg("导入成功!"); }