Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How to resolve the following error in a C# console application using VS2010: "The process cannot access the file 'error.txt' because it is being used by another process."

I am developing a C# console application using VS2010 and in the following snippet of code, when the following statement is executed:

 File.Delete(errorFilePath)

Open in new window


I get the error:
"The process cannot access the file 'L:\\F\FS\\C\\TEST\\error.txt' because it is being used by another process."

------------------------------------------------------

static void Main()
        {            
            string errorFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, "error.txt");
           
            //delete the error file if it presently exists so that a new error file can be created in the event any errors are discovered
            if (File.Exists(errorFilePath))
            {
                File.Delete(errorFilePath);
            }            

            DirectoryInfo parentDirectory = new DirectoryInfo(MyGlobals.BASE_DIR);

            foreach (FileInfo file in parentDirectory.GetFiles())
            {
                if (!string.Equals(file.Name, MyGlobals.BASE_FILE_Name + ".ard", StringComparison.InvariantCultureIgnoreCase) &&
                    !string.Equals(GetFileTitleOnly(file.Name), GetFileTitleOnly(currentRunningAssembly.ManifestModule.Name), StringComparison.InvariantCultureIgnoreCase))
                {
                    StreamWriter sw1 = new StreamWriter(errorFilePath);
                    ProcessFile(file.FullName, sw1);               
                }
            }

            //if an Error file was created but no information was written to it, then the error file will be deleted 
            //before the application is finished and closes down
            if (File.Exists(errorFilePath))
            {
                if (new FileInfo(errorFilePath).Length == 0)
                {
                    if (File.Exists(errorFilePath))
                    {
                        File.Delete(errorFilePath);       <---- Error occurs here
                    }
                }
            }
           
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did you check, may be your file was opened in some editor like Notepad?

Also, seems you are not closing your stream (streamwriter)...

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.close(v=vs.110).aspx