判断文件夹/文件是否存在
[code] public static void fileOperation() {string path = AppDomain.CurrentDomain.BaseDirectory;Console.WriteLine(\"================path:\" + path);//判断文件所在的文件夹是否存在,如果不存在就创建文件夹if (Directory.Exists(path+\"backUp\")==false){//创建文件夹Directory.CreateDirectory(path + \"backUp\");/* Directory.SetCreationTime(path + \"backUp\", DateTime.Now );//设置文件夹创建时间Directory.SetLastWriteTime(path + \"backUp\", DateTime.Now);//设置文件夹最近被修改时间Directory.SetLastAccessTime(path + \"backUp\", DateTime.Now);//设置文件夹最近被访问时间*/FileAttributes MyAttributes = File.GetAttributes(path + \"backUp\");File.SetAttributes(path + \"backUp\",MyAttributes|FileAttributes.Hidden); //设置文件夹隐藏/* File.SetAttributes(path + \"backUp\", FileAttributes.Normal);//设置文件夹属性为正常File.SetAttributes(path + \"backUp\", FileAttributes.ReadOnly);//设置成只读文件夹File.SetAttributes(path + \"backUp\", MyAttributes | FileAttributes.System);//设置添加系统文件夹File.SetAttributes(path + \"backUp\", MyAttributes | FileAttributes.Archive);//设置添加归档文件夹*/}else{ //如果存在,判断文件是否存在if (File.Exists(path + \"backUp/backUp.txt\") == false){File.Create(path + \"backUp/backUp.txt\").Close();}StreamWriter stream = new StreamWriter(path + \"backUp/backUp.txt\", true);BackUpRecord backUpRecord = new BackUpRecord();backUpRecord.id = System.Guid.NewGuid().ToString(\"N\");backUpRecord.name = \"张三\";backUpRecord.date = \"2020-12-12 12:12:12\";backUpRecord.size = \"3.3G\";backUpRecord.type = \"手动\";backUpRecord.ip = \"192.168.0.113\";backUpRecord.dName = \"test1\";backUpRecord.address = \"d/rrr\";JavaScriptSerializer json = new JavaScriptSerializer();String jsonString = json.Serialize(backUpRecord);stream.WriteLine(jsonString+\"&\");stream.Dispose();//流关闭StreamReader reader = new StreamReader(path + \"backUp/backUp.txt\");string data = reader.ReadToEnd();reader.Dispose();Console.WriteLine(\"data:\"+data);}}
2,读,写,删除,数据的工具类
[code]using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Web.Script.Serialization;using Model;namespace Util{public class FileUntils //封装操作文件的公共类{public static String path = AppDomain.CurrentDomain.BaseDirectory;//获取项目根目录public static JavaScriptSerializer serializer = new JavaScriptSerializer();//判断根目录下文件是否存在public static Boolean fileIsExistInRoot(string fileName) {Boolean flag=File.Exists(path + fileName);return flag;}//判断根目录下文件夹是否存在public static Boolean directoryIsExistInRoot(string directoryName){String path = AppDomain.CurrentDomain.BaseDirectory;//获取项目根目录Boolean flag = File.Exists(path + directoryName);return flag;}//往根目录文件中写入数据public static Boolean writeData<T>(T backUpRecord, string fileName){if (fileIsExistInRoot(fileName)){string jsonstring=serializer.Serialize(backUpRecord);StreamWriter streamWriter = new StreamWriter(path + fileName, true);streamWriter.WriteLine(jsonstring + \"&\");streamWriter.Dispose();return true;}else {return false;}}//从根目录文件中读取数据,返回对象集合public static List<T> readerData<T>(T record, string fileName){List<T> ts = new List<T>();if (fileIsExistInRoot(fileName)){StreamReader streamReader = new StreamReader(path + fileName, true);string data=streamReader.ReadToEnd();streamReader.Dispose();string[] vs= data.Split(\'&\');if (vs.Length>0) {Array.Reverse(vs);//倒序foreach (string dt in vs) {T recode1 = serializer.Deserialize<T>(dt);if (recode1 != null){ts.Add(recode1);}}}return ts;}else{return ts;}}//从根目录文件中读取数据,返回字符串数组public static string[] readerData( string fileName){string[] arr;if (fileIsExistInRoot(fileName)){StreamReader streamReader = new StreamReader(path + fileName, true);string data = streamReader.ReadToEnd();streamReader.Dispose();string[] vs = data.Split(\'&\');if (vs.Length > 0){Array.Reverse(vs);//倒序return vs;}else {return arr = new string[0];}}else{return arr=new string[0];}}//从根目录文件中删除数据public static bool deleteData(string id, string fileName){string[] arr = readerData(fileName);if (arr.Length > 0){Array.Reverse(arr);List<String> list=arr.ToList();for (int a = 0; a < list.Count(); a++) {bool flag = list[a].Contains(id);if (flag) {list.RemoveAt(a);}}if (list.Count>0) {arr= list.ToArray();StreamWriter streamWriter = new StreamWriter(path + fileName, false);streamWriter.WriteLine(\"\");streamWriter.Dispose();for (int a=0;a<arr.Length;a++) {if (!string.IsNullOrWhiteSpace(arr[a])) {StreamWriter streamWriter1 = new StreamWriter(path + fileName, true);streamWriter1.WriteLine(arr[a] + \"&\");streamWriter1.Dispose();}}}clearNullLine(path,fileName);return true;}else {return false;}}//清除文件里的空行public static void clearNullLine(string path,string fileName) {string filename1 = \"backUp/backUp1.txt\";bool flag= File.Exists(path + filename1);if (!flag){File.Create(path+filename1).Close();}try {using (var streamReader = new StreamReader(path + fileName))using (var streamWriter = new StreamWriter(path + filename1)){string line;while ((line = streamReader.ReadLine()) != null){if (!string.IsNullOrWhiteSpace(line)){streamWriter.WriteLine(line);}}//关闭流streamReader.Dispose();streamWriter.Dispose();File.Copy(filename1, fileName, true);}} finally {File.Delete(filename1);};}}}