C# 设置文件夹访问权限
C# 设置文件夹访问权限
Applying permissions on folders/directories in Windows is one of the things developers want to control with their applications. Today we are going to look at how can we create such an application in C# which can define any kind of permission to any user account in Windows environment.
Start Visual Studio and create a new C# Windows application. Name it DirectoryPermission and Create an interface which looks similar to the one below:
1. Change the text of your window to 男生名字大全Folder Permission or any other you like. 
2. Drag two labels, two buttons, 1 textbox and 1 combo box on the form. Name the buttons as SelectDirectorybtn andPermissionbtn and leave the names of others as default. 
3. Now we need to add two references, for that right-click your project root and select Add reference.
 淘宝保证金怎么解冻
4. From the Add Reference Menu, add following two highlighted references.
Both of these references actually help us interact with Operating System’s account management and queries.
5. Right click on your Windows Form and Select View Code. 
6. First of all we will define the references that we added in Step 4. We’ll also add Syste
m.IO andSystem.Security.AccessControl
  1:  using System.IO;
  2:  using System.Security.AccessControl;
  3:  using System.Management;
  4:  using System.Management.Instrumentation;
十大劳动模范人物事迹简介
离骚 名句 
7. Now we are going to define a method which will fill our combobox with names of all user account.
  1:  public void GetUsers()
  2:  {
  3:          // This query will query for all user account names in our current Domain
  4:          SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='"
                            + System.Environment.UserDomainName.ToString() + "'");
  5:   
  6:          try
  7:          {
  8:                    // Searching for available Users
  9:                      ManagementObjectSearcher mSearcher = new
                                                        ManagementObjectSearcher(sQuery);
  10:   
  11:                      foreach (ManagementObject mObject in mSearcher.Get())
  12:                      {
  13:                              // Adding all user names in our combobox
  14:                              comboBox1.Items.Add(mObject["Name"]);
  15:                      }
  16:          }
  17:          catch (Exception ex)
  18:          {
  19:                      MessageBox.Show(ex.ToString());
  20:          }
撒旦是谁  21:  }
Note: System.Environment.UserDomainName is going to deliver us our current domain.
 
8. In order to make the above method effective, we need to modify our form constructor.
  1:  public Form1()
  2:  {
  3:              InitializeComponent();
  4:              GetUsers();
  5:  }
 
9. Now we’ll go back to our design window and double click Select Directory button to define its clicking event. Change the definition to following;
  1:  private void SelectDirectorybtn_Click(object sender, EventArgs e)
  2:  {
  3:            // creating a new instance fot FolderBrowsingDialog
                                    to provide user capability to select target Folder
  4:            FolderBrowserDialog myFolderBrowserDialog = new FolderBrowserDialog();
  5:   
  6:            // showing dialog
  7:            myFolderBrowserDialog.ShowDialog();
  8:   
  9:            // Show the path of selected directory in our text Box
  10:            textBox1.Text myFolderBrowserDialog.SelectedPath.ToString();
  11:  }
 
10. Move back to your design view and add a Click event to the second  Make Unreadable, double click it to modify the event.
  1:  private void Permissionbtn_Click(object sender, EventArgs e)
  2:  {
  3:            // retrieving the directory information
  4:            DirectoryInfo myDirectoryInfo = new DirectoryInfo(textBox1.Text);
  5:   
  6:            // Get a DirectorySecurity object that represents the
  7:            // current security settings.
伪装者剧情  8:            DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
  9:            string User = System.Environment.UserDomainName + "\\" +
                                                          comboBox1.SelectedItem.ToString();
  10:   
  11:            // Add the FileSystemAccessRule to the security settings.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。