public static List <string> ReadFrom(string file, bool saveread) { string line; try { using (var reader = File.OpenText(file)) { List <string> result = new List <string>(); while ((line = reader.ReadLine()) != null) { if (line.Contains("\\n")) { result.Add(Function.ToDataString(line)); } else { result.Add(line); } } return(result); } } catch { if (saveread) { WriteFiles.Write(new string[0], file); return(new List <string>()); } else { throw new Exception("无法找到文件"); } } }
public static string DeleteLines(string path, int startIndex, int endIndex) { if (File.Exists(path)) { if (startIndex <= endIndex) { string[] tem_read = ReadFiles.ReadOnly(path, false); //缓存原内容 if (endIndex < tem_read.Length) { string[] output_str = new string[tem_read.Length - (endIndex - startIndex + 1)]; //声明输出变量 int j = 0; //为输出变量添加索引 for (int i = 0; i < startIndex; i++) { output_str[j] = tem_read[i]; j++; } for (int i = endIndex + 1; i < tem_read.Length; i++) { output_str[j] = tem_read[i]; j++; } WriteFiles.Write(output_str, path); return("OK"); } else { return("[Error]Index超出范围"); } } throw new ArgumentException("[Error]StartIndex大于EndIndex"); } else { return("[Error]找不到文件"); } }