-1

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);
        }
    }
user57129
  • 309
  • 1
  • 6
  • 17

1 Answers1

0

You don't need Environment.CurrentDirectory at all and in particular if you are in a web application. Instead you should use the APP_DATA folder and read and write your files in that folder. Moreover, to create a full path to this folder you use the Server.MapPath method

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";

    // This builds the full qualified filename to your json file and you
    // can use with the 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);
        ....

Some info on the APP_DATA folder

Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • it seems like a good answer, but can you show the references you use?, for some reason the "Server" doesn't connect with the "MapPath" that i'm using. From where you are getting the Server and the MapPath?, what references you include? Thanks in advance. – user57129 Sep 09 '16 at 14:11
  • The Server static object is defined in the System.Web assembly and to use it you need to add the using _System.Web;_ at the top of your file. This should be present by default in a WebForms project. The full reference is _HttpContext.Current.Server.MapPath_ [See the example here](https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29.aspx) – Steve Sep 09 '16 at 15:36