工作常用的小知识,递归,写日志,序列化,异常处理等

文件操作

文件和文件夹的操作,都来自命名空间System.IO

Directory是一个帮助类库,静态类。用于检查文件是否存在,创建和删除文件夹

DirectoryInfo是文件夹对象

写日志函数public static void Log(string strmsg)

  1 using System;
  2 using System.Collections.Generic;
  3 using System.IO;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Threading.Tasks;
  7 
  8 namespace _009__数据库
  9 {
 10     class OperatorDictionary
 11     {
 12         public static string LogPath = AppDomain.CurrentDomain.BaseDirectory + "Log";
 13         public static void OperatorDir()
 14         {
 15             string LogPath = AppDomain.CurrentDomain.BaseDirectory + "LogPath";
 16             string strPatch = AppDomain.CurrentDomain.BaseDirectory + "\\Log";
 17             string strPatchLogMove = AppDomain.CurrentDomain.BaseDirectory + "LogMove";
 18             //Directory是一个帮助类库,静态类。用于检查文件是否存在,创建和删除文件夹
 19             //DirectoryInfo是文件夹对象
 20             if (!Directory.Exists(strPatch))
 21             {
 22                 DirectoryInfo dir = Directory.CreateDirectory(strPatch);
 23                 Directory.Move(strPatch, strPatchLogMove);//移动
 24                 Directory.Delete(strPatchLogMove);//删除
 25             }
 26             //文件操作
 27             string fileName = Path.Combine(LogPath, "log.txt");
 28             string fileNameCopy = Path.Combine(LogPath, "logCopy.txt");
 29             string fileNameMove = Path.Combine(LogPath, "logMove.txt");
 30             if (!File.Exists(fileName))
 31             {
 32                 Directory.CreateDirectory(LogPath);//打开文件流 (创建文件并写入)注意:重新覆盖的
 33                 //创建文件,
 34                 using (FileStream fs = File.Create(fileName))
 35                 {
 36                     StreamWriter sw = new StreamWriter(fs);
 37                     sw.WriteLine("这函数覆盖");
 38                     sw.Flush();
 39                 }
 40                 using (StreamWriter sw = File.AppendText(fileName))//流写入器(创建/打开文件并写入)
 41                 {
 42                     string msg = "这个函数是追加到文本中,不覆盖";
 43                     sw.WriteLine(msg);
 44                     sw.Flush();
 45                 }
 46                 //读取文件,以下三种方式读取,都是把文件直接读取到内存中
 47                 foreach (var item in File.ReadLines(fileName))
 48                 {
 49                     Console.WriteLine(item);
 50                 }
 51                 string strResult = File.ReadAllText(fileName);
 52 
 53                 Byte[] bt = File.ReadAllBytes(fileName);
 54                 string strValue = Encoding.UTF8.GetString(bt);
 55 
 56                 //大文件读写
 57                 using (FileStream stream = File.OpenRead(fileName))//分批读取
 58                 {
 59                     int length = 5;
 60                     int result = 0;
 61 
 62                     do
 63                     {
 64                         byte[] bytes = new byte[length];
 65                         result = stream.Read(bytes, 0, 5);
 66                         for (int i = 0; i < result; i++)
 67                         {
 68                             Console.WriteLine(bytes[i].ToString());
 69                         }
 70                     }
 71                     while (length == result);
 72                 }
 73 
 74                 File.Copy(fileName, fileNameCopy);
 75                 File.Move(fileName, fileNameMove);
 76                 File.Delete(fileNameCopy);
 77                 File.Delete(fileNameMove);//尽量不要delete
 78             }
 79         }
 80         /// <summary>
 81         /// 写日志函数
 82         /// </summary>
 83         /// <param name="strmsg"></param>
 84         public static void Log(string strmsg)
 85         {
 86             StreamWriter sw = null;
 87             try
 88             {
 89                 string strFileName = $"{DateTime.Now.ToString("yyyyMMdd")}.txt";
 90                 string totalPatch = Path.Combine(LogPath, strFileName);
 91                 if (!Directory.Exists(LogPath))
 92                 {
 93                     Directory.CreateDirectory(LogPath);
 94                 }
 95                 sw = File.AppendText(totalPatch);
 96                 sw.WriteLine($"{DateTime.Now}:{strmsg}");
 97             }
 98             catch (Exception ex)
 99             {
100                 Console.WriteLine(ex.Message);
101             }
102             finally
103             {
104                 if (sw != null)
105                 {
106                     sw.Flush();
107                     sw.Close();
108                     sw.Dispose();
109                 }
110             }
111         }
112     }
113 }

 

递归小知识

应用场景,处理树形结构

优点:不需要知道有多少层要递归

缺点:内存爆掉

 

序列化

 有几种序列化:

二进制

XML

Json:用的越来越多

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Web.Script.Serialization;//System.Web.Extensions
 7 using Newtonsoft.Json;//nuget Newtonsoft
 8 
 9 namespace IOSerialize.Serialize
10 {
11     public class JsonHelper
12     {
13         #region Json
14         /// <summary>
15         /// .net框架提供
16         /// JavaScriptSerializer
17         /// </summary>
18         /// <typeparam name="T"></typeparam>
19         /// <param name="obj"></param>
20         /// <returns></returns>
21         public static string ObjectToString<T>(T obj)
22         {
23             JavaScriptSerializer jss = new JavaScriptSerializer();
24             return jss.Serialize(obj);
25         }
26 
27         /// <summary>
28         /// .net框架提供
29         /// JavaScriptSerializer
30         /// </summary>
31         /// <typeparam name="T"></typeparam>
32         /// <param name="content"></param>
33         /// <returns></returns>
34         public static T StringToObject<T>(string content)
35         {
36             JavaScriptSerializer jss = new JavaScriptSerializer();
37             return jss.Deserialize<T>(content);
38         }
39 
40         /// <summary>
41         /// 用的比较多,比微软提供的性能高,这个是第三方的类库
42         /// 注意:要注意版本,版本不同可能会导致不同,版本要用最新的
43         /// JsonConvert.SerializeObject
44         /// </summary>
45         /// <typeparam name="T"></typeparam>
46         /// <param name="obj"></param>
47         /// <returns></returns>
48         public static string ToJson<T>(T obj)
49         {
50             return JsonConvert.SerializeObject(obj);
51         }
52 
53         /// <summary>
54         /// 用的比较多,比微软提供的性能高,这个是第三方的类库
55         /// 注意:,版本要用最新的
56         /// JsonConvert.DeserializeObject
57         /// </summary>
58         /// <typeparam name="T"></typeparam>
59         /// <param name="content"></param>
60         /// <returns></returns>
61         public static T ToObject<T>(string content)
62         {
63             return JsonConvert.DeserializeObject<T>(content);
64         }
65         #endregion Json
66     }
67 }

 

posted @ 2022-07-25 21:10  东方承丘  阅读(77)  评论(0编辑  收藏  举报