Click here to Skip to main content
11,929,249 members (65,735 online)
Rate this:
Please Sign up or sign in to vote.
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 16-Oct-12 4:32am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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....
Marcus Kramer 16-Oct-12 12:00pm
+5. The easy button.
aspnet_regiis -i 17-Oct-12 5:05am
Thank you Marcus :)
Joan Murt 17-Oct-12 1:28am
Good answer. 5ed.
aspnet_regiis -i 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.151126.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100