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

Skyworks
  • 5
  • 4
Tris
  • 169
  • 1
  • 11
  • why you create StreamWriter NewAccount – Allen Hu May 13 '21 at 02:05
  • Yeah, remove that `StreamWriter` as that's what has the file open when you try to write to it with `AppendAllText`. – juharr May 13 '21 at 02: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! – Tris May 13 '21 at 02:30

2 Answers2

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.

Kit
  • 20,354
  • 4
  • 60
  • 103
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: 
Allen Hu
  • 141
  • 6