views:

561

answers:

4

Hi,

If I am trying to delete a file, but at the same time another process is performing a File.Exists(...) on the same file, will that potentially lock the file and cause my process to fail?

+8  A: 

No, File.Exist() only checks that the file in question is on the file system at the specified path. It does not access the file data or headers in anyway, so it won't put a lock on the file.

On a side note, even opening a file won't necessarily lock it. It depends on the access parameters you choose when opening.

Adam Haile
+2  A: 

No, File.Exist doesn't lock the file. A great way to see what's happening under the covers, though, is to look through the source code.

Pull down the source code for the .Net Framework base class libraries and take a look at what's going on behind the scenes. The System.IO.File class, in particular, has some very interesting defaulted selections for file handling, locking, releasing, etc. In instances like yours, seeing the source of what's happening behind the scenes can make all the difference in terms of how you design your application.

Per the link:

What can I do with the Shared Source CLI? There is a wealth of programming language technology in the Shared Source CLI. It is likely to be of interest to a wide audience, including:

  • Developers interested in the internal workings of the .NET Framework can explore this implementation of the CLI to see how garbage collection works, JIT compilation and verification is handled, security protocols implemented, and the organization of frameworks and virtual object systems.
  • Teachers and researchers doing work with advanced compiler technology. Research projects into language extensions, JIT optimizations, and modern garbage collection all have a basis in the Shared Source CLI. Modern compiler courses can be based on the C# or JScript languages implemented on the CLI.
  • People developing their own CLI implementations will find the Shared Source CLI an indispensable guide and adjunct to the ECMA standards.
jro
A: 

hi im having a problem with File.Exist() ...this is my flow

open a comand promt application

create a file caled "killme.txt"

open an excel file using , OleDbCommand(strSql, objConn)

 while 'killme.txt' file exist 

     //do something

 loop

*in mids of application runnnig i manually delete the "killme.txt" this should stop my consol application..but not closing the window....

it works fine if i dont open the excel file in my flow

wen i open the excel file jus like in my flow above then i get this error

cannot delete killme: it it being used by another program or person close any application which might be using the file and try again

any input is appreciated..thanxx a bunch in advance ;)

A: 

This is an answer to anu - and anyone doing something similar with files.

It is pretty vital to access files with a using statement. This ensures you appropriately dispose of your reference to the file.

using (var fs = File.OpenRead(path)) {
    // Do something
}
Sohnee