简体   繁体   中英

Is there any way to prevent a file from being moved in .NET?

I am loading assemblies dynamically and need to prevent being loaded files from being moved via file move or file cut and paste.

Windows does prevent the loaded assemblies from being deleted. However, I can still move the files to different directories in the same volume.

Do you know of any way to prevent a file from being moved?

Thanks.

You can set permissions to the folders you don't want users to move. you will need to use the System.Security.AccessControl to get the file or folder access control . this post on msdn can help you use it

This may not be the easiest or the correct way to do it, but the first thing that comes to mind is a windows service project I did in college.

It was basically a service that ran, which scanned a directory, and anytime a file was changed it would notify you. This might be some way of catching the event in that directory, and if its one of the assemblies you know you don't want moved just notify the user and don't let it be moved?

Maybe this link will help, although i don't know which event would be thrown by a file being moved, i guess "Changed"?

https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6165137.html

Hope this helps!

Your comment on zaladane's answer actually goes to the heart of the matter:

It [setting access rights] does not prevent the file from being moved by users with equal or higher permission.

If someone, or some process, with higher access rights than you comes along then I don't think there's anything you can do to prevent them being moved. If users need rights to the folder to install new assemblies then they'll have rights to move files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM