如何在C#中鎖定文件夾
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
以下代碼可以用來鎖定和解鎖文件夾,基本原理是在鎖定時(shí)將目錄的訪問權(quán)限改為指定人員才有權(quán)限訪問,解鎖時(shí)將權(quán)限設(shè)置為所有用戶都可以訪問。 using System.IO; using System.Security.AccessControl;
private void btnBrowse_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) { // Select the folder to lock textBox1.Text = folderBrowserDialog1.SelectedPath; } }
private void btnLock_Click(object sender, EventArgs e) { try { string folderPath = textBox1.Text; string adminUserName = Environment.UserName;// getting your adminUserName DirectorySecurity ds = Directory.GetAccessControl(folderPath); FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName, FileSystemRights.FullControl, AccessControlType.Deny) ds.AddAccessRule(fsa); Directory.SetAccessControl(folderPath, ds); MessageBox.Show("Locked"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void btnUnLock_Click(object sender, EventArgs e) { try { string folderPath = textBox1.Text; string adminUserName = Environment.UserName;// getting your adminUserName DirectorySecurity ds = Directory.GetAccessControl(folderPath); FileSystemAccessRule fsa = new FileSystemAccessRule(adminUserName,FileSystemRights.FullControl, AccessControlType.Deny) ds.RemoveAccessRule(fsa); Directory.SetAccessControl(folderPath, ds); MessageBox.Show("UnLocked"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } 該文章在 2024/1/9 2:19:49 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |