-3
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 Youtube_Player
{
    public partial class Authentication : Form
    {
        public static string AuthenticationApplicationDirectory;
        public static string AuthenticationFileName = "Authentication.txt";
        StreamWriter w;
        public static bool formclosed = false;

        static Authentication()
        {
            AuthenticationApplicationDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) + "Authentication";
            if (!Directory.Exists(AuthenticationApplicationDirectory))
            {
                Directory.CreateDirectory(AuthenticationApplicationDirectory);
            }
            AuthenticationFileName = Path.Combine(AuthenticationApplicationDirectory, AuthenticationFileName);

            if (!File.Exists(AuthenticationFileName))
                File.Create(AuthenticationFileName);
        }


        public Authentication()
        {
            InitializeComponent();

            cTextBox1.Text = Form1.AuthenticationValues.ApiKey;
            cTextBox2.Text = Form1.AuthenticationValues.UserId;
            cTextBox3.Text = Form1.AuthenticationValues.JsonFileDirectory;

            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.Text = "Confirmed";
            button1.Enabled = false;
            label1.Text = "Updating Settings File";
            label1.Visible = true;
            if (label1.Visible == true)
            {
                button6.Enabled = false;
                button4.Enabled = false;
                button1.Enabled = false;
            }
            FileInfo fi = new FileInfo(AuthenticationFileName);
            if (fi.Length == 0)
            {
                w = new StreamWriter(AuthenticationFileName, true);
                w.WriteLine("Api" + "=" + cTextBox1.Text);
                w.Close();
            }
            else
            {
                w = new StreamWriter(AuthenticationFileName);
                w.WriteLine("Api" + "=" + 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;
            if (label1.Visible == true)
            {
                button6.Enabled = false;
                button4.Enabled = false;
                button1.Enabled = false;
            }
            w = new StreamWriter(AuthenticationFileName, true);
            w.WriteLine("UserId" + "=" + 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;
                label1.Text = "Updating Settings File";
                label1.Visible = true;
                if (label1.Visible == true)
                {
                    button6.Enabled = false;
                    button4.Enabled = false;
                    button1.Enabled = false;
                }
                w = new StreamWriter(AuthenticationFileName, true);
                w.WriteLine("JsonFileDirectory" + "=" + cTextBox3.Text);
                w.Close();

                timer1.Start();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ResetValues(true);
        }

        int count = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            count += 1;
            if (count == 2)
            {
                label1.Visible = false;
                timer1.Stop();
                count = 0;
            }
        }

        private void Authentication_FormClosing(object sender, FormClosingEventArgs e)
        {
            ResetValues(false);
        }

        private void ResetValues(bool DeleteFile)
        {
            cTextBox1.Text = "";
            cTextBox2.Text = "";
            cTextBox3.Text = "";
            button1.Text = "Confirm";
            button4.Text = "Confirm";
            button6.Enabled = true;
            timer1.Stop();
            count = 0;
            if (DeleteFile == true)
            {
                if (File.Exists(AuthenticationFileName))
                    File.Delete(AuthenticationFileName);
            }
            Form1.AuthenticationValues.ApiKey = "";
            Form1.AuthenticationValues.JsonFileDirectory = "";
            Form1.AuthenticationValues.UserId = "";
            if (Form1.AuthenticationValues.AuthenticationMenu.Enabled
                == false && (Form1.lines.Length < 3 && Form1.lines.Length > 0))
                Form1.AuthenticationValues.AuthenticationMenu.Enabled = true;
            formclosed = true;
        }
    }
}

The first problem is in the constructor when checking for the file existing:

if (!File.Exists(AuthenticationFileName))
                File.Create(AuthenticationFileName);

This make the file to be busy in use. When later in my code i'm trying to use the file again to write to it i'm getting exception that the file is in use by another process.

The second problem is in the 3 places i'm trying to write to the file. The first place:

w = new StreamWriter(AuthenticationFileName, true);
w.WriteLine("Api" + "=" + cTextBox1.Text);
w.Close();

Then under it in another place in the code i'm writing again to the file:

w = new StreamWriter(AuthenticationFileName, true);
w.WriteLine("UserId" + "=" + cTextBox2.Text);
w.Close();

And last:

w = new StreamWriter(AuthenticationFileName, true);
w.WriteLine("JsonFileDirectory" + "=" + cTextBox3.Text);
w.Close();

The problems are first the file is busy in use since the checking if exist i'm doing in the constructor.

The second problem is that i want to make that if in the file there is no line that start with "Api" then write the Api part:

w.WriteLine("Api" + "=" + cTextBox1.Text);

But if it does exist and the text is changed in the textBox cTextBox1.Text i want to write to the file the changed text to the place where the Api line is. And to append a new Api line.

Same for all two other places i'm writing to the file. If i will make it all false not to appen when writing it will write one line each time and will overwrite the line. But if i'm doing true it will append many Api lines or many UserId lines. And i want each to time to replace the line with the textBox to overwrite only on this line but with the new text.

If in cTextBox1 there is the text: Hello World Then i changed it to: Hello World Now Then replace the Api line Hello World with Hello World Now

  • 1
    Please make sure to read [MCVE] guidance - there is no point of pasting wall of code where 2 lines are enough. (Clearly searching for similar problems would be nice, but most people are fine with downvotes for "no research shown" - so most consider search optional) – Alexei Levenkov Apr 06 '16 at 18:33

1 Answers1

1

Alright, addressing your main concern:

File.CreateFile actually opens a filestream for you to use. Easiest way to solve this is to do File.CreateFile().Close() to close it when you're done.

Next, you are trying to change data in a file, but you never read it to find the information.

You can look up StreamReader and use that, and parse out data and figure out what's where, but there's a much more obvious choice: Just re-write all of the information every time.

oppassum
  • 1,746
  • 13
  • 22