Wednesday 15 January 2014

c# - Append to a json file in windows application -



c# - Append to a json file in windows application -

i have created json format file in windows application using newtonsoft.the code working , json creating fine.but want append new file old json file,now replacing.here code

list<devicedata> tempdate = new list<devicedata>(); devicedata d = new devicedata(); d.deviceid = st_id.tostring(); d.ansid = answerstr; tempdate.add(d); string ans = jsonconvert.serializeobject(tempdate, formatting.indented); system.io.file.writealltext(@"e:\" + " device.json", ans);

can 1 help.thanks in advance.

according documentation, method writealltext creates new file, writes specified string file, , closes file. if target file exists, overwritten.

to accomplish want need check whether file exists or not

string ans = jsonconvert.serializeobject(tempdate, formatting.indented); if (file.exists(@"e:\" + " device.json") file.appendalltext(@"e:\" + " device.json", appendtext); else system.io.file.writealltext(@"e:\" + " device.json", ans);

c# json json.net

No comments:

Post a Comment