1

I have scoured the web looking for solutions on how to SafeBoot into Windows using only C#. Since Vista and above, safe booting is controlled using BCD. Ofcourse you could use the commandline tool "bcdedit":

bcdedit /set {current} safeboot Minimal

However I do not want to use this approach. So my question is:

How do I reboot into safe mode using only C#?

I have already looked at this SO post, which has got me started. But I'm still missing pieces to this puzzle.

Any help is greatly appreciated. =)

BCD WMI Provider Reference is of little help.

Community
  • 1
  • 1
Vippy
  • 1,356
  • 3
  • 20
  • 30
  • take a look at this `SO` posting should be straight forward write it in C# http://stackoverflow.com/questions/14023051/bcdedit-not-recognized-when-running-via-c-sharp – MethodMan Aug 13 '14 at 20:14
  • Thanks for your response, however I have no issues using bcdedit in other situations. In my situation, I cannot use bcdedit or more specifically, cannot use a command-line tool. – Vippy Aug 13 '14 at 20:41
  • is there a WMI solution out there that you can try – MethodMan Aug 13 '14 at 20:43
  • Yes, I just cannot figure out the implementation. Most use it to get information back, as is used in the SO post linked above. – Vippy Aug 13 '14 at 20:45
  • so how come you can't use the posted anwser from the `SO` link that you posted..? C# uses WMI from the `ManagementObject` I do not think you are familiar with how to use WMI and C# – MethodMan Aug 13 '14 at 20:51
  • I have built my code using their example and have gotten back a BCD object. However I cannot find the appropriate property for setting the "SafemodeMinimal" as described in the MSDN docs. – Vippy Aug 13 '14 at 21:05
  • that sounds more like and `Enum` than a property.. but I could be wrong – MethodMan Aug 13 '14 at 21:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59305/discussion-between-vippy-and-dj-kraze). – Vippy Aug 13 '14 at 21:08

1 Answers1

2

I wrote up the following code in C# that should allow you to set the safeboot value and delete that value:

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

namespace EditBcdStore
{
    public class BcdStoreAccessor
    {
        public const int BcdOSLoaderInteger_SafeBoot = 0x25000080;

        public enum BcdLibrary_SafeBoot
        {
            SafemodeMinimal = 0,
            SafemodeNetwork = 1,
            SafemodeDsRepair = 2
        }

        private ConnectionOptions connectionOptions;
        private ManagementScope managementScope;
        private ManagementPath managementPath;

        public BcdStoreAccessor()
        {
            connectionOptions = new ConnectionOptions();
            connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
            connectionOptions.EnablePrivileges = true;

            managementScope = new ManagementScope("root\\WMI", connectionOptions);

            managementPath = new ManagementPath("root\\WMI:BcdObject.Id=\"{fa926493-6f1c-4193-a414-58f0b2456d1e}\",StoreFilePath=\"\"");
        }

        public void SetSafeboot()
        {
            ManagementObject currentBootloader = new ManagementObject(managementScope, managementPath, null);
            currentBootloader.InvokeMethod("SetIntegerElement", new object[] { BcdOSLoaderInteger_SafeBoot, BcdLibrary_SafeBoot.SafemodeMinimal });
        }

        public void RemoveSafeboot()
        {
            ManagementObject currentBootloader = new ManagementObject(managementScope, managementPath, null);
            currentBootloader.InvokeMethod("DeleteElement", new object[] { BcdOSLoaderInteger_SafeBoot });
        }
    }
}

I tested this on my Surface Pro and it seemed to work, as can be verified by running:

bcdedit /enum {current} /v

Update:

The code above is just for setting or removing the value that allows you to safeboot.

After this has been performed, a reboot is required, which can also be accomplished using WMI as is shown here:

WMI to reboot remote machine

The answer shows an example for performing this locally or remotely.

Big thanks to Helen and L-Williams.

Community
  • 1
  • 1
L-Williams
  • 146
  • 1
  • 9
  • Wow, thank you SOOO very much. This is exactly what I needed! Just tested it and works beautifully! Many thanks. – Vippy Aug 14 '14 at 23:51