1

I saw this question: Prevent a user from deleting, moving or renaming a file, but it doesn't meet my needs.
If I understood correctly, as long as that stream is opened, the file is locked.

I want to have a list of files in my computer, inside some folder, that cannot be edited, or renamed.
The edit prevention is easy - I just set System.IO.FileAttributes.ReadOnly (Works well)

Now I need to prevent a rename of a files and directories

Community
  • 1
  • 1
user990635
  • 3,979
  • 13
  • 45
  • 66
  • 1
    You could simply use the rights system built into whatever file system you are using. (that's what it's for) – Timothy Groote Jan 20 '14 at 13:10
  • readonly still allows file rename, plus I need to make sure folders cannot be renamed as well – user990635 Jan 20 '14 at 13:17
  • i'm not talking about marking a file as "read only", i'm talking about flat out denying access to a specific user, group or container – Timothy Groote Jan 20 '14 at 13:19
  • possible duplicate of [Prevent a user from Deleting, Moving, or Renaming a file in C#](http://stackoverflow.com/questions/11318663/prevent-a-user-from-deleting-moving-or-renaming-a-file-in-c-sharp) – AeroX Jan 20 '14 at 13:20
  • @TimothyGroote can you provide a code/function/attribute to do it? or at least give me a keyword to search for – user990635 Jan 20 '14 at 13:20
  • @AeroX Did you read something except of the title? – user990635 Jan 20 '14 at 13:21
  • If you're using NTFS, this should do : http://stackoverflow.com/questions/7451861/setting-ntfs-permissions-in-c-net – Timothy Groote Jan 20 '14 at 13:21
  • @user990635 The top voted answer on that question says that holding an open file-stream with the `FileShare.ReadWrite` permission prevents the file being renamed whilst the stream is held open – AeroX Jan 20 '14 at 13:23
  • but I don't want to hold it open. this is what I said. It is just NTFS folder, containing files (many of them) that should not be renamed. I don't want to hold an open stream. I am looking in a direction of attributes or something like that – user990635 Jan 20 '14 at 13:24

1 Answers1

0

My understanding is rename or move are both delete operations at the core. You could try to catch and prevent instead. I'm not sure you can really do what you want to do there doesn't seem to be a flag for it.

http://msdn.microsoft.com/en-us/library/system.io.fileattributes(v=vs.110).aspx

This might help. How to prevent or intercept a call to Directory.Delete(path, true)

Community
  • 1
  • 1
CubanAzcuy
  • 125
  • 1
  • 13
  • JFI: Renaming within one filesystem is a separate operation, not deletion. – Eugene Mayevski 'Callback Feb 13 '14 at 06:12
  • @EugeneMayevski'EldoSCorp Can you expand on this. Do you mean path/folder when you say filesystem? Is that in both NTFS and FAT? I know NTFS is a Journaling System so that make sense but FAT isn't how would it work in that cause. Not disagreeing just want to learn more. – CubanAzcuy Feb 13 '14 at 15:45
  • Filesystem driver receives different requests from the OS to perform various operations. There exist operations such as "Open file", "write to file", "delete file". And also there's a "move file" operation present, where old and new filenames are parameters to the operation. The filenames include paths, but the paths must be within the same filesystem. I.e. if you rename file \PathA\File1 to file \PathB\File2, there's no command to delete File1 - it's renamed/moved and not deleted. – Eugene Mayevski 'Callback Feb 13 '14 at 16:16