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