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
596 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
2 Comments
 
LVL 74

Accepted Solution

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

Expert Comment

by:anarki_jimbel
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
crm development 2 34
SQl help with selection 14 43
Paging GridView 7 32
Showdialog 8 20
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now