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

Posted on 2014-03-11
Medium Priority
Last Modified: 2014-03-11
I am developing a C# console application using VS2010 and in the following snippet of code, when the following statement is executed:


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))

            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

Question by:zimmer9
LVL 75

Accepted Solution

käµfm³d   👽 earned 2000 total points
ID: 39921672
Try closing your StreamWriter just after line 19.
LVL 30

Expert Comment

ID: 39921676
Did you check, may be your file was opened in some editor like Notepad?

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


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Hi, this video explains a free download that you can incorporate into your Access databases, or use stand-alone for contact management. Contacts -- Names, Addresses, Phone Numbers, eMail Addresses, Websites, Lists, Projects, Notes, Attachments…
Watch the video to know the simple way to remove or recover or reset lost or forgotten passwords of Outlook PST file. With Kernel Outlook Password Recovery tool such operation is very easy to perform. It is a freeware with limitation to use with 500…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question