SoFunction
Updated on 2025-03-07

C# file operation class sharing

This article has shared the specific code of C# file operation for your reference. The specific content is as follows

using System;
using ;
using ;
using ;
using ;
using ;
using ;

namespace 
{
 //JSON conversion class public class ConvertJson
 {
 #region Private Method /// <summary>
 /// Filter special characters /// </summary>
 private static string String2Json(String s)
 {
  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < ; i++)
  {
  char c = ()[i];
  switch (c)
  {
   case '\"':
   ("\\\""); break;
   case '\\':
   ("\\\\"); break;
   case '/':
   ("\\/"); break;
   case '\b':
   ("\\b"); break;
   case '\f':
   ("\\f"); break;
   case '\n':
   ("\\n"); break;
   case '\r':
   ("\\r"); break;
   case '\t':
   ("\\t"); break;
   default:
   (c); break;
  }
  }
  return ();
 }

 /// <summary>
 /// Format character type, date type, boolean type /// </summary>
 private static string StringFormat(string str, Type type)
 {
  if (type == typeof(string))
  {
  str = String2Json(str);
  str = "\"" + str + "\"";
  }
  else if (type == typeof(DateTime))
  {
  str = "\"" + str + "\"";
  }
  else if (type == typeof(bool))
  {
  str = ();
  }
  else if (type != typeof(string) && (str))
  {
  str = "\"" + str + "\"";
  }
  return str;
 }
 #endregion

 #region List converted to Json /// <summary>
 /// Convert List to Json /// </summary>
 public static string ListToJson<T>(IList<T> list)
 {
  object obj = list[0];
  return ListToJson<T>(list, ().Name);
 }

 /// <summary>
 /// Convert List to Json /// </summary>
 public static string ListToJson<T>(IList<T> list, string jsonName)
 {
  StringBuilder Json = new StringBuilder();
  if ((jsonName)) jsonName = list[0].GetType().Name;
  ("{\"" + jsonName + "\":[");
  if ( > 0)
  {
  for (int i = 0; i < ; i++)
  {
   T obj = <T>();
   PropertyInfo[] pi = ().GetProperties();
   ("{");
   for (int j = 0; j < ; j++)
   {
   Type type = pi[j].GetValue(list[i], null).GetType();
   ("\"" + pi[j].() + "\":" + StringFormat(pi[j].GetValue(list[i], null).ToString(), type));

   if (j <  - 1)
   {
    (",");
   }
   }
   ("}");
   if (i <  - 1)
   {
   (",");
   }
  }
  }
  ("]}");
  return ();
 }
 #endregion

 #region object converted to Json /// <summary> 
 /// Convert object to Json /// </summary> 
 /// <param name="jsonObject">Object</param> /// <returns>Json string</returns> public static string ToJson(object jsonObject)
 {
  string jsonString = "{";
  PropertyInfo[] propertyInfo = ().GetProperties();
  for (int i = 0; i &lt; ; i++)
  {
  object objectValue = propertyInfo[i].GetGetMethod().Invoke(jsonObject, null);
  string value = ;
  if (objectValue is DateTime || objectValue is Guid || objectValue is TimeSpan)
  {
   value = "'" + () + "'";
  }
  else if (objectValue is string)
  {
   value = "'" + ToJson(()) + "'";
  }
  else if (objectValue is IEnumerable)
  {
   value = ToJson((IEnumerable)objectValue);
  }
  else
  {
   value = ToJson(());
  }
  jsonString += "\"" + ToJson(propertyInfo[i].Name) + "\":" + value + ",";
  }
  ( - 1, );
  return jsonString + "}";
 }
 #endregion

 #region object collection conversion Json /// &lt;summary&gt; 
 /// Object collection conversion Json /// &lt;/summary&gt; 
 /// <param name="array">collection object</param> /// <returns>Json string</returns> public static string ToJson(IEnumerable array)
 {
  string jsonString = "[";
  foreach (object item in array)
  {
  jsonString += ToJson(item) + ",";
  }
  ( - 1, );
  return jsonString + "]";
 }
 #endregion

 #region Normal collection conversion Json /// &lt;summary&gt; 
 /// Normal collection conversion Json /// &lt;/summary&gt; 
 /// <param name="array">collection object</param> /// <returns>Json string</returns> public static string ToArrayString(IEnumerable array)
 {
  string jsonString = "[";
  foreach (object item in array)
  {
  jsonString = ToJson(()) + ",";
  }
  ( - 1, );
  return jsonString + "]";
 }
 #endregion

 #region DataSet convert to Json /// &lt;summary&gt; 
 /// Convert DataSet to Json /// &lt;/summary&gt; 
 /// <param name="dataSet">DataSet object</param> /// <returns>Json string</returns> public static string ToJson(DataSet dataSet)
 {
  string jsonString = "{";
  foreach (DataTable table in )
  {
  jsonString += "\"" +  + "\":" + ToJson(table) + ",";
  }
  jsonString = (',');
  return jsonString + "}";
 }
 #endregion

 #region Datatable to Json /// &lt;summary&gt; 
 /// Convert Datatable to Json /// &lt;/summary&gt; 
 /// <param name="table">Datatable object</param> /// <returns>Json string</returns> public static string ToJson(DataTable dt)
 {
  StringBuilder jsonString = new StringBuilder();
  ("[");
  DataRowCollection drc = ;
  for (int i = 0; i &lt; ; i++)
  {
  ("{");
  for (int j = 0; j &lt; ; j++)
  {
   string strKey = [j].ColumnName;
   string strValue = drc[i][j].ToString();
   Type type = [j].DataType;
   ("\"" + strKey + "\":");
   strValue = StringFormat(strValue, type);
   if (j &lt;  - 1)
   {
   (strValue + ",");
   }
   else
   {
   (strValue);
   }
  }
  ("},");
  }
  ( - 1, 1);
  ("]");
  return ();
 }

 /// &lt;summary&gt;
 /// Convert DataTable to Json /// &lt;/summary&gt;
 public static string ToJson(DataTable dt, string jsonName)
 {
  StringBuilder Json = new StringBuilder();
  if ((jsonName)) jsonName = ;
  ("{\"" + jsonName + "\":[");
  if ( &gt; 0)
  {
  for (int i = 0; i &lt; ; i++)
  {
   ("{");
   for (int j = 0; j &lt; ; j++)
   {
   Type type = [i][j].GetType();
   ("\"" + [j].() + "\":" + StringFormat([i][j].ToString(), type));
   if (j &lt;  - 1)
   {
    (",");
   }
   }
   ("}");
   if (i &lt;  - 1)
   {
   (",");
   }
  }
  }
  ("]}");
  return ();
 }
 #endregion

 #region DataReader to Json /// &lt;summary&gt; 
 /// Convert DataReader to Json /// &lt;/summary&gt; 
 /// <param name="dataReader">DataReader object</param> /// <returns>Json string</returns> public static string ToJson(DbDataReader dataReader)
 {
  StringBuilder jsonString = new StringBuilder();
  ("[");
  while (())
  {
  ("{");
  for (int i = 0; i &lt; ; i++)
  {
   Type type = (i);
   string strKey = (i);
   string strValue = dataReader[i].ToString();
   ("\"" + strKey + "\":");
   strValue = StringFormat(strValue, type);
   if (i &lt;  - 1)
   {
   (strValue + ",");
   }
   else
   {
   (strValue);
   }
  }
  ("},");
  }
  ();
  ( - 1, 1);
  ("]");
  return ();
 }
 #endregion
 }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.