I want to change the positioning folder in order to create a file who can be consume, all of this on runtime.
Is there any form to target the Release folder dinamicaly to create the file there and consume it?
Or, if that's not possible,
Is it possible to give instructions to move the folder where i found in Enviroment.CurrentDirectory, so i can place myself in a space where i have permits to create the file without any problem?
like "C:/" if that works of course, i need place it somewhere where i can modify it later.
I'm looking to create a json file for consumption here is the base code:
public partial class WebForm1 : System.Web.UI.Page
{
List<LoginData> lstLoginData = new List<LoginData>();
//with this one you check if login was 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 is 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 get viestate to 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 the 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);
}
}