Solved

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
2
628 Views
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:

 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

0
Comment
Question by:zimmer9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

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

Expert Comment

by:anarki_jimbel
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)...

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 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