json - How to change the folder where the Enviroment.CurrentDirectory is positioned. C# -
i want change positioning folder in order create file can consume, of on runtime.
is there form target release folder dinamicaly create file there , consume it?
or, if that's not possible,
is possible give instructions move folder found in enviroment.currentdirectory, can place myself in space have permits create file without problem?
like "c:/" if works of course, need place somewhere can modify later.
i'm looking create json file consumption here base code:
public partial class webform1 : system.web.ui.page { list<logindata> lstlogindata = new list<logindata>(); //with 1 check if login successfull public bool loginvalidated = false; string namefile = "somefile.json"; string whereisthefile = environment.currentdirectory + "\\"; datatable dt = new datatable(); protected void page_load(object sender, eventargs e) { whereisthefile += namefile; if (!file.exists(namefile)) { file.create(namefile); whereisthefile = environment.currentdirectory + "\\" + namefile; string someotherstring = whereisthefile; } lstlogindata = parsingreadjsonfile(whereisthefile); if (ispostback) { lstlogindata = (list<logindata>)viewstate["listado"]; if (lstlogindata == null) { lstlogindata = new list<logindata>(); } } if ((!string.isnullorwhitespace(session["name"].tostring())) && (!string.isnullorwhitespace(session["passwrd"].tostring()))) { //here happends if login successfull foreach (logindata usrdata in lstlogindata) { if (usrdata.name == session["name"].tostring()) { if (usrdata.passwrd == session["passwrd"].tostring()) { loginvalidated = true; } } } } if (viewstate["data"] == null) { datatable dtbl = new datatable(); dtbl.columns.add("name"); dtbl.columns.add("passwrd"); viewstate["data"] = dtbl; } } protected void btningresar_click(object sender, eventargs e) { #region codigo sin usar (comentado) //before add value viestate datatable // dt = (datatable)viewstate["data"]; // string _valuename = txbnombre.text.toupper().trim(); // string _valuepassword = txbpassword.text.trim(); // logindata newlogindata = new logindata() { name = _valuename, passwrd = _valuepassword }; // dt.rows.add(newlogindata); // //now bind data // listbox1.datasource = dt; // listbox1.datatextfield = "name"; // listbox1.datavaluefield = "name"; // listbox1.datatextfield = "passwrd"; // listbox1.datavaluefield = "passwrd"; // listbox1.databind(); // txbnombre.text = txbpassword.text = string.empty; //to clear data #endregion session["name"] = txbnombre.text.trim(); session["passwrd"] = txbpassword.text.trim(); } //listo registrar protected void btnregistrar_click(object sender, eventargs e) { parsingwritejsonfile(txbnombre.text, txbpassword.text, "somefile.json"); } public void parsingwritejsonfile(string loginname, string loginpassword, string nameoffile) { list<logindata> oldlogin = parsingreadjsonfile(nameoffile); list<logindata> newlogin = new list<logindata>(); if ((oldlogin != null) && (oldlogin.count() >= 1)) { newlogin.addrange(oldlogin); } logindata newlogindata = new logindata() { name = loginname, passwrd = loginpassword }; newlogin.add(newlogindata); //javascriptserializer ser = new javascriptserializer(); file.writealltext(nameoffile, jsonconvert.serializeobject(newlogin)); } public list<logindata> parsingreadjsonfile(string nameoffile) { string jsonstring = file.readalltext(nameoffile); //javascriptserializer ser = new javascriptserializer(); list<logindata> p1 = new list<logindata>(); return p1 = jsonconvert.deserializeobject<list<logindata>>(jsonstring); } }
you don't need environment.currentdirectory @ , in particular if in web application. instead should use app_data folder , read , write files in folder. moreover, create full path folder use server.mappath method
public partial class webform1 : system.web.ui.page { list<logindata> lstlogindata = new list<logindata>(); //with 1 check if login successfull public bool loginvalidated = false; string namefile = "somefile.json"; // builds full qualified filename json file , // can use standard file.io methods string whereisthefile = server.mappath("~/app_data/" + namefile); datatable dt = new datatable(); protected void page_load(object sender, eventargs e) { if (!file.exists(whereisthefile)) { file.create(whereisthefile); ...... } lstlogindata = parsingreadjsonfile(whereisthefile); ....
Comments
Post a Comment