.NET 2.x Запрет доступа к файлу - C#

Узнай цену своей работы

Формулировка задачи:

Ребят, подскажите пожалуйста, как программно запретить доступ к файлу? Очень надо. За ответы заранее благодарен.

Решение задачи: «.NET 2.x Запрет доступа к файлу»

textual
Листинг программы
 using System.Security.AccessControl;
    using System.IO;
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //объявляет метод
        public static void AddFileSecurity(string fileName, string account,
          FileSystemRights rights, AccessControlType controlType)
        {
 
            // 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);
 
        }
        //реализуем метод AddFileSecurity;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
 
                string fileName = @"C:\System Volume Information";
 
                //получаем имя компьютора и пользователя
                System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
                string user = wi.Name;
 
                // Add the access control entry to the file.
                AddFileSecurity(fileName, @user,
                    FileSystemRights.FullControl, AccessControlType.Allow);
 
                
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
 
        }
    }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

5   голосов , оценка 4 из 5
Похожие ответы