0

i want to know how i can delete file without the System.IO.IOException it keeps saying that "System.IO.IOException: 'The process cannot access the file 'C:\A1\AccNum.txt' because it is being used by another process.'"

       private static void deleteaccount()
       {
        int accountNum2;
        string accountVar = string.Empty;
        const int MaxLength = 10;
        string line2;

        Console.WriteLine("DELETE AN ACCOUNT");
        Console.WriteLine("=================");
        Console.WriteLine("ENTER THE DETAILS");
        Console.WriteLine("");
        Console.WriteLine("Account Number: {0}", accountVar);
        accountVar = Console.ReadLine();
        Console.WriteLine("=================");

        try
        {
            accountNum2 = Convert.ToInt32(accountVar);
        }
        catch (OverflowException ex)
        {
            Console.WriteLine("The Error is {0}: ", ex);
        }
        finally
        {
            if (accountVar.Length < MaxLength && accountVar.Length > 5)
            {
                System.IO.StreamReader file = new System.IO.StreamReader(@"C://A1//AccNum.txt");
                while ((line2 = file.ReadLine()) != null)
                    if (line2.Contains(accountVar))
                    {
                        Console.WriteLine("Account Found! Details display below...");
                        string text2 = File.ReadAllText(@"C://A1//AccNum.txt");
                        Console.WriteLine(text2);
                        Console.WriteLine("=================");
                        Console.WriteLine("");
                        Console.Write("Delete? (y/n): ");
                        string answer2;
                        answer2 = Console.ReadLine();
                        if ((answer2.Contains("y") || answer2.Contains("Y")))
                        {
                            if (File.Exists(@"C://A1//AccNum.txt"))
                            {
                                File.Delete(@"C://A1//AccNum.txt"); // This part says it has 'System.IO.IOException: 'The process cannot access the file 'C:\A1\AccNum.txt' because it is being used by another process.''
                            }

                            Console.Write("Account Deleted!...");
                            Console.Read();
                            ShowMainMenu();
                            break;
                        }
3
  • Your StreamReader still has the file open. This code overall is not very clear, and you keep re-reading the same file over and over, all while keeping another reader open on the file. How many times do you really need to read the same file? What exactly is this code attempting to do? It looks like you don't need or want the StreamReader at all. You can just read the whole file into memory (with ReadAllText or ReadAllLines or anything of that nature) and perform all of your logic on that in-memory data. Then you won't have any file handles open and can delete it whenever you want. Commented Sep 15, 2019 at 18:38
  • The other answers address why you're getting the exception, but I'd also suggest that you rethink / refactor how the method is working, especially from the perspective of the amount of code in the finally block - there is a lot that could be going wrong in there. That said, if you take the advice from the other folks then I'd expect your method will be a lot smaller / simpler. Commented Sep 15, 2019 at 19:22
  • Always use the using statement when dealing with files. Commented Sep 15, 2019 at 21:54

2 Answers 2

1

You are using the file yourself (using the reader) and you cannot delete it unless you stop using it.

Close the reader inside the if block, then attempt to delete the file.

When you use File.ReadAllText you do not need to close anything (and in other static members of File), but for StreamReader you need to close the stream yourself.

P.s You should also refactor your code as it has flaws.

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

Comments

1

AccNum.txt file is opened twice by your code. And while you're opening the file, same time you're trying to delete the file which triggers IOException.

If you want to delete the file make sure that no other program has opened your file. You cant have already opened file handles to a file which you are trying to delete. On your program, you're the one opening the file while deleting it. So release the file handles first.

 if (File.Exists(@"C://A1//AccNum.txt"))
 {
     // file is your StreamReader.
     file.Close();
     file.Dispose();
     File.Delete(@"C://A1//AccNum.txt"); 
 }

Also note that you're opening same file 2 or multiple times if accountVar is found in your file. Better choice would be sticking to a single File.ReadAllText() call and use that content.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.