手動でユーザプロファイルを消してしまった。

消すユーザプロファイルが多かったので、エクスプローラで消したら
消したユーザーがログインできなくなってしまった。

修復を試みるためにC#プログラムを作成
実行するときは管理者権限で行う。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace check
{
    class Program
    {
        static void Main(string[] args)
        {
            string ProfileList = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList";
            Microsoft.Win32.RegistryKey rkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(ProfileList, true);
            Microsoft.Win32.RegistryKey skey;
            if (rkey == null)
            {
                Console.WriteLine("レジストリキーが開けませんでした。");
                return;
            }
            string[] SubKeys = rkey.GetSubKeyNames();
            foreach (string sname in SubKeys)
            {
                string subkey = ProfileList + @"\" + sname;
                skey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(subkey);
                string upath = (string)skey.GetValue("ProfileImagePath");
                skey.Close();
                if (Directory.Exists(upath))
                {
                    Console.WriteLine(sname + " ;あり " + upath);
                }
                else
                {
                    Console.WriteLine(sname + " :削除 " + upath);
                    try
                    {
                        rkey.DeleteSubKey(sname, false);
                    }
                    catch (Exception)
                    {
                    }
                    try
                    {
                        rkey.DeleteSubKeyTree(sname, false);
                    }
                    catch (Exception)
                    {

                    }
                }
            }
            rkey.Close();
        }
    }
}
カテゴリー: Windows, プログラム, 仕事 タグ: , , パーマリンク