The process cannot access the file (in Windows2003 only)

  • Thread starter Thread starter Anthony Lopez
  • Start date Start date
A

Anthony Lopez

I have an application that is working on WindowsXP and Windows2000. I
recently moved it onto a new Windows2003 Server Service Pack 2 machine. It
is
now getting an error message which reads "The process cannot access the file
<filename> because it is being used by another process"

this is what I have (abreviated version):

If File.Exist(FullPathName) Then
Dim sr As StreamReader = File.OpenText(FullPathName)
Dim sw As StreamWriter = File.CreateText(NewFullPathName)

... do some work

sw.Flush()
sw.Close()
sr.Close()

File.Delete(FullPathName)
File.Move(NewFullPathName, FullPathName)
End If
 
use Dispose() on your readers and writers.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box! |
*************************************************
 
Sure I can add the dispose(), but I am having an issue when I try to open
the file the initial time.

Dim sr As StreamReader = File.OpenText(FullPathName)
 
put the code change
deploy the code.
reboot the server.

then see what happens.

look at the "using" statement as well.
 
Well I looked at using and it appears to be a C# keyword.

Also, close() calls dispose(true) for a StreamReader/StreamWriter.

Did I mentioned that I was using VB.Net and version 1.1 .Net ?

Maybe a good opportunity again mention that this code is working in XP and
2000. I am only have trouble in 2003 server. Why doesn't it work here -
this is the answer I am looking for!!!!
 
The code has been modified as follows based on the advisement of another
posting.

If File.Exist(FullPathName) Then

Dim sr As StreamReader
Dim sw As StreamWriter
try
sr = File.OpenText(FullPathName)
try
sw = File.CreateText(NewFullPathName)

... do your work ..

catch ex as exception
msgbox("! OPEN FOR WRITING ERROR : "+ex.Message)
finally
sw.close()
end try
catch ex as exception
msgbox("! OPEN FOR READING ERROR: "+ex.Message)
finally
sr.Close()
end try

try
File.Delete(FullPathName)
try
File.Move(NewFullPathName, FullPathName)
catch ex as exception
msgbox("! ERROR MOVING FILE: "+ex.Message)
end try
catch ex as exception
msgbox("! ERROR DELETING FILE: "+ex.Message)
end try

End If

The message box displayed was indeed the


"! OPEN FOR READING ERROR: The process cannot access the file <filename>
because it is being used by another process"

As can be seen, we did not make it to the point were we would attempt to
delete and/or move the file. For that matter we did not even make it to the
next step were we create a new text file.

As for the using issue, I have .Net 1.1 and it looks like "using" was added
in 2.0 oh well would have been nice.
 
I guess I see your point about a concise-but-complete code example, I will
work on that tomorrow.

I would like to provide further information, the input file is the same
input file that was opened in windowsXP and windows2000 using the same code
in VB .Net 1.1. I am now using it in windows2003 server where I am now
getting the error.

I should explain further my assumptions. Since the code was working in both
XP and 2000 server, I was looking for differences in local policies, 1.1
framework, security ... And no so much approached on how to code this as I
feel confident that this code base is working (XP, 2K server)

I don't know if that helps any. Anyway thanks for the suggestions and I will
see if I can come up with a complete code example.


The code has been modified as follows based on the advisement of another
posting.

If File.Exist(FullPathName) Then

Dim sr As StreamReader
Dim sw As StreamWriter
try
sr = File.OpenText(FullPathName)
try
sw = File.CreateText(NewFullPathName)

... do your work ..

catch ex as exception
msgbox("! OPEN FOR WRITING ERROR : "+ex.Message)
finally
sw.close()
end try
catch ex as exception
msgbox("! OPEN FOR READING ERROR: "+ex.Message)
finally
sr.Close()
end try
[...]
The message box displayed was indeed the

"! OPEN FOR READING ERROR: The process cannot access the file <filename>
because it is being used by another process"

Well, where does the file come from? If you get that exception when
trying to call OpenText(), then the file must already be opened (note that
the phrasing "by another process" is misleading...even having the file
open within the same process may cause the same error).

Unfortunately, you haven't posted a concise-but-complete code example that
reliably demonstrates the problem. There's really nothing in your
question that provides enough information to know why the exception might
occur, never mind actually reproduce the problem (even assuming someone's
got an installation of Windows 2003 handy to test on...I don't).

Lacking anything directly applicable to suggest, I'll comment on the call
you make to "File.Exist()" and point out that it's completely
superfluous. Having made that call, it is entirely possible that the file
might cease to exist between that point and when you try to open it. The
correct strategy is to simply attempt to open the file; you'll get an
exception stating that the file doesn't exist if it in fact doesn't.

As for the original error you're asking about, you'll either have to
figure that out yourself, or post a concise-but-complete code example that
reliably reproduces the error. The error itself is pretty clear about
what's wrong, but there's no general rule for what might cause the error
to actually happen. That's entirely dependent on the exact file, what
processes use it, and how.

Pete
 
The first time the file has ever been touched?
Or, the first time this run?

If #1, you have another process that is locking the file. If #2, you may
have code that is not being properly garbage collected as you failed to call
Dispose() in the past and .NET, for some reason, was unable to dispose of
the reader/writer in question.

What I mean here is unless you are saying "I have this file that has been
sitting forever on my drive and the first time I ever tried to open it,
after a fresh reboot, it is locked and errors out", you can have dependent
processes that are causing you grief. If you are not Disposing, you are
likely the cause of the error.

You also have to look at your code. If you write and then later read, the
undisposed writer might still have a lock, as it has not been garbage
collected. If you call Dispose(), this is not an issue, as the ties to the
underlying COM-based (or native based) reader/writer is broken.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box! |
*************************************************
 
using(StreamReader reader = new StreamReader(path))
{
}

is a C# concept. I think VB finally gets this in .NET 4.0, but I am not sure
(maybe 3.5?). I am not a VB programmer.

The pattern for using, however, is:

Dim o As New ObjectType(params)

Try
'User object here
Finally
o.Dispose()
End Try

Check the blog entry (yes, code in C#, but the concept is the same):
http://snurl.com/hfgn0

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog: http://gregorybeamer.spaces.live.com
Twitter: @gbworld

*************************************************
| Think outside the box! |
*************************************************
 
Back
Top