欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限

 更新時(shí)間:2024年03月12日 11:13:52   作者:無熵~  
這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)讀取和設(shè)置文件與文件夾的權(quán)限,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

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)文章

  • 如何使用C#從word文檔中提取圖片

    如何使用C#從word文檔中提取圖片

    圖片和文字是word文檔中兩種最常見的對(duì)象,在微軟word中,如果我們想要提取出一個(gè)文檔內(nèi)的圖片,只需要右擊圖片選擇另存為然后命名保存就可以了,今天這篇文章主要是實(shí)現(xiàn)如何使用C#從word文檔中提取圖片,需要的朋友參考下
    2016-02-02
  • Unity 2017使用UGUI實(shí)現(xiàn)大轉(zhuǎn)盤抽獎(jiǎng)

    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#實(shí)現(xiàn)在Word中更改字體顏色

    利用C#實(shí)現(xiàn)在Word中更改字體顏色

    在日常工作中,我們有時(shí)會(huì)需要修改字體的顏色來突出文本重點(diǎn),讓讀者更容易抓住文章要點(diǎn)。在今天這篇文章中,我將為大家介紹如何以編程方式,在Word更改字體顏色,感興趣的可以了解一下
    2023-02-02
  • c# 繪制中國象棋棋盤與棋子

    c# 繪制中國象棋棋盤與棋子

    這篇文章主要介紹了c# 繪制中國象棋棋盤與棋子,文中實(shí)例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • C#匹配中文字符串的4種正則表達(dá)式分享

    C#匹配中文字符串的4種正則表達(dá)式分享

    這篇文章主要介紹了C#匹配中文字符串的4種正則表達(dá)式分享,本文介紹了4種形式下的中文字符串匹配正則,需要的朋友可以參考下
    2014-08-08
  • 利用C#編寫一個(gè)Windows服務(wù)程序的方法詳解

    利用C#編寫一個(gè)Windows服務(wù)程序的方法詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C#編寫一個(gè)Windows服務(wù)程序,文中的實(shí)現(xiàn)方法講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-03-03
  • C# WebApi 接口返回值不困惑:返回值類型詳解

    C# WebApi 接口返回值不困惑:返回值類型詳解

    這篇文章主要介紹了C# WebApi 接口返回值不困惑:返回值類型詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • C#中的虛函數(shù)virtual

    C#中的虛函數(shù)virtual

    這篇文章介紹了C#中的虛函數(shù)virtual,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • 在C#中根據(jù)HardwareID獲取驅(qū)動(dòng)程序信息的實(shí)現(xiàn)代碼

    在C#中根據(jù)HardwareID獲取驅(qū)動(dòng)程序信息的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C#中根據(jù)HardwareID獲取驅(qū)動(dòng)程序信息的實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2016-12-12
  • c#裝箱和拆箱知識(shí)整理

    c#裝箱和拆箱知識(shí)整理

    這篇文章主要介紹了c#裝箱和拆箱知識(shí),裝箱和拆箱是一個(gè)抽象的概念,需要的朋友可以參考下
    2014-03-03

最新評(píng)論