-2

There is nothing wrong in the syntax of my code but whenever I try to run it keeps saying "The process cannot access the file because it is being used by another process". The only way I am running my application is my ending my application from the task manager. Please help me by explaining why this is happening and how to fix it.

private void btnLogin_Click(object sender, EventArgs e)
{
    if (File.Exists("users.txt"))
    {
        string[] users = File.ReadAllLines("users.txt");
        bool userFound = false;

        foreach (string user in users)
        {
            string[] splitDetails = user.Split('~');

            string username = splitDetails[1];
            string password = splitDetails[2];

            if ((txtBoxUsername.Text == username) && (txtBoxPassword.Text == password))
            {
                userFound = true;
                break;
            }
        }

        if (userFound)
        {
            Hide();
            HomeForm home = new HomeForm();
            home.Show();
        }
        else
        {
            MessageBox.Show("User details are incorrect",
                            "Incorrect details entered");
        }
    }
    else
    {
        MessageBox.Show("No users have been registered", "No users");
    }
}

private void btnRegister_Click(object sender, EventArgs e)
{
    Hide();
    RegisterForm registerForm = new RegisterForm();
    registerForm.Show();
}

This application is for my a level software systems development coursework and I am coding it in c#. I have only been learning c# for the past 5 months so I am still a beginner. I have already tried to find the answer to my problem in stack overflow and other websites.

I am expecting my application to launch when I press run, but instead I get a dialog box saying:

Error Unable to copy file "obj\Debug\SSD AS2 coursework.exe" to "bin\Debug\SSD AS2 coursework.exe". The process cannot access the file 'bin\Debug\SSD AS2 coursework.exe' because it is being used by another process.
SSD AS2 coursework

dferenc
  • 7,163
  • 12
  • 36
  • 42
  • Possible duplicate of [Can't read all lines in file that being used by another process](https://stackoverflow.com/questions/27771319/cant-read-all-lines-in-file-that-being-used-by-another-process) – Lews Therin Jan 02 '19 at 18:18
  • Are you using Visual Studio on Windows? Try shutting down VS and deleting the obj and bin folders in your project. – chadnt Jan 02 '19 at 18:43
  • 1
    Is there a way for the user to EXIT your application after starting it? Based on the code you have posted it is not surprising that you find it necessary to END TASK using the task manager. If there is an EXIT capability already in your application -- please show us that code. Visual Studio is indicating that your app is running when it tries to write a new copy. – David Tansey Jan 02 '19 at 19:28
  • no tere isent a special way to exit the application i just use the red x at the top of the form to exit – Christo Polachan Jan 02 '19 at 19:36
  • yes i am i have colsed all other open applications – Christo Polachan Jan 02 '19 at 19:50
  • 1
    Possible duplicate of [error "unable to copy file because it is being used by another process](https://stackoverflow.com/questions/18321422/error-unable-to-copy-file-because-it-is-being-used-by-another-process) – Peter B Jan 02 '19 at 22:49

2 Answers2

1

Check if you are closing all windows of your application when finalizing the app. You must use Application.Exit() in any events that are going to finalize your application.

You can read more on the Documentation

0

It seems like the file you are trying to open is being used by another process try to close your text editor or another program writing to that file.

it is still possible to overcome the issue by using FileShare.ReadWrite and use the file from multiple processes, example on the following code:

FileStream fileStream = new FileStream("c:\users.txt", FileMode.Open, 
FileAccess.Read, FileShare.ReadWrite);
StreamReader fileReader = new StreamReader(fileStream);

while (!fileReader.EndOfStream)
{
    string user = fileReader.ReadLine();

    string[] splitDetails = user.Split('~');
    // the rest of the user logic in here...
}


fileReader.Close();
fileStream.Close();
Drormat
  • 94
  • 1
  • 6
  • re-read the question. The data file is not the problem. His program isn't even starting because the build is failing. – Jim Mischel Jan 03 '19 at 16:47