简体   繁体   English

C#删除用户配置文件

[英]c# delete user profile

I am trying to use c# to delete a user profile on a remote server. 我正在尝试使用c#删除远程服务器上的用户配置文件。 I am running the program as myself. 我自己运行程序。 If I browse to \\\\server\\c$\\Users\\ as myself I can delete the directory "User". 如果我以自己的身份浏览到\\\\ server \\ c $ \\ Users \\,则可以删除目录“ User”。 It gives no error. 它没有错误。 If I use my program written in C# with the code below to attempt to delete the same directory I get back this exception. 如果我使用用C#编写的程序和下面的代码来尝试删除相同的目录,则会得到此异常。

Access to the path 'appsFolder.itemdata-ms' is denied. 拒绝访问路径“ appsFolder.itemdata-ms”。

Am I doing something wrong with my delete? 我的删除操作有问题吗?

Directory.Delete("\\\\server\\c$\\Users\\User\\",true);

Deleting a user profile folder without cleaning the registry accoringly can lead to several undesired side-effects like temporary profile creation etc. i recommend using DeleteProfile function which can be found in userenv.dll 删除用户配置文件文件夹而未正确清理注册表可能会导致一些不良影响,例如临时配置文件创建等。我建议使用可以在userenv.dll中找到的DeleteProfile函数

my code is as follow: 我的代码如下:

    internal class Program
{
    [DllImport("userenv.dll", CharSet = CharSet.Unicode, ExactSpelling = false, SetLastError = true)]
    public static extern bool DeleteProfile(string sidString, string profilePath, string omputerName);

    private static void Main(string[] args)
    {
        try
        {
            var username = args[0];
            var principalContext = new PrincipalContext(ContextType.Domain); // Domain => to support local user this should be changed probably, didn't test yet
            var userPrincipal = UserPrincipal.FindByIdentity(principalContext, username);
            if (userPrincipal != null)
            {
                Console.WriteLine("User found");
                var userSid = userPrincipal.Sid;
                Console.WriteLine("User {0} has SID: {1}", username, userSid);
                Console.WriteLine("Will try to DeleteProfile next..");
                DeleteProfile(userSid.ToString(), null, null);
                Console.WriteLine("Done - bye!");
            }
            else
            {
                Console.WriteLine("ERROR! User: {0} not found!", username);
            }
        }
        catch (Exception exception)
        {
            Console.WriteLine(exception);   
        }
    }
}

consider, this code is just for demonstration purpose and should be stabilized for production.. 考虑一下,该代码仅用于演示目的,应在生产中加以稳定。

cheers, 干杯,

-Chris -克里斯

btw, here more on MSDN https://msdn.microsoft.com/en-us/library/windows/desktop/bb762273(v=vs.85).aspx 顺便说一句,有关MSDN的更多信息https://msdn.microsoft.com/zh-cn/library/windows/desktop/bb762273(v=vs.85).aspx

Hi I was trying to same thing and found Directory.Delete() cannot delete files if file is Hidden or a system file. 嗨,我尝试做同样的事情,发现Directory.Delete()无法删除文件(如果文件为隐藏文件或系统文件)。

Using cmd instead to delete folder. 使用cmd代替删除文件夹。

   public static FileAttributes RemoveAttribute (FileAttributes att, FileAttributes attToRemove)
   {
        return att &  ~attToRemove;
    }

public void DeleteProfileFolder(string file)
 {
    Process process = new Process();
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.WindowStyle = ProvessWindowsStyle.Hiddenl
    startInfo.FileName = "cmd";
    startInfo.Arguments = "/C rd /S /Q  \"" + file + "\"";
    process.StartInfo = startInfo;
   process.Start();
   process.WaitForExit();
}

public void Deletes(DirectoryInfo baseDir)
{  
     if(! baseDir.Exists)
       return;
   var Dirs = Directory.EnumerateDirectories(baseDir.ToString(),"*.*",SearchOption.TopDirectoryOnly);
   var files = Directory.EnumerateFiles(baseDir.ToString(),"*.*",SearchOption.TopDirectoryOnly);

   foreach(var dir in Dirs)
   {
         DeleteProfileFolder(dir);
    } 
  foreach(var file in files)
 {
      FileAttributes att = File.GetAttributes(f);
      if((att & FileAttributes.Hidden) == FileAttribute.Hidden)
      {  
            att = RemoveAttribute(att, FileAttributes.Hidden);
            File.SetAttributes(file , att);
            File.SetAttributes(File, FileAttributes.Normal)
        }
   File.Delete(file);
  }

}

To call This 打电话给这个

Deletes("c:\\Users\\"); 删除( “C:\\用户\\”); // did this on local system. //在本地系统上执行此操作。

I havn't tried on network location but I thing this will work. 我没有尝试过网络位置,但我认为这可以工作。

Note: To completely delete userProfile we also need to delete registry. 注意:要完全删除userProfile,我们还需要删除注册表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM