错误:尝试删除本地用户时,请求了未知的目录对象

时间:2014-05-02 02:58:51

标签: c# winforms windows-8 windows-7 directory

我正在为我父亲编写一个程序用于他的工作。他基本上想要一个能够通过并删除本地用户配置文件的程序,因为当每天有20个人登录计算机时,这是一项相当繁琐的任务。无论如何,我有这个找到所有用户并删除他们的本地配置文件:

//get a list of usernames that we don't want to get rid of
List<string> usernames = new List<string>();
string[] rawusernames = ProfileNamesTextBox.Text.Split('\n');

foreach(string user in rawusernames)
{
    usernames.Add(user.Replace("\r", ""));
}

//add default usernames that are on every (windows) machine
usernames.Add("Public");
usernames.Add("Default");
usernames.Add("Default User");
usernames.Add("All Users");

//add the current user to that list
usernames.Add(Environment.UserName);

//find the users directory
DirectoryInfo di = new DirectoryInfo(Environment.GetEnvironmentVariable("USERPROFILE"));

//get our machine name path
DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName.ToString());
DirectoryEntries users = localDirectory.Children;

//for every child directory of the users directory
foreach (DirectoryInfo d in di.Parent.GetDirectories())
{
    //check if its a safe user name
    if(!usernames.Contains(d.Name))
    {
        //exception happens here
        DirectoryEntry user = users.Find(d.Name);
        users.Remove(user);
        user.Close();
    }
}
localDirectory.Close();
this.UseWaitCursor = false;
MessageBox.Show("All done!");

我不确定错误的含义是什么,我在Google上查了一下,但答案似乎比本地机器更基于服务器。有谁知道这个错误是什么以及如何修复它?

0 个答案:

没有答案