C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限
C#程序運(yùn)行時(shí)經(jīng)常遇到文件或文件夾權(quán)限問(wèn)題,導(dǎo)致程序運(yùn)行失敗。為了解決這個(gè)頭疼的問(wèn)題,我們通常要讀取和設(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用戶(hù)組的完全控制權(quán)限
/// </summary>
/// <param name="filePath"></param>
static void AddSecurityControll2File(string filePath)
{
//獲取文件信息
FileInfo fileInfo = new FileInfo(filePath);
//獲得該文件的訪問(wèn)權(quán)限
System.Security.AccessControl.FileSecurity fileSecurity = fileInfo.GetAccessControl();
//添加ereryone用戶(hù)組的訪問(wèn)權(quán)限規(guī)則 完全控制權(quán)限
fileSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
//添加Users用戶(hù)組的訪問(wèn)權(quán)限規(guī)則 完全控制權(quán)限
fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
//設(shè)置訪問(wèn)權(quán)限
fileInfo.SetAccessControl(fileSecurity);
}
設(shè)置文件夾權(quán)限
/// <summary>
///為文件夾添加users,everyone用戶(hù)組的完全控制權(quán)限
/// </summary>
/// <param name="dirPath"></param>
static void AddSecurityControll2Folder(string dirPath)
{
//獲取文件夾信息
DirectoryInfo dir = new DirectoryInfo(dirPath);
//獲得該文件夾的所有訪問(wèn)權(quán)限
System.Security.AccessControl.DirectorySecurity dirSecurity = dir.GetAccessControl(AccessControlSections.All);
//設(shè)定文件ACL繼承
InheritanceFlags inherits = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
//添加ereryone用戶(hù)組的訪問(wèn)權(quán)限規(guī)則 完全控制權(quán)限
FileSystemAccessRule everyoneFileSystemAccessRule = new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, inherits, PropagationFlags.None, AccessControlType.Allow);
//添加Users用戶(hù)組的訪問(wèn)權(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è)置訪問(wèn)權(quán)限
dir.SetAccessControl(dirSecurity);
}
注意:修改權(quán)限有時(shí)候需要程序有管理員權(quán)限,怎么以管理員身份運(yùn)行程序,請(qǐng)參考:
C#/WPF 以管理員身份運(yùn)行程序
在Vista 和 Windows 7 及更新版本的操作系統(tǒng),增加了 UAC(用戶(hù)賬戶(hù)控制) 的安全機(jī)制,如果 UAC 被打開(kāi),用戶(hù)即使以管理員權(quán)限登錄,其應(yīng)用程序默認(rèn)情況下也無(wú)法對(duì)系統(tǒng)目錄、系統(tǒng)注冊(cè)表等可能影響系統(tǒng)正常運(yùn)行的設(shè)置進(jìn)行寫(xiě)操作。這個(gè)機(jī)制大大增強(qiáng)了系統(tǒng)的安全性,但對(duì)應(yīng)用程序開(kāi)發(fā)者來(lái)說(shuō),我們不能強(qiáng)迫用戶(hù)去關(guān)閉UAC,但有時(shí)我們開(kāi)發(fā)的應(yīng)用程序又需要以Administrator 的方式運(yùn)行。
解決辦法有以下幾種方式:
通過(guò) System.Diagnostics.Process.Start() 方式啟動(dòng)(推薦)
通過(guò)添加應(yīng)用程序清單文件(過(guò)于暴力,且有提示)
直接修改程序文件的屬性(不方便部署)
方法一:通過(guò) 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)前用戶(hù)是管理員的時(shí)候,直接啟動(dòng)應(yīng)用程序
* 如果不是管理員,則使用啟動(dòng)對(duì)象啟動(dòng)程序,以確保使用管理員身份運(yùn)行
*/
//獲得當(dāng)前登錄的Windows用戶(hù)標(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)前登錄用戶(hù)是否為管理員
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)文章
c#中Invoke與BeginInvoke的用法及說(shuō)明
這篇文章主要介紹了c#中Invoke與BeginInvoke的用法及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
基于WebClient實(shí)現(xiàn)Http協(xié)議的Post與Get對(duì)網(wǎng)站進(jìn)行模擬登陸和瀏覽實(shí)例
這篇文章主要介紹了基于WebClient實(shí)現(xiàn)Http協(xié)議的Post與Get對(duì)網(wǎng)站進(jìn)行模擬登陸和瀏覽的方法,以實(shí)例形式詳細(xì)分析了WebClient模擬POST與GET登陸與瀏覽的過(guò)程,對(duì)于C#項(xiàng)目開(kāi)發(fā)來(lái)說(shuō)具有不錯(cuò)的參考借鑒價(jià)值,需要的朋友可以參考下2014-11-11
C#中實(shí)現(xiàn)可變參數(shù)實(shí)例
這篇文章主要介紹了C#中實(shí)現(xiàn)可變參數(shù)實(shí)例,本文演示使用params 實(shí)現(xiàn)可變數(shù)量的參數(shù),并且這些參數(shù)的類(lèi)型可以不同,需要的朋友可以參考下2015-01-01
使用C#代碼在Word文檔中設(shè)置段落縮進(jìn)
本文介紹了如何通過(guò)編程方式在Word文檔中設(shè)置段落縮進(jìn),主要左縮進(jìn)、右縮進(jìn)、首行縮進(jìn)和懸掛縮進(jìn)等內(nèi)容,詳細(xì)解釋了設(shè)置段落縮進(jìn)的具體步驟和代碼實(shí)現(xiàn),提供了示例代碼幫助讀者更好地理解和實(shí)現(xiàn),需要的朋友可以參考下2026-04-04
WPF輕松實(shí)現(xiàn)進(jìn)度條的示例代碼
WPF中的ProgressBar控件用于表示任務(wù)進(jìn)度,適用于文件下載、數(shù)據(jù)處理等場(chǎng)景,本文將通過(guò)XAML和C#代碼展示如何創(chuàng)建一個(gè)基本的WPF進(jìn)度條,并演示如何通過(guò)事件處理程序更新進(jìn)度條的值來(lái)模擬耗時(shí)操作,感興趣的小伙伴跟著小編一起來(lái)看看吧2024-12-12
C#?原生編碼智能體運(yùn)行時(shí)?SharpClawCode詳解
SharpClawCode是一個(gè)專(zhuān)為 .NET 10 和 C# 13 生態(tài)系統(tǒng)設(shè)計(jì)的C# 原生編碼智能體運(yùn)行時(shí)(coding-agent runtime),本文給大家介紹C#原生編碼智能體運(yùn)行時(shí)?SharpClawCode的相關(guān)知識(shí),感興趣的朋友一起看看吧2026-04-04
C#實(shí)現(xiàn)寫(xiě)入與讀出文本文件的實(shí)例代碼
本篇文章是對(duì)使用C#實(shí)現(xiàn)寫(xiě)入與讀出文本文件的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

