0

Possible Duplicate:
FileStream with locked file

I am trying to build an AV for my college project and during scanning I am getting exception-Access denied,file is used by another process and hence can't scan the file.Also when it's opened in windows explorer,same thing happens.I also used Fileshare.ReadWrite but of no use.It'mostly with C: drive files.

Community
  • 1
  • 1
  • 1
    Try the code posted here : http://stackoverflow.com/questions/1625042/the-process-cannot-access-the-file-because-it-is-being-used-by-another-process/1625089#1625089 If you got an exception, add the details to your post with the file path. – Guillaume Dec 06 '11 at 08:41

3 Answers3

2

It probably is related to your operating system (which might be Windows), not to the particular language (C#) you are using. If exclusive access is required by the operating system kernel, it should be hard or impossible to avoid.

If you coded on Linux or other Posix systems, you won't have that issue: several processes can access the same file at once (and you can use locking to prevent that).

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

Just use FileShare.Read, not ReadWrite. You will also have to accept that there will be some files that will not be scannable (i.e. pagefile), so you should add an exception handler around your read code and just ignore (or report) exceptions with reading the file.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
0

You could try using Stream:

Stream stream = new FileStream(localPath, FileMode.Open, FileAccess.Read, FileShare.Read)

Just don't forget to dispose it in the end, or enclose it in using statement

Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52