c# - 以编程方式更改某些 Windows 用户名(重命名 Windows 用户)

标签 c# .net windows windows-7

如何以编程方式更改 Windows 用户名(使用一些 API 或命令行工具) 或者如何重命名 windows 用户?

最佳答案

我已经编写了一些使用 System.DirectoryServices.DirectoryEntry 类重命名 Windows 用户的小方法。

public bool RenameUser(string oldLoginName, string newLoginName)
{
    bool renamed = false;
    try
    {
        using (DirectoryEntry AD = new
                   DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"))
        {
            try
            {
                using (DirectoryEntry NewUser = AD.Children.Find(oldLoginName, "user"))
                {
                    if (NewUser != null)
                    {
                        NewUser.Rename(newLoginName);
                        renamed = true;
                    }
                }
            }
            catch (Exception ex)
            {
                //TODO: Log
            }
        }
    }
    catch (Exception ex)
    {
         //TODO: Log
    }
    return renamed;
}

关于c# - 以编程方式更改某些 Windows 用户名(重命名 Windows 用户),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7056888/

相关文章:

c# - FluentValidation、Validators 生命周期和使用 Validators 检查 Db

c# - 如何创建同一个 xaml 文件的多个部分类

c# - 如何在 C# 3.5 的父类(super class)中定义强制转换运算符?

Http Post 请求消息正文中的 C# Xml

c# - 图像与位图类

c# - 如何避免在每个 Controller 中重复一个参数?

windows - VIM "!"和 Windows 启动命令

windows - 如何取消延迟的 MoveFileEx 操作?

xml - GhostScript 在 ImageMagick 中的文件路径

c# - 签署 ILMerge 的 DLL