C#文件操作类分享
本文实例为大家分享了C#文件操作类的具体代码,供大家参考,具体内容如下
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Text;
usingSystem.Data;
usingSystem.Reflection;
usingSystem.Collections;
usingSystem.Data.Common;
namespaceDotNet.Utilities
{
//JSON转换类
publicclassConvertJson
{
#region私有方法
///
///过滤特殊字符
///
privatestaticstringString2Json(Strings)
{
StringBuildersb=newStringBuilder();
for(inti=0;i
///格式化字符型、日期型、布尔型
///
privatestaticstringStringFormat(stringstr,Typetype)
{
if(type==typeof(string))
{
str=String2Json(str);
str="\""+str+"\"";
}
elseif(type==typeof(DateTime))
{
str="\""+str+"\"";
}
elseif(type==typeof(bool))
{
str=str.ToLower();
}
elseif(type!=typeof(string)&&string.IsNullOrEmpty(str))
{
str="\""+str+"\"";
}
returnstr;
}
#endregion
#regionList转换成Json
///
///List转换成Json
///
publicstaticstringListToJson(IListlist)
{
objectobj=list[0];
returnListToJson(list,obj.GetType().Name);
}
///
///List转换成Json
///
publicstaticstringListToJson(IListlist,stringjsonName)
{
StringBuilderJson=newStringBuilder();
if(string.IsNullOrEmpty(jsonName))jsonName=list[0].GetType().Name;
Json.Append("{\""+jsonName+"\":[");
if(list.Count>0)
{
for(inti=0;i();
PropertyInfo[]pi=obj.GetType().GetProperties();
Json.Append("{");
for(intj=0;j
///对象转换为Json
///
///对象
///Json字符串
publicstaticstringToJson(objectjsonObject)
{
stringjsonString="{";
PropertyInfo[]propertyInfo=jsonObject.GetType().GetProperties();
for(inti=0;i
///对象集合转换Json
///
///集合对象
///Json字符串
publicstaticstringToJson(IEnumerablearray)
{
stringjsonString="[";
foreach(objectiteminarray)
{
jsonString+=ToJson(item)+",";
}
jsonString.Remove(jsonString.Length-1,jsonString.Length);
returnjsonString+"]";
}
#endregion
#region普通集合转换Json
///
///普通集合转换Json
///
///集合对象
///Json字符串
publicstaticstringToArrayString(IEnumerablearray)
{
stringjsonString="[";
foreach(objectiteminarray)
{
jsonString=ToJson(item.ToString())+",";
}
jsonString.Remove(jsonString.Length-1,jsonString.Length);
returnjsonString+"]";
}
#endregion
#regionDataSet转换为Json
///
///DataSet转换为Json
///
///DataSet对象
///Json字符串
publicstaticstringToJson(DataSetdataSet)
{
stringjsonString="{";
foreach(DataTabletableindataSet.Tables)
{
jsonString+="\""+table.TableName+"\":"+ToJson(table)+",";
}
jsonString=jsonString.TrimEnd(',');
returnjsonString+"}";
}
#endregion
#regionDatatable转换为Json
///
///Datatable转换为Json
///
///Datatable对象
///Json字符串
publicstaticstringToJson(DataTabledt)
{
StringBuilderjsonString=newStringBuilder();
jsonString.Append("[");
DataRowCollectiondrc=dt.Rows;
for(inti=0;i
///DataTable转换为Json
///
publicstaticstringToJson(DataTabledt,stringjsonName)
{
StringBuilderJson=newStringBuilder();
if(string.IsNullOrEmpty(jsonName))jsonName=dt.TableName;
Json.Append("{\""+jsonName+"\":[");
if(dt.Rows.Count>0)
{
for(inti=0;i
///DataReader转换为Json
///
///DataReader对象
///Json字符串
publicstaticstringToJson(DbDataReaderdataReader)
{
StringBuilderjsonString=newStringBuilder();
jsonString.Append("[");
while(dataReader.Read())
{
jsonString.Append("{");
for(inti=0;i
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。