1

I'm a student who just started learning C#,

I'm trying to create Sign-Up and Login functions using text files, and save details that the user inputs into the textfile, but when I run my function again it rewrites what is already there instead of starting a new line.

This is a bit of my code that is important

using System;
using System.IO;

namespace SDD_Assessment_2
{
    class Program
    {
        static void Main(string[] args)
        {
            //Write();
            //GetTime();
            var privInfo = SignUp();
            var FullName = Profile(privInfo);
            Profile(privInfo);
            //Test(FullName);
        }
        const string filename = "Workout.txt";
        static string[] SignUp()
        {
            string[] privinfo = new string[4];

            Console.Write("First Name: ");
            privinfo[0] = Console.ReadLine();
            Console.Write("Last Name: ");
            privinfo[1] = Console.ReadLine();
            Console.Write("Email: ");
            privinfo[2] = Console.ReadLine();
            Console.Write("Password: ");
            privinfo[3] = Console.ReadLine();

            StreamWriter NewAccount = new StreamWriter(filename);
            //NewAccount.WriteLine(privinfo[0] + "," + privinfo[1] + "," + privinfo[2] + "," + privinfo[3] + Environment.NewLine);
            File.AppendAllText(filename, privinfo[0] + ", " + privinfo[1] + "," + privinfo[2] + "," + privinfo[3] + Environment.NewLine);
            NewAccount.Close();

            return privinfo;
        }
    }
}

When I run it right now, it says "System.IO.IOException: The process cannot access the file 'filename' because it is being used by another process."

3
  • why you create StreamWriter NewAccount Commented May 13, 2021 at 2:05
  • Yeah, remove that StreamWriter as that's what has the file open when you try to write to it with AppendAllText. Commented May 13, 2021 at 2:12
  • that is so frustrating, I have been following a tutorial but it wasn't quite working for what I wanted to do and I visited 10 different sites and I merged the two together. I have spent 90 minutes trying to create a new line and all I had to do was remove 1 line to create 1 line. Thankyou! Commented May 13, 2021 at 2:30

2 Answers 2

1

Your code is mostly correct. You need to change

StreamWriter NewAccount = new StreamWriter(filename);
//NewAccount.WriteLine(privinfo[0] + "," + privinfo[1] + "," + privinfo[2] + "," + privinfo[3] + Environment.NewLine);
File.AppendAllText(filename, privinfo[0] + ", " + privinfo[1] + "," + privinfo[2] + "," + privinfo[3] + Environment.NewLine);
NewAccount.Close();

to just

File.AppendAllText(filename, privinfo[0] + ", " + privinfo[1] + "," + privinfo[2] + "," + privinfo[3] + Environment.NewLine);

Your allocation of NewAccount opens a stream to the file, which keeps the file handle open. That means your File.AppendAllText can't complete, because the file was previously opened in a non-sharing mode.

If you had deleted File.AppendAllText you wouldn't get the error, but you'd end up with a zero byte file as you wrote nothing to the stream.

As you are not using NewAccount at all, removing those lines will allow File.AppendAllText to complete without error, and... append all the text you expect, which would be the values for first and last names, email and password.

Sign up to request clarification or add additional context in comments.

Comments

0

I try, it work

Don't open stream when you not used

File name include directory

class Program
{
    static void Main(string[] args)
    {
        SignUp();
    }

    const string filename = "Workout.txt";
    static string[] SignUp()
    {
        string[] privinfo = new string[4];

        privinfo[0] = "First Name:";
        privinfo[1] = "Last Name: ";
        privinfo[2] = "Email: ";
        privinfo[3] = "Password: ";

        File.AppendAllText(Path.Combine(Environment.CurrentDirectory, filename), privinfo[0] + ", " + privinfo[1] + "," + privinfo[2] + "," + privinfo[3] + Environment.NewLine);

        return privinfo;
    }
}

Run 2 times

file content is

First Name:, Last Name: ,Email: ,Password: 
First Name:, Last Name: ,Email: ,Password: 

2 Comments

I think OP wants values, not labels.
So I just test File.AppendAllText, I don't need Console.ReadLine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.