I want to set permissions for a file to "cannot be deleted" in C #, read-only. But I do not know how to do this. Can you help me?
See File.SetAttributes () . There are many examples online about how to use it.
Taken from this MSDN page:
FileAttributes attributes = File.GetAttributes(path); if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) { // Show the file. attributes = RemoveAttribute(attributes, FileAttributes.Hidden); File.SetAttributes(path, attributes); Console.WriteLine("The {0} file is no longer hidden.", path); } else { // Hide the file. File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden); Console.WriteLine("The {0} file is now hidden.", path); }
You forgot to copy in the RemoveAttribute method, which:
private static FileAttributes RemoveAttribute(FileAttributes attributes, FileAttributes attributesToRemove) { return attributes & ~attributesToRemove; }
Is it about attributes (see jb. Answer) or permissions, i.e. read and write access, etc.? In the latter case, see File.SetAccessControl .
From MSDN:
// Get a FileSecurity object that represents the // current security settings. FileSecurity fSecurity = File.GetAccessControl(fileName); // Add the FileSystemAccessRule to the security settings. fSecurity.AddAccessRule(new FileSystemAccessRule(account, rights, controlType)); // Set the new access settings. File.SetAccessControl(fileName, fSecurity);
See How to grant full permission to a file created by my application for ALL users? for a more specific example.
In the original question, it sounds like you want to deny the FileSystemRights.Delete right.
FileSystemRights.Delete
Source: https://habr.com/ru/post/921948/More articles:Sending data from the ganglia to graphite - graphiteHow to work with NopCommerce MVC as a team - asp.net-mvcHow to make a read-only file? - c #Setting NTFS permissions in C # .NET - c #How to save / cache data in a cross-platform Mono application (e.g. Xamarin Framework)? - cachinggridView.DataSource as a DataTable sets to null in asp.net - c #Frequency of polling on the server from the server - androidSee Action Bar in all actions - Android - androidRunning code again and again - javaI donβt understand why DATEADD does not increase time - datetimeAll Articles