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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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