1.C#代码操作Windows文件夹安全设置.如:添加文件夹的访问用户及访问权限
2.C#代码设置Windows用户及用户组的权限
3.C#代码设置修改IIS匿名访问Windows用户帐户
2.C#代码设置Windows用户及用户组的权限
3.C#代码设置修改IIS匿名访问Windows用户帐户
解决方案 »
- |ZYCWPF| DataGrid添加的DependencyProperty的SelectedEvent无效,且如何在Selected中取得选中项
- bubble的attach问题
- 写了一个关于BackgroundWorker的小程序,错误没有.功能有问题.帮忙看看
- 初涉c#,一段代码,关于母版页的
- 主管交给我一个算法问题,我想了很久也搞不定,求解!
- 怎样查找XML串中指定节点;
- 求VC#.NET数据库开发资料
- 大牛们平时都是再怎么管理自己的个人代码库的
- 求一条SQL语句
- 高分请教:如何在安装项目中生成Uninstall程序?
- 请大家帮我改改这个程序,编译总是不通过,我现在比较糊涂。
- 关于RSA加密解密的问题,高手帮忙看一下下面的代码有什么问题,谢谢!
/// 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 //完全控制权限
}
}