C# 设置文件夹访问权限
2012-03-27 21:59
323 查看
Applyingpermissionsonfolders/directoriesinWindowsisoneofthethingsdeveloperswanttocontrolwiththeirapplications.TodaywearegoingtolookathowcanwecreatesuchanapplicationinC#whichcandefineanykindof
permissiontoanyuseraccountinWindowsenvironment.
StartVisualStudioandcreateanewC#Windowsapplication.NameitDirectoryPermissionandCreateaninterfacewhichlookssimilartotheonebelow:
1.ChangethetextofyourwindowtoFolderPermissionoranyotheryoulike.
2.Dragtwolabels,twobuttons,1textboxand1comboboxontheform.NamethebuttonsasSelectDirectorybtnandPermissionbtnandleavethenamesofothersasdefault.
3.Nowweneedtoaddtworeferences,forthatright-clickyourprojectrootandselectAddreference.
4.FromtheAddReferenceMenu,addfollowingtwohighlightedreferences.
BothofthesereferencesactuallyhelpusinteractwithOperatingSystem’saccountmanagementandqueries.
5.RightclickonyourWindowsFormandSelectViewCode.
6.FirstofallwewilldefinethereferencesthatweaddedinStep4.We’llalsoaddSystem.IOandSystem.Security.AccessControl
7.Nowwearegoingtodefineamethodwhichwillfillourcomboboxwithnamesofalluseraccount.
Note:System.Environment.UserDomainNameisgoingtodeliverusourcurrentdomain.
8.Inordertomaketheabovemethodeffective,weneedtomodifyourformconstructor.
9.Nowwe’llgobacktoourdesignwindowanddoubleclickSelectDirectorybuttontodefineitsclickingevent.Changethedefinitiontofollowing;
10.MovebacktoyourdesignviewandaddaClickeventtothesecondbuttoni.e.MakeUnreadable,doubleclickittomodifytheevent.
That’sit.Compileandexecutetheapplicationtotestresults.Ifyouhaveanyqueriesfeelfreetoask…Cheers!
permissiontoanyuseraccountinWindowsenvironment.
StartVisualStudioandcreateanewC#Windowsapplication.NameitDirectoryPermissionandCreateaninterfacewhichlookssimilartotheonebelow:
1.ChangethetextofyourwindowtoFolderPermissionoranyotheryoulike.
2.Dragtwolabels,twobuttons,1textboxand1comboboxontheform.NamethebuttonsasSelectDirectorybtnandPermissionbtnandleavethenamesofothersasdefault.
3.Nowweneedtoaddtworeferences,forthatright-clickyourprojectrootandselectAddreference.
4.FromtheAddReferenceMenu,addfollowingtwohighlightedreferences.
BothofthesereferencesactuallyhelpusinteractwithOperatingSystem’saccountmanagementandqueries.
5.RightclickonyourWindowsFormandSelectViewCode.
6.FirstofallwewilldefinethereferencesthatweaddedinStep4.We’llalsoaddSystem.IOandSystem.Security.AccessControl
1:usingSystem.IO;
2:usingSystem.Security.AccessControl;
3:usingSystem.Management;
4:usingSystem.Management.Instrumentation;
7.Nowwearegoingtodefineamethodwhichwillfillourcomboboxwithnamesofalluseraccount.
1:publicvoidGetUsers()
2:{
3://ThisquerywillqueryforalluseraccountnamesinourcurrentDomain
4:SelectQuerysQuery=newSelectQuery("Win32_UserAccount","Domain='"+System.Environment.UserDomainName.ToString()+"'");
5:
6:try
7:{
8://SearchingforavailableUsers
9:ManagementObjectSearchermSearcher=newManagementObjectSearcher(sQuery);
10:
11:foreach(ManagementObjectmObjectinmSearcher.Get())
12:{
13://Addingallusernamesinourcombobox
14:comboBox1.Items.Add(mObject["Name"]);
15:}
16:}
17:catch(Exceptionex)
18:{
19:MessageBox.Show(ex.ToString());
20:}
21:}
Note:System.Environment.UserDomainNameisgoingtodeliverusourcurrent
8.Inordertomaketheabovemethodeffective,weneedtomodifyourformconstructor.
1:publicForm1()
2:{
3:InitializeComponent();
4:GetUsers();
5:}
9.Nowwe’llgobacktoourdesignwindowanddoubleclickSelectDirectorybuttontodefineitsclickingevent.Changethedefinitiontofollowing;
1:privatevoidSelectDirectorybtn_Click(objectsender,EventArgse)
2:{
3://creatinganewinstancefotFolderBrowsingDialogtoprovideusercapabilitytoselecttargetFolder
4:FolderBrowserDialogmyFolderBrowserDialog=newFolderBrowserDialog();
5:
6://showingdialog
7:myFolderBrowserDialog.ShowDialog();
8:
9://ShowthepathofselecteddirectoryinourtextBox
10:textBox1.TextmyFolderBrowserDialog.SelectedPath.ToString();
11:}
10.MovebacktoyourdesignviewandaddaClickeventtothesecondbuttoni.e.MakeUnreadable,doubleclickittomodifytheevent.
1:privatevoidPermissionbtn_Click(objectsender,EventArgse)
2:{
3://retrievingthedirectoryinformation
4:DirectoryInfomyDirectoryInfo=newDirectoryInfo(textBox1.Text);
5:
6://GetaDirectorySecurityobjectthatrepresentsthe
7://currentsecuritysettings.
8:DirectorySecuritymyDirectorySecurity=myDirectoryInfo.GetAccessControl();
9:stringUser=System.Environment.UserDomainName+"\\"+comboBox1.SelectedItem.ToString();
10:
11://AddtheFileSystemAccessRuletothesecuritysettings.
//FileSystemRightsisabiglistwearecurrentusingReadpropertybutyou
//canalteranyotherormanysmeofwhichare:
12://CreateDirectories:forsubdirectoriesAuthority
13://CreateFiles:forfilescreationaccessinaparticularfolder
14://Delete:fordeletionathorityonfolder
15://DeleteSubdirectoriesandfiles:forauthorityofdeletionover
//subdirectoriesandfiles
16://Executefile:Forexecutionaccessibilityinfolder
17://Modify:Forfoldermodification
18://Read:Fordirectoryopening
19://Write:toaddthingsindirectory
20://FullControl:Foradministrationrightsetcetc
21:
22://AlsoAccessControlTypewhichareoftwokindseither“Allow”or“Deny”
23:myDirectorySecurity.AddAccessRule(newFileSystemAccessRule(User,FileSystemRights.Read,AccessControlType.Deny));
24:
25://Setthenewaccesssettings.
26:myDirectoryInfo.SetAccessControl(myDirectorySecurity);
27:
28://ShowingaSuccesfullyDoneMessage
29:MessageBox.Show("PermissionsAlteredSuccessfully");
30:}
That’sit.Compileandexecutetheapplicationtotestresults.Ifyouhaveanyqueriesfeelfreetoask…Cheers!
相关文章推荐
- C# 设置文件夹的访问权限
- C#_设置文件夹访问权限
- 如何快速设置Windows XP系统中某个文件夹对某用户的访问权限
- 配置共享文件夹--不同文件夹设置不同访问权限
- 为AD安全组或账户设置Sharepoint库文件夹访问权限 推荐
- apache环境下禁止某文件夹内运行PHP脚本、禁止访问文件或目录执行权限的设置方法
- linux设置其他用户可以访问本用户下的文件夹的权限
- C#创建文件夹并设置权限
- C#设置文件夹权限
- 转载:C# 设置文件夹权限(代码简单)
- mkdir中设置文件夹访问权限不起作用
- C#创建文件夹并设置权限
- XP NTFS分区 中文件夹访问权限的设置
- 在Win XP及Win 2003下使用程序方式(C#)设置共享文件夹的文件夹权限的问题及解决方案。
- C#设置文件夹用户权限
- 在Win XP及Win 2003下使用程序方式(C#)设置共享文件夹的文件夹权限的问题及解决方案
- C#添加文件和文件夹访问用户并设定权限
- 共享文件夹的权限设置和远程访问
- [原]C#设置文件夹用户权限
- C#设置文件夹用户权限