当前位置:编程学习 > C#/ASP.NET >>

c# 如何为目录(或文件)设置权限

RT,能有代码更好. --------------------编程问答-------------------- System.IO.File.SetAttributes   --------------------编程问答-------------------- MSDN 帮助文档里就有设置文件属性的代码,在 system.io 中。 --------------------编程问答-------------------- System.IO.DirectoryInfo   DirInfo   =   new   DirectoryInfo(Path);   
  DirInfo.Attributes   =   FileAttributes.Normal   &   FileAttributes.Directory;   
    
  参考System.IO.FileAttributes
--------------------编程问答-------------------- public static void changFileAttributes(string strFileName,FileAttributes fa)
        {
            try
            {
                if (System.IO.File.Exists(strFileName))
                {
                    File.SetAttributes(strFileName, fa);                    
                }
                
            }
            catch
            {
            }
        } --------------------编程问答--------------------
System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(@"d:\");
            dirInfo.Attributes = System.IO.FileAttributes.ReadOnly;//只读
--------------------编程问答-------------------- System.IO.FileAttributes是一个枚举量 --------------------编程问答-------------------- http://msdn.microsoft.com/en-us/library/system.io.fileattributes.aspx
用FileAttributes枚举值:
ReadOnly         The file is read-only.
Hidden           The file is hidden, and thus is not included in an ordinary directory listing.  
System           The file is a system file. The file is part of the operating system or is used exclusively by the operating system. 
Directory        The file is a directory. 
Archive存档文件   The file's archive status. Applications use this attribute to mark files for backup or removal.  




using System;
using System.IO;
using System.Text;

class Test 
{
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        // Create the file if it does not exist.
        if (!File.Exists(path)) 
        {
            File.Create(path);
        }

        if ((File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden) 
        {
            // Show the file.
            File.SetAttributes(path, FileAttributes.Archive);
            Console.WriteLine("The {0} file is no longer hidden.", path);

        } 
        else 
        {
            // Hide the file.
            File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden);
            Console.WriteLine("The {0} file is now hidden.", path);
        }
    }
}
--------------------编程问答--------------------

using System;
using System.IO;
using System.Security.AccessControl;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string fileName = "test.xml";

                Console.WriteLine("Adding access control entry for " 
                    + fileName);

                // Add the access control entry to the file.
                AddFileSecurity(fileName, @"DomainName\AccountName",
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Removing access control entry from " 
                    + fileName);

                // Remove the access control entry from the file.
                RemoveFileSecurity(fileName, @"DomainName\AccountName", 
                    FileSystemRights.ReadData, AccessControlType.Allow);

                Console.WriteLine("Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        // Adds an ACL entry on the specified file for the specified account.
        public static void AddFileSecurity(string fileName, string account, 
            FileSystemRights rights, AccessControlType controlType)
        {
            

            // Get a FileSecurity object that represents the 
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Add the FileSystemAccessRule to the security settings. 
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }

        // Removes an ACL entry on the specified file for the specified account.
        public static void RemoveFileSecurity(string fileName, string account, 
            FileSystemRights rights, AccessControlType controlType)
        {

            // Get a FileSecurity object that represents the 
            // current security settings.
            FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Add the FileSystemAccessRule to the security settings. 
            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,
                rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }
    }
}


--------------------编程问答-------------------- 到帮助中看看System.IO.DirectoryInfo类的详细说明,没有比帮助更详细的说明了
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,