Click here to Skip to main content
Rate this: bad
good
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
good
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....
  Permalink  
v2
Comments
Marcus Kramer at 16-Oct-12 12:00pm
   
+5. The easy button.
aspnet_regiis -i at 17-Oct-12 5:05am
   
Thank you Marcus :)
Joan Murt at 17-Oct-12 1:28am
   
Good answer. 5ed.
aspnet_regiis -i at 17-Oct-12 5:05am
   
Thank you Joan
Member 7946537 at 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
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 Zoltán Zörgő 99
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 16 Oct 2012
Copyright © CodeProject, 1999-2014
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