locked
Unlock bit locker programmatically. RRS feed

  • General discussion

  • 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();

                         }

                  }

           }

    • Edited by Chao Kuo Wednesday, July 4, 2012 11:04 AM
    Wednesday, July 4, 2012 10:41 AM

All replies

  • I haven't done this before hence not so sure. Anyway I found something here which might be of your interest.

    If this post answers your question, please click Mark As Answer. If this post is helpful please click Mark as Helpful.

    Regards,
    Nairs

    Wednesday, July 4, 2012 11:34 AM
  • I know this is an old thread but I just wanted to say THANKS! :) this code sample really helped me, there aren't many resources out there for working with Win32_EncryptableVolume or than reading information.
    Wednesday, October 7, 2015 11:09 AM
  • 

    As said before, this is an old thread but really usefull for me. Many Thanks !!! Your peace of code helped me greatly :-)

    

    Friday, November 4, 2016 10:01 AM

  • Hi Everyone,

    We are using ManagementClass from "System.Management.dll" assembly to find the drive is locked by bit-locker. Below provided the code snippet we were using.

    This is code works fine in Windows 10, 8 and lower versions

    However, we are getting exception in "management.GetInstances();" in Windows Server 2012, 2016 and in some Windows 10 OS machines.

    Please suggest any ideas or ways to get resolve this exception.


    Code snippet:


    var drive = new ManagementPath();
    drive.NamespacePath = "\\ROOT\\CIMV2\\Security\\MicrosoftVolumeEncryption";
    drive.ClassName = "Win32_EncryptableVolume";

    var scope = new ManagementScope(drive, new ConnectionOptions() { Impersonation = ImpersonationLevel.Impersonate });
    var management = new ManagementClass(scope, drive, new ObjectGetOptions());

    var twxt = management.GetInstances();

    Exception Details:

    ErrorCode: InvalidNamespace

    Error Message: Invalid namespace 

    Stack Trace:

       at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
       at System.Management.ManagementScope.InitializeGuts(Object o)
       at System.Management.ManagementScope.Initialize()
       at System.Management.ManagementObject.Initialize(Boolean getObject)
       at System.Management.ManagementClass.GetInstances(EnumerationOptions options)
       at ConsoleApplication1.Program.Main(String[] args) in C:\Users\Administrator\documents\visual studio 2015\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs:line 45
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
       
    Regards,
    Balaji R

    -ji

    Wednesday, October 31, 2018 4:34 PM