public void Execute() { if (string.IsNullOrWhiteSpace(FilePath)) { throw new InvalidOperationException("文件路径尚未初始化。"); } if (Regexes == null) { throw new InvalidOperationException("正则表达式尚未初始化。"); } var encoding = TextFileEncodingDetector.DetectTextFileEncoding(FilePath, Encoding.Default); string content = File.ReadAllText(FilePath, encoding); string contains = string.Empty; foreach (var key in Regexes.Keys) { if (Regex.IsMatch(content, key)) { contains += Regexes[key] + "; "; } } if (contains != string.Empty && outputInfo != null) { outputInfo(Environment.NewLine + FilePath + "包含:" + contains + Environment.NewLine); } }
public void LogonTest() { IList <Cookie> cookies = Authentication.AuthenticationManager.Current.CookieCache; Assert.IsTrue(cookies != null); Console.WriteLine("Count:{0}", cookies.Count); foreach (Cookie cookie in cookies) { Console.WriteLine("Index:{0}", cookies.IndexOf(cookie)); Console.WriteLine("\tDomain:{0}", cookie.Domain); Console.WriteLine("\tName:{0}", cookie.Name); string value = cookie.Value; if (cookie.Name.ToLower() == "cadata") { byte[] content = Convert.FromBase64String(value.Substring(1, value.Length - 2)); Encoding encoding = TextFileEncodingDetector.DetectTextByteArrayEncoding(content); //Encoding.GetEncoding("gzip"); if (encoding != null) { value = encoding.GetString(content); Console.WriteLine("\tBase64 Value:{0}", cookie.Value); } } Console.WriteLine("\tValue:{0}", value); Console.WriteLine("\tPort:{0}", string.IsNullOrEmpty(cookie.Port)?"N/A":cookie.Port); Console.WriteLine("\tPath:{0}", string.IsNullOrEmpty(cookie.Port) ? "N/A" : cookie.Path); Console.WriteLine("\tHttpOnly:{0}", cookie.HttpOnly); Console.WriteLine("\tComment:{0}", string.IsNullOrEmpty(cookie.Port) ? "N/A" : cookie.Comment); } Assert.IsTrue(cookies.Count != 0); }
public void TextFileEncodingDetector22() { string testFileDir = Path.Combine(new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.Parent.FullName, "EncodingTestFiles/"); Encoding encodingUtf8WithBOM = TextFileEncodingDetector.DetectTextFileEncoding(testFileDir + "2. UTF8 with BOM.txt"); Encoding encodingUtf8WithoutBOM = TextFileEncodingDetector.DetectTextFileEncoding(testFileDir + "3. UTF8 without BOM.txt"); Encoding encodingANSI = TextFileEncodingDetector.DetectTextFileEncoding(testFileDir + "UserInfo.cs.txt"); Assert.IsTrue(encodingUtf8WithBOM is UTF8Encoding && (encodingUtf8WithBOM as UTF8Encoding).GetPreamble().Length > 0, "检测uft8 with bom"); Assert.IsTrue(encodingUtf8WithoutBOM is UTF8Encoding && (encodingUtf8WithoutBOM as UTF8Encoding).GetPreamble().Length == 0, "检测uft8 without bom"); Assert.IsTrue(encodingANSI == Encoding.Default, "检测ansi"); }
public void Execute() { if (string.IsNullOrWhiteSpace(filePath) || !File.Exists(filePath)) { throw new InvalidOperationException("filePath is empty or not exists."); } encoding = TextFileEncodingDetector.DetectTextFileEncoding(filePath, Encoding.Default); string content = File.ReadAllText(filePath, encoding); string replacedContent = new Regex(this.regexString).Replace(content, this.replacer); File.WriteAllText(filePath, replacedContent, encoding); }
public static CsvReader <List <Object> > CreateLineReader(string filePath, Encoding encoding = null) { if (encoding == null) { encoding = TextFileEncodingDetector.DetectTextFileEncoding(filePath); } var streamReader = new StreamReader(filePath, encoding); var csvReader = new CsvReader <List <Object> >(streamReader, false, filePath); return(csvReader); }
private void Open() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*"; if (ofd.ShowDialog() == DialogResult.OK && !string.IsNullOrWhiteSpace(ofd.FileName)) { Encoding encoding = TextFileEncodingDetector.DetectTextFileEncoding(ofd.FileName); this.enhancedTextBox1.Text = File.ReadAllText(ofd.FileName, encoding); this.Text = ofd.FileName; fileName = ofd.FileName; } }
public void InitBytes(byte[] bytes, EncodingPair defaultEncoding, out string error) { error = null; string text = ""; encodingPair = defaultEncoding; if (bytes != null) { try { if (!settedEncodingPair.IsNull) { encodingPair = settedEncodingPair; } else { bool bom; Encoding encoding = TextFileEncodingDetector.DetectTextByteArrayEncoding(bytes, out bom); if (encoding != null) { encodingPair = new EncodingPair(encoding, bom); } } int bomLength = encodingPair.CorrectBomLength(bytes); if (encodingPair.bom && encodingPair.encoding == Encoding.UTF8 && bomLength == 0) { encodingPair = new EncodingPair(Encoding.UTF8, false); settedEncodingPair = encodingPair; if (error == null) { error = "Missing bom, loaded as without it"; } } text = encodingPair.GetString(bytes, bomLength); } catch (Exception e) { error = e.Message; } } if (encodingPair.IsNull) { encodingPair = new EncodingPair(Encoding.UTF8, false); } Controller.InitText(text); }
public static string TransformAndCopy(string sourceFile, string outputDir, string projectFile) { string outputFile = outputDir + Path.DirectorySeparatorChar + NewFile; try { var encoding = TextFileEncodingDetector.DetectTextFileEncoding(sourceFile); File.WriteAllText( outputFile, AddCSHTMLCode(File.ReadAllText(sourceFile, encoding), projectFile) , encoding); } catch (Exception ex) { throw new InvalidOperationException($"Unable to update destination file {outputFile}", ex); } return(outputFile); }
void Init(string file) { encoding = TextFileEncodingDetector.DetectTextFileEncoding(file, Encoding.Default); using (FileStream stream = File.OpenRead(file)) { byte[] data = new byte[256]; stream.Read(data, 0, data.Length); string datastr = encoding.GetString(data); string[] strs = datastr.Split('\n'); string title = strs[0]; string line1 = strs[1]; string line2 = strs[2]; start = encoding.GetBytes(title + '\n').Length; size = encoding.GetBytes(line1 + '\n').Length; var data1 = new StockData(line1); var data2 = new StockData(line2); step = (int)(data2.Time - data1.Time); startTime = data1.Time; } }
public void Execute() { if (string.IsNullOrWhiteSpace(FilePath)) { throw new InvalidOperationException("文件路径尚未初始化。"); } if (RegexAndReplacer == null) { throw new InvalidOperationException("正则表达式尚未初始化。"); } Encoding encoding = TextFileEncodingDetector.DetectTextFileEncoding(this.FilePath); String content = File.ReadAllText(this.FilePath, encoding); foreach (var strRegex in RegexAndReplacer.Keys) { Regex regex = new Regex(strRegex); content = regex.Replace(content, RegexAndReplacer[strRegex]); } File.WriteAllText(FilePath, content, encoding); }
public Dictionary <string, ROMChar> CharMap = new Dictionary <string, ROMChar>(); //indexed by character public static ROMTable LoadTable(string file) { ROMTable rt = new ROMTable(); int errCount = 0; Encoding enc = TextFileEncodingDetector.DetectTextFileEncoding(file); if (enc == null) { enc = ASCIIEncoding.ASCII; } using (StreamReader sr = new StreamReader(file, enc)) { while (!sr.EndOfStream) { string str = sr.ReadLine(); try { if (str.Split('=')[0].Contains("**")) {//if other similar mappings are well defined, they should be before this definition for (int d = 0; d < 256; d++) { if (str.Split('=')[0].Contains("%%")) { for (int e = 0; e < 256; e++) { ROMTable.ROMChar chr = new ROMTable.ROMChar(); chr.AsciiUnicode = str.Split('=')[1].Replace("**", d.ToString("X2")).Replace("%%", e.ToString("X2")); chr.HexValue = str.Split('=')[0].Replace("**", d.ToString("X2")).Replace("%%", e.ToString("X2")); chr.DecValue = new HexWord().StringToDec(chr.HexValue); chr.ByteLength = (chr.HexValue.Length / 2);//get the number of bytes... if (!rt.HexMap.ContainsKey(chr.HexValue)) { rt.HexMap.Add(chr.HexValue, chr); } string chrprep = chr.AsciiUnicode.Replace("\\", ""); if (!rt.CharMap.ContainsKey(chrprep)) { rt.CharMap.Add(chrprep, chr); } } } else { ROMTable.ROMChar chr = new ROMTable.ROMChar(); chr.AsciiUnicode = str.Split('=')[1].Replace("**", d.ToString("X2")); chr.HexValue = str.Split('=')[0].Replace("**", d.ToString("X2")); chr.DecValue = new HexWord().StringToDec(chr.HexValue); chr.ByteLength = (chr.HexValue.Length / 2);//get the number of bytes... if (!rt.HexMap.ContainsKey(chr.HexValue)) { rt.HexMap.Add(chr.HexValue, chr); } string chrprep = chr.AsciiUnicode.Replace("\\", ""); if (!rt.CharMap.ContainsKey(chrprep)) { rt.CharMap.Add(chrprep, chr); } } } } else { ROMTable.ROMChar chr = new ROMTable.ROMChar(); chr.AsciiUnicode = str.Split('=')[1]; chr.HexValue = str.Split('=')[0]; chr.DecValue = new HexWord().StringToDec(chr.HexValue); chr.ByteLength = (chr.HexValue.Length / 2);//get the number of bytes... if (!rt.HexMap.ContainsKey(chr.HexValue)) { rt.HexMap.Add(chr.HexValue, chr); } string chrprep = chr.AsciiUnicode.Replace("\\", ""); if (!rt.CharMap.ContainsKey(chrprep)) { rt.CharMap.Add(chrprep, chr); } } } catch { errCount++; } } } return(rt); }
private void btnChooseFile_Click(object sender, EventArgs e) { OpenFileDialog oDialog = new OpenFileDialog(); if (oDialog.ShowDialog() == DialogResult.OK && !string.IsNullOrWhiteSpace(oDialog.FileName)) { Task task = new Task(() => { string file = oDialog.FileName; Encoding encoding = TextFileEncodingDetector.DetectTextFileEncoding(file); string[] lines = File.ReadAllLines(file, encoding); List <string> lstResult = new List <string>(); foreach (string line in lines) { string[] arrLine = line.Split(new string[] { "): " }, StringSplitOptions.RemoveEmptyEntries); if (arrLine.Length < 2) { continue; } //不处理部分文件类型 string fileName = arrLine[0].Substring(0, arrLine[0].LastIndexOf("(")); string fileExtension = new FileInfo(fileName).Extension.ToLower(); if (new List <string>() { ".js", ".css", ".xsd", ".xml" }.Contains(fileExtension)) { continue; } //忽略的关键字 string trimedLine = arrLine[1].Trim().ToLower(); trimedLine = trimedLine .Replace("something", "") ; //排除注释 trimedLine = Regex.Replace(trimedLine, @"^([^/]+)//[^/]+$", ""); if (trimedLine.StartsWith("//") || trimedLine.StartsWith("border-") || trimedLine.EndsWith("aaa".ToLower()) || trimedLine.EndsWith("bbb".ToLower()) || trimedLine.EndsWith("ccc".ToLower()) || trimedLine.Contains("ddd".ToLower()) || !trimedLine.Contains("eee") || !trimedLine.Contains("fff")) { continue; } if (trimedLine.Contains("aa") && trimedLine.Contains("bb")) { lstResult.Add(line); } } if (this.InvokeRequired) { this.Invoke(new Action(() => { SaveTextForm form = new SaveTextForm(lstResult.ToArray()); form.ShowDialog(); this.progressBar1.Visible = false; })); } else { SaveTextForm form = new SaveTextForm(lstResult.ToArray()); form.ShowDialog(); this.progressBar1.Visible = false; } }); task.Start(); this.progressBar1.Visible = true; } }