/// <summary> /// 得到文件的所有行(包括空行、#开头的) /// </summary> /// <param name="filePath"></param> /// <returns></returns> private List <InfoItem> GetFileAllLine(string filePath) { List <InfoItem> cList = null; var lList = File.ReadAllLines(filePath, Encoding.ASCII); int index = 0; InfoItem ii = null; if (lList.Count() > 0) { cList = new List <InfoItem>(); } foreach (var item in lList) { if (item == "" || item.StartsWith("#")) { cList.Add(new InfoItem { Key = "", Value = item }); } else { string[] itemArr = item.Split('='); if (itemArr.Length > 1) { var val = GetSecondToEndInfo(itemArr); ii = new InfoItem { Key = itemArr[0], Value = val }; cList.Add(ii); } if (!item.Contains("=") && index > 0) { int count = cList.Count; cList[count - 1].Value += "\r\n" + item; } } index++; } return(cList); }
private List <InfoItem> GetFileInfo(string filePath) { List <InfoItem> cList = null; //得到WebStorm语言资源文件内容,#开头(注册),空行过滤 var lList = File.ReadAllLines(filePath, Encoding.ASCII) .Where(l => !l.StartsWith("#")) .Where(l => l.Trim() != ""); int index = 0; InfoItem ii = null; if (lList.Count() > 0) { cList = new List <InfoItem>(); } foreach (var item in lList) { string[] itemArr = item.Split('='); if (itemArr.Length > 1) { var val = GetSecondToEndInfo(itemArr); ii = new InfoItem { Key = itemArr[0], Value = val }; cList.Add(ii); } if (!item.Contains("=") && index > 0) { int count = cList.Count; cList[count - 1].Value += "\r\n" + item; } index++; } return(cList); }
private void ReplaceInfoByReferenceInfo(string sourceFile, string refPath, string distPath) { try { if (File.Exists(sourceFile)) { string fileName = Path.GetFileName(sourceFile); string refereceFile = Path.Combine(refPath, fileName); if (!Directory.Exists(distPath)) { Directory.CreateDirectory(distPath); } string distFile = Path.Combine(distPath, fileName); if (File.Exists(refereceFile)) { //string noOperateFileName = ""; var refContent = this.GetFileInfo(refereceFile); //File.ReadAllLines(refereceFile, Encoding.ASCII).Where(l => !l.StartsWith("#")).Where(l => l.Trim() != "").Where(l => l.IndexOf("=") != -1).ToList().Select(l => new InfoItem { Key = l.Split('=')[0], Value = GetSecondToEndInfo(l.Split('=')) }).ToList(); var sContent = this.GetFileAllLine(sourceFile); //File.ReadAllLines(sourceFile, Encoding.ASCII); foreach (var item in sContent) { if (item.Key != "") { InfoItem findIt = refContent.Find(r => r.Key == item.Key); item.Value = findIt != null ? findIt.Value : item.Value; } } File.WriteAllLines(distFile, sContent.Select(c => c.Key + (c.Key != "" ? "=" : "") + c.Value)); } } } catch { throw; } }