/// <summary> /// 根据文件内容,生成 Lrc 对象实例集合 /// <para>异常:</para> /// <para>FileNotFoundException,无法获取指定的歌词文件</para> /// </summary> private void BuildLrcCollection() { List <LrcItem> unOrderLrcList = new List <LrcItem>(); FileInfo f = new FileInfo(FilePath); if (f.Exists && f.Length > 10) { string ec = ENCODER.GetEncodingName(f); StreamReader sr = new StreamReader(f.FullName, ENCODER.GetEncoding(ec)); while (!sr.EndOfStream) { string lineString = sr.ReadLine(); unOrderLrcList.AddRange(_ParseLine(lineString)); } } else if (f.Exists && f.Length < 10) { throw new FileLoadException("文件长度太短"); } else { //歌词文件不存在 throw new FileNotFoundException("未找到歌词文件。"); } this.Items = new LinkedList <LrcItem>(unOrderLrcList.OrderBy(pp => pp.Time.Ticks)); }
/// <summary> /// 获取文件编码 /// </summary> /// <param name="path"></param> /// <returns></returns> public static Encoding GetEncoding(String path) { FileInfo fileInfo = new FileInfo(path); IdentifyEncoding identitfy = new IdentifyEncoding(); return(Encoding.GetEncoding(identitfy.GetEncodingName(fileInfo))); }
private void ConvertFileEncode(string filePath) { Encoding oriEncode; if (chkUnknownEncoding.Checked) //编码识别 { IdentifyEncoding ie = new IdentifyEncoding(); FileInfo fi = new FileInfo(filePath); string encodingName = ie.GetEncodingName(fi); if (encodingName == "UNKNOWN") { txtResult.Text += string.Format("\r\n{0}文件格式不正确或已损坏。 ", filePath); return; } else { oriEncode = Encoding.GetEncoding(encodingName); } } else { oriEncode = GetSelectEncoding(cmbSourceEncode.SelectedIndex); } string text = File.ReadAllText(filePath, oriEncode); if (chkIsBackup.Checked) //备份 { File.WriteAllText(filePath + ".bak", text, oriEncode); } File.WriteAllText(filePath, text, GetSelectEncoding(cmbTargetEncode.SelectedIndex)); if (filePath.LastIndexOf("[1]") != -1) { File.Move(filePath, filePath.Replace("[1]", string.Empty)); } }
private void ConvertCP(string sFromFilePath, Encoding encode) { if (sFromFilePath.Trim().Equals("")) { return; } Encoding oriEncode; #region 编码识别 IdentifyEncoding ie = new IdentifyEncoding(); FileInfo fi = new FileInfo(sFromFilePath); string encodingName = string.Empty; string message = string.Empty; encodingName = ie.GetEncodingName(fi); fi = null; if (encodingName.ToLower() == "other") { message = string.Format("\r\n{0}文件格式不正确或已损坏。 ", sFromFilePath); txtOutput.AppendText((sFromFilePath + ('\t' + (message)))); return; } else { oriEncode = Encoding.GetEncoding(encodingName); } #endregion string text = File.ReadAllText(sFromFilePath, oriEncode); File.WriteAllText(sFromFilePath, text, encode); txtOutput.AppendText(sFromFilePath); }
public static Encoding EncodingInfo(string path) { //对编码格式处理 FileInfo fileInfo = new FileInfo(path); IdentifyEncoding identitfy = new IdentifyEncoding(); Encoding encoding = Encoding.GetEncoding(identitfy.GetEncodingName(fileInfo)); return(encoding); }
/// <summary> /// 从指定位置开始读取数据,直到接收到停止消息。 /// </summary> public void Read(int offset) { //下面这句话保证,每次都从OneTimeRead的倍数位置读取数据 offset = offset / OneTimeRead * OneTimeRead; byte[] bytes = new byte[OneTimeRead]; TotalLen = _cacheBuffer.Read(offset, bytes); StartedOffset = offset; string encodingName = identifyEncoding.GetEncodingName(IdentifyEncoding.ToSByteArray(bytes)); string text = Encoding.GetEncoding(encodingName).GetString(bytes, 0, TotalLen); Text = text; }
public static void Test() { IdentifyEncoding sinodetector; string result = null; sinodetector = new IdentifyEncoding(); try { result = sinodetector.GetEncodingName(new System.Uri("http://china5.nikkeibp.co.jp/china/news/com/200307/pr_com200307170131.html")); } catch (System.Exception e) { Console.Error.WriteLine("Bad URL " + e.ToString()); } Console.Write(result); Console.Read(); }
public string GetString(string encodingName, byte[] bytes, int count) { if (string.IsNullOrWhiteSpace(encodingName)) { try { string autoCheckName = _identifyEncoding.GetEncodingName(IdentifyEncoding.ToSByteArray(bytes)); string text = Encoding.GetEncoding(autoCheckName).GetString(bytes, 0, count); return(text); } catch (Exception) { encodingName = "Hex"; } } if (encodingName == "Hex") { string text = BytesToHexString(bytes, count); return(text); } return(string.Empty); }
/// <summary> /// 文件写入到 Oracle Blob 字段中。type = 1 表示地图文件,其他值表示战斗文件 /// </summary> public static bool SaveFileToDB(int file_type, string DBKey, string FullFileName) { FileStream mapfs; try { mapfs = new FileStream(FullFileName, FileMode.Open, FileAccess.Read); } catch (Exception ex) { Logging.Write("SaveFileToDB error: " + ex.ToString()); return(false); } string sql; if (file_type == 1) { //sql = string.Format("select file_context from map_file where map_name = '{0}' for update", DBKey); sql = string.Format("update map_file set file_context = :1 where map_name = '{0}'", DBKey); } else { //sql = string.Format("select file_context from fight_file where roll_type = '{0}' for update", DBKey); sql = string.Format("update fight_file set file_context = :2 where roll_type = '{0}'", DBKey); } OracleTransaction transaction = null; try { if (!isConnected) { OraConnect(); } // 利用事务处理(必须) transaction = conn.BeginTransaction(); OracleCommand oCmd = new OracleCommand(sql, conn); OracleParameter param = oCmd.Parameters.Add("1", OracleDbType.Clob, ParameterDirection.Input); IdentifyEncoding sinodetector = new IdentifyEncoding(); FileInfo finfo = new FileInfo(FullFileName); StreamReader sr = new StreamReader(mapfs, sinodetector.GetEncoding(sinodetector.GetEncodingName(finfo))); string context = sr.ReadToEnd(); param.Value = context; oCmd.ExecuteNonQuery(); transaction.Commit(); sr.Close(); mapfs.Close(); } catch (Exception ex) { transaction.Rollback(); Logging.Write("SaveFileToDB error: " + ex.ToString()); return(false); } return(true); }