Click here to Skip to main content
15,883,896 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am creating a file through c# code and want to lock the file (prevent deleting the file) till application closes.

kindly help me with sample code.
Posted

1 solution

When you open the file for read/write, do not use the file.Close() handle to close the file. This will not release the locks and since the file will be in use the OS will not allow any other process to delete the file.

Then in the application close event close the file handle and then it can be deleted as required....
 
Share this answer
 
v2
Comments
fjdiewornncalwe 16-Oct-12 12:00pm    
+5. The easy button.
bbirajdar 17-Oct-12 5:05am    
Thank you Marcus :)
Joan M 17-Oct-12 1:28am    
Good answer. 5ed.
bbirajdar 17-Oct-12 5:05am    
Thank you Joan
Member 7946537 17-Oct-12 5:45am    
Actually we are saving and opening vsd file and then rename it to .rcm file.
during open of the file we are passing the path of the file and we open the vsd file through axdrawingcontrol (visio canvas). we are not using fileinfo or streamreader to open the file.

So in this case file.close() will not applicable.
one option i got that i have to make database connection alive till the file closes..but i think it is not the right way..Kindly let me know if this is the right way or is there anything else possible.

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