Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Helloo Everbody

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.I also used Fileshare.ReadWrite but of no use.It'mostly with C: drive files.
Posted

If the file is in use, then the other process (assuming it isn't your application that is holding it - if you get this with every file, then it may be) has an exclusive lock on the file. The system will not allow you to open the file under these circumstances. The best thing I can suggest is that you add the file to a list of "difficult" files, and try again later. The file may become free in the meantime.
 
Share this answer
 
Comments
Wonde Tadesse 12-Dec-11 19:40pm    
5+
If you have access to the source of the other application you can set it's file open to something like:

FileStream writer = new FileStream("C:\theFile", FileMode.Open, FileAccess.Write, FileShare.Read);


Then you can open the file using:
FileStream reader = new FileStream("C:\theFile", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);


Note that you can always try to open the file in read only mode, if you succeed also depends on how nice the initial user of the file is.

If this doesn't do it for you, you need to sort out who is holding the file. If you know that, you're one step closer to the solution.
you can use Handle[^] to find the application.

Depending on what you find you can take the appropriate action; if it turns out that your own applicaiton already has a handle to the file you might be just have multiple threads doing the same... really the same :).

Cheers, AT
 
Share this answer
 
v2
Comments
RakeshRaushan121 7-Dec-11 9:48am    
i need ur help can u plz contact me on : rakeshraushan121@gmail.com
Addy Tas 15-Dec-11 12:38pm    
Hi,
I did send you a mail a couple of days ago, did you get it or is there an spam filter working overtime?

Cheers, AT
RakeshRaushan121 9-Dec-11 4:31am    
i have tried this also But Still Getting The same Exception
Hi,

You can try this.

C#
FileStream fs = new FileStream(@"<file path>", FileMode.Open, FileAccess.Read, FileShare.Read);


So <file path=""> should be file that you want to access.

then should be able to use "fs" object as you want.

Regards.
 
Share this answer
 
v2
be sure that you have permission on doing that

hope it helps!

have a smile :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900