C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限
C#程序運(yùn)行時(shí)經(jīng)常遇到文件或文件夾權(quán)限問題,導(dǎo)致程序運(yùn)行失敗。為了解決這個(gè)頭疼的問題,我們通常要讀取和設(shè)置文件、文件夾權(quán)限。
讀取文件、文件夾權(quán)限
/// <summary>
/// 讀取文件、文件夾權(quán)限
/// </summary>
/// <param name="path"></param>
private void ReadPathRule(string path)
{
DirectoryInfo di = new DirectoryInfo(path);
DirectorySecurity ds = di.GetAccessControl(AccessControlSections.All);
foreach (FileSystemAccessRule rule in ds.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount)))
{
Console.WriteLine("----------------------------------");
Console.WriteLine(rule.IdentityReference.Value);
if ((rule.FileSystemRights & FileSystemRights.Read) != 0)
Console.WriteLine(rule.FileSystemRights.ToString());
}
}
設(shè)置文件權(quán)限
/// <summary>
/// 為文件添加users,everyone用戶組的完全控制權(quán)限
/// </summary>
/// <param name="filePath"></param>
static void AddSecurityControll2File(string filePath)
{
//獲取文件信息
FileInfo fileInfo = new FileInfo(filePath);
//獲得該文件的訪問權(quán)限
System.Security.AccessControl.FileSecurity fileSecurity = fileInfo.GetAccessControl();
//添加ereryone用戶組的訪問權(quán)限規(guī)則 完全控制權(quán)限
fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
//添加Users用戶組的訪問權(quán)限規(guī)則 完全控制權(quán)限
fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
//設(shè)置訪問權(quán)限
fileInfo.SetAccessControl(fileSecurity);
}
設(shè)置文件夾權(quán)限
/// <summary>
///為文件夾添加users,everyone用戶組的完全控制權(quán)限
/// </summary>
/// <param name="dirPath"></param>
static void AddSecurityControll2Folder(string dirPath)
{
//獲取文件夾信息
DirectoryInfo dir = new DirectoryInfo(dirPath);
//獲得該文件夾的所有訪問權(quán)限
System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All);
//設(shè)定文件ACL繼承
InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
//添加ereryone用戶組的訪問權(quán)限規(guī)則 完全控制權(quán)限
FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
//添加Users用戶組的訪問權(quán)限規(guī)則 完全控制權(quán)限
FileSystemAccessRule usersFileSystemAccessRule = new FileSystemAccessRule("Users", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
bool isModified = false;
dirSecurity.ModifyAccessRule(AccessControlModification.Add, everyoneFileSystemAccessRule, out isModified);
dirSecurity.ModifyAccessRule(AccessControlModification.Add, usersFileSystemAccessRule, out isModified);
//設(shè)置訪問權(quán)限
dir.SetAccessControl(dirSecurity);
}
注意:修改權(quán)限有時(shí)候需要程序有管理員權(quán)限,怎么以管理員身份運(yùn)行程序,請(qǐng)參考:
C#/WPF 以管理員身份運(yùn)行程序
在Vista 和 Windows 7 及更新版本的操作系統(tǒng),增加了 UAC(用戶賬戶控制) 的安全機(jī)制,如果 UAC 被打開,用戶即使以管理員權(quán)限登錄,其應(yīng)用程序默認(rèn)情況下也無法對(duì)系統(tǒng)目錄、系統(tǒng)注冊(cè)表等可能影響系統(tǒng)正常運(yùn)行的設(shè)置進(jìn)行寫操作。這個(gè)機(jī)制大大增強(qiáng)了系統(tǒng)的安全性,但對(duì)應(yīng)用程序開發(fā)者來說,我們不能強(qiáng)迫用戶去關(guān)閉UAC,但有時(shí)我們開發(fā)的應(yīng)用程序又需要以Administrator 的方式運(yùn)行。
解決辦法有以下幾種方式:
通過 System.Diagnostics.Process.Start() 方式啟動(dòng)(推薦)
通過添加應(yīng)用程序清單文件(過于暴力,且有提示)
直接修改程序文件的屬性(不方便部署)
方法一:通過 System.Diagnostics.Process.Start() 方式啟動(dòng):
WPF App.xaml代碼:
<Application x:Class="AdminLoadDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:AdminLoadDemo"
Startup="Application_Startup">
<Application.Resources>
</Application.Resources>
</Application>
WPF App.xaml.cs代碼:
private void Application_Startup(object sender, StartupEventArgs e)
{
/**
* 當(dāng)前用戶是管理員的時(shí)候,直接啟動(dòng)應(yīng)用程序
* 如果不是管理員,則使用啟動(dòng)對(duì)象啟動(dòng)程序,以確保使用管理員身份運(yùn)行
*/
//獲得當(dāng)前登錄的Windows用戶標(biāo)示
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判斷當(dāng)前登錄用戶是否為管理員
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
{
//如果是管理員,則直接運(yùn)行
new MainWindow().Show();
}
else
{
//創(chuàng)建啟動(dòng)對(duì)象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.WorkingDirectory = Environment.CurrentDirectory;
startInfo.FileName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//設(shè)置啟動(dòng)動(dòng)作,確保以管理員身份運(yùn)行
startInfo.Verb = "runas";
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return;
}
//退出
Environment.Exit(0);
}
到此這篇關(guān)于C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限的文章就介紹到這了,更多相關(guān)C#文件權(quán)限內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Unity 2017使用UGUI實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)
這篇文章主要為大家詳細(xì)介紹了Unity 2017使用UGUI實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
利用C#編寫一個(gè)Windows服務(wù)程序的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何利用C#編寫一個(gè)Windows服務(wù)程序,文中的實(shí)現(xiàn)方法講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下2023-03-03
在C#中根據(jù)HardwareID獲取驅(qū)動(dòng)程序信息的實(shí)現(xiàn)代碼
這篇文章主要介紹了C#中根據(jù)HardwareID獲取驅(qū)動(dòng)程序信息的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-12-12

