zimmer9
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:
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."
-------------------------- ---------- ---------- --------
File.Delete(errorFilePath)
I get the error:
"The process cannot access the file 'L:\\F\FS\\C\\TEST\\error.
--------------------------
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
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Also, seems you are not closing your stream (streamwriter)...
http://msdn.microsoft.com/en-us/library/system.io.streamwriter.close(v=vs.110).aspx