锁定文件以防止删除 [英] lock a file from deleting

查看:57
本文介绍了锁定文件以防止删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过c#代码创建文件,并希望锁定文件(防止删除文件),直到应用程序关闭.

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.

推荐答案

打开文件进行读/写时,请勿使用file.Close()句柄关闭文件.这将不会释放锁,并且由于文件将被使用,因此操作系统将不允许任何其他进程删除该文件.

然后在应用程序关闭事件中关闭文件句柄,然后可以根据需要将其删除....
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....


这篇关于锁定文件以防止删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆