Unlock bit locker programmatically.

  • Thread starter Thread starter Chao Kuo
  • Start date Start date
C

Chao Kuo

Guest
Hi, dudes

I always thinking a question about how to unlock the bitlocker programmatically, And finally get an answer from the related classes, and I want this can help.


class Program

{

static void Main(string[] args)

{

//Create an short cut to startup For this program.

string startupLocation = @"C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup";

CreateShortCutToAFolder(startupLocation);


string programStartLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;

Console.WriteLine(programStartLocation);

var path = new ManagementPath();

path.NamespacePath = "\\ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption"; path.ClassName = "Win32_EncryptableVolume";

var scope = new ManagementScope(path, new ConnectionOptions() { Impersonation = ImpersonationLevel.Impersonate });

var management = new ManagementClass(scope, path, new ObjectGetOptions());

foreach (ManagementObject vol in management.GetInstances())

{

Console.WriteLine("----" + vol["DriveLetter"]);

switch ((uint)vol["ProtectionStatus"])

{

case 0:

Console.WriteLine("not protected by bitlocker");

break;

case 1:

Console.WriteLine("unlocked");

break;

case 2:

Console.WriteLine("locked");

break;

}

if ((uint)vol["ProtectionStatus"] == 2)

{

Console.WriteLine("unlock this driver ...");

vol.InvokeMethod("UnlockWithPassphrase", new object[]{"password"});

Console.WriteLine("unlock done.");

}

}

}


private static void CreateShortCutToAFolder(string startupLocation)

{

String name = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);

if(!File.Exists(startupLocation + @"\" + name +".lnk"))

{

Shell32.Shell shl = new Shell32.Shell();

// Optional code to create the shortcut

System.IO.StreamWriter sw = new System.IO.StreamWriter(startupLocation + @"\" + name + ".lnk", false);

sw.Close();

// End optional code

Shell32.Folder dir = shl.NameSpace(startupLocation);

Shell32.FolderItem itm = dir.Items().Item(name + ".lnk");

Shell32.ShellLinkObject lnk = (Shell32.ShellLinkObject)itm.GetLink;

// Optional code to create the shortcut

lnk.Path = System.Reflection.Assembly.GetExecutingAssembly().Location;

lnk.Description = "shortcut";


// we can set the configure file in the following.

// lnk.Arguments = @"c:\config.xml";

lnk.WorkingDirectory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

// End optional code

lnk.Save();

}


}

}

Continue reading...
 
Back
Top