查看: 3628|回复: 0

[.NET开发] C#文件操作类分享

发表于 2018-2-9 08:00:00

本文实例为大家分享了C#文件操作类的具体代码,供大家参考,具体内容如下

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using System.Reflection;
  6. using System.Collections;
  7. using System.Data.Common;
  8. namespace DotNet.Utilities
  9. {
  10. //JSON转换类
  11. public class ConvertJson
  12. {
  13. #region 私有方法
  14. /// <summary>
  15. /// 过滤特殊字符
  16. /// </summary>
  17. private static string String2Json(String s)
  18. {
  19. StringBuilder sb = new StringBuilder();
  20. for (int i = 0; i < s.Length; i++)
  21. {
  22. char c = s.ToCharArray()[i];
  23. switch (c)
  24. {
  25. case '\"':
  26. sb.Append("\\\""); break;
  27. case '\\':
  28. sb.Append("\\\\"); break;
  29. case '/':
  30. sb.Append("\\/"); break;
  31. case '\b':
  32. sb.Append("\\b"); break;
  33. case '\f':
  34. sb.Append("\\f"); break;
  35. case '\n':
  36. sb.Append("\\n"); break;
  37. case '\r':
  38. sb.Append("\\r"); break;
  39. case '\t':
  40. sb.Append("\\t"); break;
  41. default:
  42. sb.Append(c); break;
  43. }
  44. }
  45. return sb.ToString();
  46. }
  47. /// <summary>
  48. /// 格式化字符型、日期型、布尔型
  49. /// </summary>
  50. private static string StringFormat(string str, Type type)
  51. {
  52. if (type == typeof(string))
  53. {
  54. str = String2Json(str);
  55. str = "\"" + str + "\"";
  56. }
  57. else if (type == typeof(DateTime))
  58. {
  59. str = "\"" + str + "\"";
  60. }
  61. else if (type == typeof(bool))
  62. {
  63. str = str.ToLower();
  64. }
  65. else if (type != typeof(string) && string.IsNullOrEmpty(str))
  66. {
  67. str = "\"" + str + "\"";
  68. }
  69. return str;
  70. }
  71. #endregion
  72. #region List转换成Json
  73. /// <summary>
  74. /// List转换成Json
  75. /// </summary>
  76. public static string ListToJson<T>(IList<T> list)
  77. {
  78. object obj = list[0];
  79. return ListToJson<T>(list, obj.GetType().Name);
  80. }
  81. /// <summary>
  82. /// List转换成Json
  83. /// </summary>
  84. public static string ListToJson<T>(IList<T> list, string jsonName)
  85. {
  86. StringBuilder Json = new StringBuilder();
  87. if (string.IsNullOrEmpty(jsonName)) jsonName = list[0].GetType().Name;
  88. Json.Append("{\"" + jsonName + "\":[");
  89. if (list.Count > 0)
  90. {
  91. for (int i = 0; i < list.Count; i++)
  92. {
  93. T obj = Activator.CreateInstance<T>();
  94. PropertyInfo[] pi = obj.GetType().GetProperties();
  95. Json.Append("{");
  96. for (int j = 0; j < pi.Length; j++)
  97. {
  98. Type type = pi[j].GetValue(list[i], null).GetType();
  99. Json.Append("\"" + pi[j].Name.ToString() + "\":" + StringFormat(pi[j].GetValue(list[i], null).ToString(), type));
  100. if (j < pi.Length - 1)
  101. {
  102. Json.Append(",");
  103. }
  104. }
  105. Json.Append("}");
  106. if (i < list.Count - 1)
  107. {
  108. Json.Append(",");
  109. }
  110. }
  111. }
  112. Json.Append("]}");
  113. return Json.ToString();
  114. }
  115. #endregion
  116. #region 对象转换为Json
  117. /// <summary>
  118. /// 对象转换为Json
  119. /// </summary>
  120. /// <param name="jsonObject">对象</param>
  121. /// <returns>Json字符串</returns>
  122. public static string ToJson(object jsonObject)
  123. {
  124. string jsonString = "{";
  125. PropertyInfo[] propertyInfo = jsonObject.GetType().GetProperties();
  126. for (int i = 0; i < propertyInfo.Length; i++)
  127. {
  128. object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
  129. string value = string.Empty;
  130. if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
  131. {
  132. value = "'" + objectValue.ToString() + "'";
  133. }
  134. else if (objectValue is string)
  135. {
  136. value = "'" + ToJson(objectValue.ToString()) + "'";
  137. }
  138. else if (objectValue is IEnumerable)
  139. {
  140. value = ToJson((IEnumerable)objectValue);
  141. }
  142. else
  143. {
  144. value = ToJson(objectValue.ToString());
  145. }
  146. jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ",";
  147. }
  148. jsonString.Remove(jsonString.Length - 1, jsonString.Length);
  149. return jsonString + "}";
  150. }
  151. #endregion
  152. #region 对象集合转换Json
  153. /// <summary>
  154. /// 对象集合转换Json
  155. /// </summary>
  156. /// <param name="array">集合对象</param>
  157. /// <returns>Json字符串</returns>
  158. public static string ToJson(IEnumerable array)
  159. {
  160. string jsonString = "[";
  161. foreach (object item in array)
  162. {
  163. jsonString += ToJson(item) + ",";
  164. }
  165. jsonString.Remove(jsonString.Length - 1, jsonString.Length);
  166. return jsonString + "]";
  167. }
  168. #endregion
  169. #region 普通集合转换Json
  170. /// <summary>
  171. /// 普通集合转换Json
  172. /// </summary>
  173. /// <param name="array">集合对象</param>
  174. /// <returns>Json字符串</returns>
  175. public static string ToArrayString(IEnumerable array)
  176. {
  177. string jsonString = "[";
  178. foreach (object item in array)
  179. {
  180. jsonString = ToJson(item.ToString()) + ",";
  181. }
  182. jsonString.Remove(jsonString.Length - 1, jsonString.Length);
  183. return jsonString + "]";
  184. }
  185. #endregion
  186. #region DataSet转换为Json
  187. /// <summary>
  188. /// DataSet转换为Json
  189. /// </summary>
  190. /// <param name="dataSet">DataSet对象</param>
  191. /// <returns>Json字符串</returns>
  192. public static string ToJson(DataSet dataSet)
  193. {
  194. string jsonString = "{";
  195. foreach (DataTable table in dataSet.Tables)
  196. {
  197. jsonString += "\"" + table.TableName + "\":" + ToJson(table) + ",";
  198. }
  199. jsonString = jsonString.TrimEnd(',');
  200. return jsonString + "}";
  201. }
  202. #endregion
  203. #region Datatable转换为Json
  204. /// <summary>
  205. /// Datatable转换为Json
  206. /// </summary>
  207. /// <param name="table">Datatable对象</param>
  208. /// <returns>Json字符串</returns>
  209. public static string ToJson(DataTable dt)
  210. {
  211. StringBuilder jsonString = new StringBuilder();
  212. jsonString.Append("[");
  213. DataRowCollection drc = dt.Rows;
  214. for (int i = 0; i < drc.Count; i++)
  215. {
  216. jsonString.Append("{");
  217. for (int j = 0; j < dt.Columns.Count; j++)
  218. {
  219. string strKey = dt.Columns[j].ColumnName;
  220. string strValue = drc[i][j].ToString();
  221. Type type = dt.Columns[j].DataType;
  222. jsonString.Append("\"" + strKey + "\":");
  223. strValue = StringFormat(strValue, type);
  224. if (j < dt.Columns.Count - 1)
  225. {
  226. jsonString.Append(strValue + ",");
  227. }
  228. else
  229. {
  230. jsonString.Append(strValue);
  231. }
  232. }
  233. jsonString.Append("},");
  234. }
  235. jsonString.Remove(jsonString.Length - 1, 1);
  236. jsonString.Append("]");
  237. return jsonString.ToString();
  238. }
  239. /// <summary>
  240. /// DataTable转换为Json
  241. /// </summary>
  242. public static string ToJson(DataTable dt, string jsonName)
  243. {
  244. StringBuilder Json = new StringBuilder();
  245. if (string.IsNullOrEmpty(jsonName)) jsonName = dt.TableName;
  246. Json.Append("{\"" + jsonName + "\":[");
  247. if (dt.Rows.Count > 0)
  248. {
  249. for (int i = 0; i < dt.Rows.Count; i++)
  250. {
  251. Json.Append("{");
  252. for (int j = 0; j < dt.Columns.Count; j++)
  253. {
  254. Type type = dt.Rows[i][j].GetType();
  255. Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + StringFormat(dt.Rows[i][j].ToString(), type));
  256. if (j < dt.Columns.Count - 1)
  257. {
  258. Json.Append(",");
  259. }
  260. }
  261. Json.Append("}");
  262. if (i < dt.Rows.Count - 1)
  263. {
  264. Json.Append(",");
  265. }
  266. }
  267. }
  268. Json.Append("]}");
  269. return Json.ToString();
  270. }
  271. #endregion
  272. #region DataReader转换为Json
  273. /// <summary>
  274. /// DataReader转换为Json
  275. /// </summary>
  276. /// <param name="dataReader">DataReader对象</param>
  277. /// <returns>Json字符串</returns>
  278. public static string ToJson(DbDataReader dataReader)
  279. {
  280. StringBuilder jsonString = new StringBuilder();
  281. jsonString.Append("[");
  282. while (dataReader.Read())
  283. {
  284. jsonString.Append("{");
  285. for (int i = 0; i < dataReader.FieldCount; i++)
  286. {
  287. Type type = dataReader.GetFieldType(i);
  288. string strKey = dataReader.GetName(i);
  289. string strValue = dataReader[i].ToString();
  290. jsonString.Append("\"" + strKey + "\":");
  291. strValue = StringFormat(strValue, type);
  292. if (i < dataReader.FieldCount - 1)
  293. {
  294. jsonString.Append(strValue + ",");
  295. }
  296. else
  297. {
  298. jsonString.Append(strValue);
  299. }
  300. }
  301. jsonString.Append("},");
  302. }
  303. dataReader.Close();
  304. jsonString.Remove(jsonString.Length - 1, 1);
  305. jsonString.Append("]");
  306. return jsonString.ToString();
  307. }
  308. #endregion
  309. }
  310. }
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

您可能感兴趣的文章:

  • C# 通用文件上传类
  • C#的FileInfo类实现文件操作实例
  • C#的File类实现文件操作实例详解
  • C#实现读写ini文件类实例
  • C#读取目录下所有指定类型文件的方法
  • 基于C#实现XML文件读取工具类
  • C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】
  • 一个读写csv文件的C#类
  • C#配置文件操作类分享
  • C#实现的文件上传下载工具类完整实例【上传文件自动命名】


回复

使用道具 举报