locked
How can i use json .net to write to a text file in a new form and then read the text file using json and assign the values to a variables in form1 ? RRS feed

  • Question

  • This is the new form:

    I added to the new form now a class called it Settings but i'm not sure how to use.

    Also added a using the namespace Newtonsoft.Json 

    In this form i want to write the values with keys to the text file. Today i'm just writing lines adding lines to the text file but not all the time in the same order. What i want to do is to add to the text file using json a key and it's value. If for example it's the api so in the text file the api line will be: "Api Key = 54345345ertrsgdfg34554"

    Then in form1 when i read the text file using json i want to parse the value 54345345ertrsgdfg34554 and assign to it a string in form1.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using Newtonsoft.Json;
    
    namespace Test
    {
        public partial class Authentication : Form
        {
            public class Settings
            {
                public string ApiKey { get; set; }
                public string UserId { get; set; }
                public string JsonFileDirectory { get; set; }
            }
    
    
            public static string AuthenticationApplicationDirectory;
            public static string AuthenticationFileName = "Authentication.txt";
            StreamWriter w;
            static string settingsExample;
    
            static Authentication()
            {
                AuthenticationApplicationDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) + "Authentication";
                if (!Directory.Exists(AuthenticationApplicationDirectory))
                {
                    Directory.CreateDirectory(AuthenticationApplicationDirectory);
                }
                AuthenticationFileName = Path.Combine(AuthenticationApplicationDirectory, AuthenticationFileName);
            }
            
    
            public Authentication()
            {
                InitializeComponent();
    
                label1.Visible = false;
                button1.Enabled = false;
                button4.Enabled = false;
    
                cTextBox2.WaterMarkForeColor = Color.Blue;
                cTextBox3.WaterMarkForeColor = Color.Green;
                cTextBox2.WaterMarkActiveForeColor = Color.Blue;
                cTextBox3.WaterMarkActiveForeColor = Color.Green;
                cTextBox1.ForeColor = Color.Red;
                cTextBox2.ForeColor = Color.Blue;
                cTextBox3.ForeColor = Color.Green;
                cTextBox1.WaterMark = "Enter Api Key";
                cTextBox2.WaterMark = "Enter Email Account";
                cTextBox3.WaterMark = "Browse To The Json File Location";
            }
    
            private void Authentication_Load(object sender, EventArgs e)
            {
    
            }
    
            private void cTextBox1_TextChanged(object sender, EventArgs e)
            {
                if (cTextBox1.Text != "Enter Api Key" && cTextBox1.Text != "")
                {
                    button1.Enabled = true;
                }
                else
                {
                    button1.Enabled = false;
                }
            }
    
            private void cTextBox2_TextChanged(object sender, EventArgs e)
            {
                if (cTextBox2.Text != "Enter Email Account" && cTextBox2.Text != "")
                {
                    button4.Enabled = true;
                }
                else
                {
                    button4.Enabled = false;
                }
            }
    
            private void cTextBox3_TextChanged(object sender, EventArgs e)
            {
                if (cTextBox3.Text != "Browse To The Json File Location" && cTextBox3.Text != "")
                    button6.Enabled = false;
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
                label1.Text = "Updating Settings File";
                label1.Visible = true;
                w = new StreamWriter(AuthenticationFileName,true);
                w.WriteLine(cTextBox1.Text);
                w.Close();
                timer1.Start();
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                button4.Enabled = false;
                label1.Text = "Updating Settings File";
                label1.Visible = true;
                w = new StreamWriter(AuthenticationFileName,true);
                w.WriteLine(cTextBox2.Text);
                w.Close();
                timer1.Start();
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                openFileDialog1.InitialDirectory = "c:\\";
                openFileDialog1.Filter = "Json Files (*.json)|*.json";
                openFileDialog1.FilterIndex = 0;
                openFileDialog1.RestoreDirectory = true;
                openFileDialog1.Multiselect = true;
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    cTextBox3.BackColor = Color.White;
                    cTextBox3.ForeColor = Color.Green;
                    cTextBox3.Text = openFileDialog1.FileName;
                    w = new StreamWriter(AuthenticationFileName, true);
                    w.WriteLine(cTextBox3.Text);
                    w.Close();
                    timer1.Start();
                }
                
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            int count = 0;
            private void timer1_Tick(object sender, EventArgs e)
            {
                count += 1;
                if (count == 2)
                {
                    label1.Visible = false;
                    timer1.Stop();
                    count = 0;
                }
            }
        }
    }
    

    And this is my code today now in form1:

    private void Form1_Load(object sender, EventArgs e)
            {
                if (Authentication.
                    AuthenticationApplicationDirectory != null)
                    Authentication.AuthenticationFileName = Path.Combine(Authentication.
                        AuthenticationApplicationDirectory, Authentication.AuthenticationFileName);
                if (File.Exists(Authentication.AuthenticationFileName) &&
                    new FileInfo(Authentication.AuthenticationFileName).Length != 0)
                {
                    string[] lines = File.ReadAllLines(Authentication.AuthenticationFileName);
                    apiKey = lines[0];
                    userid = lines[1];
                    jsonfiledirectory = lines[2];
                }
                else
                {
                    Authentication auth = new Authentication();
                    auth.Show(this);
                }
            }

    Instead just using string[] lines and FileRadAllLines i want to use json to read the text file and parse the values and assign them to the apiKey userid and jsonfiledirectory vars. But i did a mess not sure how to do it the right way if at all.

    Tuesday, December 8, 2015 11:24 AM

Answers

  • You need to learn how to use Json Stringify taken Json object  to string. Then you need to learn  how to use Json DeStringify

    • Marked as answer by Chocolade1972 Tuesday, December 8, 2015 11:49 PM
    Tuesday, December 8, 2015 12:28 PM