1.C#代码操作Windows文件夹安全设置.如:添加文件夹的访问用户及访问权限
2.C#代码设置Windows用户及用户组的权限
3.C#代码设置修改IIS匿名访问Windows用户帐户

解决方案 »

  1.   

    有一个方法就是用C#来执行外部程序来设置。比如用cacls来设置文件夹权限呀
      

  2.   

    /// <summary>
    /// Windows用户、目录权限操作管理
    /// </summary>
    public class WindowsUserManager
    {
    /// <summary>
    /// 创建Windows用户
    /// </summary>
    /// <param name="username">用户名</param>
    /// <param name="password">密码</param>
    /// <param name="homedir">访问主目录</param>
    public static void CreateLocalUser(string username, string password, string homedir)
    {
    try
    {
    if (!Directory.Exists(homedir))
    Directory.CreateDirectory(homedir);
      
    Process MyProc = new Process();
    MyProc.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
    MyProc.StartInfo.FileName = "net.exe";
    MyProc.StartInfo.UseShellExecute = false;
    MyProc.StartInfo.RedirectStandardError = true;
    MyProc.StartInfo.RedirectStandardInput = true;
    MyProc.StartInfo.RedirectStandardOutput = true;
    MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      
    MyProc.StartInfo.Arguments = @" user " + username + @" " + password + @" /ADD /ACTIVE:YES " +
    @"/EXPIRES:NEVER /FULLNAME:" + username + @" /HOMEDIR:""" + 
    homedir + @""" /PASSWORDCHG:NO /PASSWORDREQ:YES";
      
    MyProc.Start();
    MyProc.WaitForExit();
    MyProc.Close();
    }
    catch
    {
    throw;
    }

    /// <summary>
    /// 添加Windows用户到Windows组
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <param name="groupName">组名</param>
    public static void AddUserToLocalGroup(string userName,string groupName)
    {
    try
    {
    Process MyProc = new Process();
    MyProc.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
    MyProc.StartInfo.FileName = "net.exe";
    MyProc.StartInfo.UseShellExecute = false;
    MyProc.StartInfo.RedirectStandardError = true;
    MyProc.StartInfo.RedirectStandardInput = true;
    MyProc.StartInfo.RedirectStandardOutput = true;
    MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      
    MyProc.StartInfo.Arguments = @" localgroup " + groupName + " " + userName + " /add";
      
    MyProc.Start();
    MyProc.WaitForExit();
    MyProc.Close();
    }
    catch
    {
    throw;
    }
    }
    /// <summary>
    /// 从Windows组中删除用户
    /// </summary>
    /// <param name="userName">用户名</param>
    /// <param name="groupName">组名</param>
    public static void DelUserFromLocalGroup(string userName,string groupName)
    {
    try
    {
    Process MyProc = new Process();
    MyProc.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
    MyProc.StartInfo.FileName = "net.exe";
    MyProc.StartInfo.UseShellExecute = false;
    MyProc.StartInfo.RedirectStandardError = true;
    MyProc.StartInfo.RedirectStandardInput = true;
    MyProc.StartInfo.RedirectStandardOutput = true;
    MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      
    MyProc.StartInfo.Arguments = @" localgroup " + groupName + " " + userName + " /delete";
      
    MyProc.Start();
    MyProc.WaitForExit();
    MyProc.Close();
    }
    catch
    {
    throw;
    }
    }
    /// <summary>
    /// 添加对目录的访问用户或组
    /// </summary>
    /// <param name="userName">用户或组名</param>
    /// <param name="sDirectory">访问目录</param>
    /// <param name="access">访问权限</param>
    public static void AddUserToDirectory(string userName,string sDirectory,DirectoryAccess access)
    {
    try
    {
    Process MyProc = new Process();
    MyProc.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
    MyProc.StartInfo.FileName = "cacls.exe";
    MyProc.StartInfo.UseShellExecute = false;
    MyProc.StartInfo.RedirectStandardError = true;
    MyProc.StartInfo.RedirectStandardInput = true;
    MyProc.StartInfo.RedirectStandardOutput = true;
    MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      
    MyProc.StartInfo.Arguments = " " + sDirectory + " /T /E /C /G " + userName + ":" + access.ToString();
      
    MyProc.Start();
    MyProc.WaitForExit();
    MyProc.Close();
    }
    catch
    {
    throw;
    }
    }
    /// <summary>
    /// 取消用户或组对目录的访问权限
    /// </summary>
    /// <param name="userName">用户或组名</param>
    /// <param name="sDirectory">访问目录</param>
    public static void CancelUserFromDirectory(string userName,string sDirectory)
    {
    try
    {
    Process MyProc = new Process();
    MyProc.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
    MyProc.StartInfo.FileName = "cacls.exe";
    MyProc.StartInfo.UseShellExecute = false;
    MyProc.StartInfo.RedirectStandardError = true;
    MyProc.StartInfo.RedirectStandardInput = true;
    MyProc.StartInfo.RedirectStandardOutput = true;
    MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      
    MyProc.StartInfo.Arguments = " " + sDirectory + " /R " + userName;
      
    MyProc.Start();
    MyProc.WaitForExit();
    MyProc.Close();
    }
    catch
    {
    throw;
    }
    }
    /// <summary>
    /// 拒绝用户或组对目录的访问权限
    /// </summary>
    /// <param name="userName">用户或组名</param>
    /// <param name="sDirectory">访问目录</param>
    public static void RejustUserAccessToDirectory(string userName,string sDirectory)
    {
    try
    {
    Process MyProc = new Process();
    MyProc.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
    MyProc.StartInfo.FileName = "cacls.exe";
    MyProc.StartInfo.UseShellExecute = false;
    MyProc.StartInfo.RedirectStandardError = true;
    MyProc.StartInfo.RedirectStandardInput = true;
    MyProc.StartInfo.RedirectStandardOutput = true;
    MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      
    MyProc.StartInfo.Arguments = " " + sDirectory + " /D " + userName;
      
    MyProc.Start();
    MyProc.WaitForExit();
    MyProc.Close();
    }
    catch
    {
    throw;
    }
    }
    /// <summary>
    /// 替换用户或组对目录的访问权限
    /// </summary>
    /// <param name="userName">用户或组名</param>
    /// <param name="sDirectory">访问目录</param>
    /// <param name="access">访问权限</param>
    public static void ReplaceUserAccessToDirectory(string userName,string sDirectory,DirectoryAccess access)
    {
    try
    {
    Process MyProc = new Process();
    MyProc.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
    MyProc.StartInfo.FileName = "cacls.exe";
    MyProc.StartInfo.UseShellExecute = false;
    MyProc.StartInfo.RedirectStandardError = true;
    MyProc.StartInfo.RedirectStandardInput = true;
    MyProc.StartInfo.RedirectStandardOutput = true;
    MyProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
      
    MyProc.StartInfo.Arguments = " " + sDirectory + " /T /E /C /P " + userName + ":" + access.ToString();
      
    MyProc.Start();
    MyProc.WaitForExit();
    MyProc.Close();
    }
    catch
    {
    throw;
    }
    }
    /// <summary>
    /// 目录访问权限
    /// </summary>
    public enum DirectoryAccess
    {
    N, //无权限
    R, //读取权限
    W, //写入权限
    C, //修改(写入)权限
    F //完全控制权限
    }
    }