简体   繁体   中英

How can i fix the 'System.IO.IOException: 'The process cannot access the file'

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;
                        }

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.

Ps You should also refactor your code as it has flaws.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM