private static bool DeleteAllUserFoldersExceptAdmin(bool hadDeleteUserErrors) { bool deleteFolderErrors = false; List <string> dontDeleteFolders = new List <string>(); dontDeleteFolders.AddRange(new string[] { "Administrator", "All Users", "Default", "Default User", "Public", currentUser }); string folder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); DirectoryInfo di = new DirectoryInfo(folder).Parent; DirectoryInfo[] subDirs = di.GetDirectories("*"); if (hadDeleteUserErrors) { WriteOutput("One or more users weren't deleted. Delete all user folders anyway? <y/[n]>"); TimeoutReader.YesNoTimeout ynt = TimeoutReader.WaitForYorNwithTimeout(10); if (ynt != TimeoutReader.YesNoTimeout.Yes) { SelectQuery query = new SelectQuery("Win32_UserAccount"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); foreach (ManagementObject envVar in searcher.Get()) { dontDeleteFolders.Add(envVar["Name"].ToString()); } } } foreach (DirectoryInfo subDir in subDirs) { if (!dontDeleteFolders.Contains(subDir.Name)) { WriteOutput(false, "Deleting userfolder {0}... ", subDir.Name); StartProcess("CMD.exe", String.Format("/C RD /S /Q \"{0}\"", subDir.FullName), 15); if (Directory.Exists(subDir.FullName)) { deleteFolderErrors = true; WriteOutput("Error!"); } else { WriteOutput("Done!"); } } } return(deleteFolderErrors); }
static void Main(string[] args) { Console.Title = "Reset Standalone PC - Made by Stiig Gade"; WriteOutput(".oO -Made by Stiig Gade- Oo.\n"); if (IsInDomain()) { WriteOutput("You are part of a domain. Run again when not in Domain. Press key to close..."); Console.ReadKey(true); Environment.Exit(0); } userProfilePath = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)).FullName; currentUser = UserPrincipal.Current.Name; WriteOutput("Delete all other users than Administrator and you? <y/[n]>"); TimeoutReader.YesNoTimeout yntDeleteUsers = TimeoutReader.WaitForYorNwithTimeout(10); WriteOutput("Create new user? <y/[n]>"); TimeoutReader.YesNoTimeout yntCreateUser = TimeoutReader.WaitForYorNwithTimeout(10); if (yntDeleteUsers == TimeoutReader.YesNoTimeout.Yes) { WriteOutput(EmptyOrHyphens.Empty); WriteOutput("Deleting users:"); bool deleteUsersError = DeleteAllUsersExceptAdmin(); WriteOutput("Done deleting users{0}!", (deleteUsersError ? ", but with errors" : "")); WriteOutput(EmptyOrHyphens.Hyphens); WriteOutput(EmptyOrHyphens.Empty); WriteOutput("Deleting user folders:"); bool deleteUserFolderErrors = DeleteAllUserFoldersExceptAdmin(deleteUsersError); WriteOutput("Done deleting users folders{0}!", (deleteUserFolderErrors ? ", but with errors" : "")); if (deleteUserFolderErrors) { Process.Start(userProfilePath); } WriteOutput(EmptyOrHyphens.Hyphens); WriteOutput(EmptyOrHyphens.Empty); } if (yntCreateUser == TimeoutReader.YesNoTimeout.Yes) { string newUsername = "******"; string newPassword = "******"; DeactivatePasswordComplexity(); WriteOutput(false, "New username (within 10 sec)? [{0}]: ", newUsername); newUsername = TimeoutReader.WaitForInputOrTimeout(10, newUsername); WriteOutput(false, "New password? (within 10 sec) [{0}]: ", newPassword); newPassword = TimeoutReader.WaitForInputOrTimeout(10, newPassword); } Console.ReadKey(true); Environment.Exit(0); ////Delete old user if exists //Console.Write("Deleting old user... "); //if (CheckIfUserExist(username)) //{ // StartProcess("CMD.exe", String.Format("/C net user \"{0}\" /DEL", username)); // if (CheckIfUserExist(username)) // { // do // { // Console.WriteLine("Error!"); // Console.WriteLine(" The user wasn't deleted!"); // Console.WriteLine(" Delete {0} manually! Then press 'c' to continue...", username); // error = true; // WaitForKey(ConsoleKey.C); // } while (CheckIfUserExist(username)); // } // else // Console.WriteLine("Done!"); //} //else //{ // Console.WriteLine("Didn't exist!"); //} ////If user-profile folder exists, delete it //Console.Write("Deleting old folder(s)..."); ////Create new user //Console.Write("Creating new user... "); //StartProcess("CMD.exe", String.Format("/C net user \"{0}\" \"{1}\" /passwordchg:no /ADD", username, password), 15); //StartProcess("CMD.exe", String.Format("/C WMIC USERACCOUNT WHERE \"Name='{0}'\" SET PasswordExpires=FALSE", username), 15); //if (CheckIfUserExist(username)) // Console.WriteLine("Done!"); //else //{ // Console.WriteLine("Error!"); // Console.WriteLine(" New user wasn't created!"); // Console.WriteLine(" Create manually! Then press 'c' to continue..."); // WaitForKey(ConsoleKey.C); // error = true; //} //bool addToAdministrator = false; //Console.WriteLine("Make {0} Administrator? <y/n>", username); //if (WaitForYorNwithTimeout(10)) //{ // addToAdministrator = true; // Console.Write("Adding user to Administrators... "); // StartProcess("CMD.exe", String.Format("/C net localgroup Administratorer \"{0}\" /ADD", username), 15); // if (IsUserInGroup(username, "Administratorer")) // { // Console.WriteLine("Done!"); // } // else // { // Console.WriteLine("Error!"); // Console.WriteLine(" User wasn't added to Administrators!"); // Console.WriteLine(" Add manually! Then press 'c' to continue..."); // WaitForKey(ConsoleKey.C); // error = true; // } //} //if (!error) //{ // Console.WriteLine(" Username: {0}", username); // Console.WriteLine(" Password: {0}", password); // Console.WriteLine(" Administrator: {0}", addToAdministrator.ToString()); // Console.WriteLine("Press key to exit..."); // Console.ReadKey(true); //} //else //{ // Console.WriteLine("Error(s) have occured! Press 'e' to exit..."); // WaitForKey(ConsoleKey.E); //} }