diff --git a/src/windows2go.switcher/FormMain.Designer.cs b/src/windows2go.switcher/FormMain.Designer.cs
index 9e2bdef..07a9b84 100644
--- a/src/windows2go.switcher/FormMain.Designer.cs
+++ b/src/windows2go.switcher/FormMain.Designer.cs
@@ -30,7 +30,7 @@
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.menuStripItemAbout = new System.Windows.Forms.ToolStripMenuItem();
this.groupBox = new System.Windows.Forms.GroupBox();
- this.button2 = new System.Windows.Forms.Button();
+ this.buttonSetNormal = new System.Windows.Forms.Button();
this.buttonSetPortable = new System.Windows.Forms.Button();
this.labelState = new System.Windows.Forms.Label();
this.menuStrip.SuspendLayout();
@@ -79,7 +79,7 @@
this.groupBox.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
- this.groupBox.Controls.Add(this.button2);
+ this.groupBox.Controls.Add(this.buttonSetNormal);
this.groupBox.Controls.Add(this.buttonSetPortable);
this.groupBox.Controls.Add(this.labelState);
this.groupBox.Location = new System.Drawing.Point(12, 27);
@@ -89,15 +89,16 @@
this.groupBox.TabStop = false;
this.groupBox.Text = "Current state:";
//
- // button2
+ // buttonSetNormal
//
- this.button2.Enabled = false;
- this.button2.Location = new System.Drawing.Point(260, 124);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(197, 41);
- this.button2.TabIndex = 1;
- this.button2.Text = "Set Normal";
- this.button2.UseVisualStyleBackColor = true;
+ this.buttonSetNormal.Enabled = false;
+ this.buttonSetNormal.Location = new System.Drawing.Point(260, 124);
+ this.buttonSetNormal.Name = "buttonSetNormal";
+ this.buttonSetNormal.Size = new System.Drawing.Size(197, 41);
+ this.buttonSetNormal.TabIndex = 1;
+ this.buttonSetNormal.Text = "Set Normal";
+ this.buttonSetNormal.UseVisualStyleBackColor = true;
+ this.buttonSetNormal.Click += new System.EventHandler(this.buttonSetNormal_Click);
//
// buttonSetPortable
//
@@ -108,6 +109,7 @@
this.buttonSetPortable.TabIndex = 1;
this.buttonSetPortable.Text = "Set Portable (windows to go)";
this.buttonSetPortable.UseVisualStyleBackColor = true;
+ this.buttonSetPortable.Click += new System.EventHandler(this.buttonSetPortable_Click);
//
// labelState
//
@@ -153,7 +155,7 @@
private System.Windows.Forms.ToolStripMenuItem menuStripItemAbout;
private System.Windows.Forms.GroupBox groupBox;
private System.Windows.Forms.Label labelState;
- private System.Windows.Forms.Button button2;
+ private System.Windows.Forms.Button buttonSetNormal;
private System.Windows.Forms.Button buttonSetPortable;
}
}
diff --git a/src/windows2go.switcher/FormMain.cs b/src/windows2go.switcher/FormMain.cs
index a42ae03..f6c6f4a 100644
--- a/src/windows2go.switcher/FormMain.cs
+++ b/src/windows2go.switcher/FormMain.cs
@@ -1,4 +1,5 @@
-using System;
+using Microsoft.Win32;
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@@ -12,12 +13,115 @@ namespace windows2go.switcher {
public partial class FormMain : Form {
public FormMain() {
InitializeComponent();
+ if (!UACSecurity.IsAdmin()) {
+ UACSecurity.AddShieldToButton(buttonSetNormal);
+ UACSecurity.AddShieldToButton(buttonSetPortable);
+ } else
+ this.Text += " (Administrator)";
}
+ bool PortableState = false;
+ bool KeyExists = false;
+ bool unExpected=false;
+
private void FormMain_Load(object sender, EventArgs e) {
-
+ FullRegistryCheck();
}
-
+ private void FullRegistryCheck() {
+ GetState();
+ if (!KeyExists) {
+ inUnexpectedlState();
+ } else {
+ switch (PortableState) {
+ case true:
+ inPortableState();
+ break;
+ case false:
+ inNormalState();
+ break;
+ default:
+ inUnexpectedlState();
+ break;
+ }
+ }
+ }
+
+ private void GetState() {
+ RegistryKey key = RegistryKey.OpenBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, RegistryView.Registry32);
+ key = key.OpenSubKey(@"SYSTEM\CurrentControlSet\Control");
+
+ //check for exists "PortableOperatingSystem"
+ if (key != null) {
+ switch (key.GetValue("PortableOperatingSystem")) {
+ case 0:
+ PortableState = true;
+ KeyExists = true;
+ break;
+ case 1:
+ PortableState = false;
+ KeyExists = true;
+ break;
+ default:
+ PortableState = false;
+ KeyExists = false;
+ break;
+ }
+ }
+
+ }
+ private void inNormalState() {
+ buttonSetNormal.Enabled = false;
+ buttonSetPortable.Enabled = true;
+ labelState.Text = @"You are in normal mode";
+ labelState.ForeColor = Color.LimeGreen;
+ unExpected = false;
+ }
+ private void inPortableState() {
+ buttonSetNormal.Enabled = true;
+ buttonSetPortable.Enabled = false;
+ labelState.Text = @"You are in portable (Windows To Go) mode";
+ labelState.ForeColor = Color.Crimson;
+ unExpected = false;
+ }
+ private void inUnexpectedlState() {
+ buttonSetNormal.Enabled = true;
+ buttonSetPortable.Enabled = false;
+ buttonSetNormal.Text = @"Forced set to normal?";
+ labelState.Text = "Maby DWORD \"PortableOperatingSystem\" not found in registry or have wrong value or type." +Environment.NewLine+
+ "sIt equals normal mode.";
+ labelState.ForeColor = Color.OrangeRed;
+ unExpected = true;
+ }
+
+ private void buttonSetNormal_Click(object sender, EventArgs e) {
+ if (UACSecurity.IsAdmin()) {
+ switch (unExpected) {
+ case true:
+ MessageBox.Show("oh no.", "");
+ FullRegistryCheck();
+ break;
+
+ case false:
+ MessageBox.Show("yay.", "");
+ FullRegistryCheck();
+ break;
+ }
+ } else {
+ UACSecurity.RestartElevated();
+ }
+
+ }
+
+ private void buttonSetPortable_Click(object sender, EventArgs e) {
+ if (UACSecurity.IsAdmin()) {
+ MessageBox.Show("okay.", "");
+ FullRegistryCheck();
+ } else {
+ UACSecurity.RestartElevated();
+ }
+
+
+ }
}
}
diff --git a/src/windows2go.switcher/UACSecurity.cs b/src/windows2go.switcher/UACSecurity.cs
new file mode 100644
index 0000000..e32cbdc
--- /dev/null
+++ b/src/windows2go.switcher/UACSecurity.cs
@@ -0,0 +1,56 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Diagnostics;
+using System.Windows.Forms;
+using System.Security.Principal;
+
+namespace windows2go.switcher {
+ public static class UACSecurity {
+ [DllImport("user32")]
+ public static extern UInt32 SendMessage(IntPtr hWnd, UInt32 msg, UInt32 wParam, UInt32 lParam);
+
+ internal const int BCM_FIRST = 0x1600;
+ internal const int BCM_SETSHIELD = (BCM_FIRST + 0x000C);
+
+ static internal bool IsVistaOrHigher() {
+ return Environment.OSVersion.Version.Major < 6;
+ }
+
+ ///
+ /// Checks if the process is elevated
+ ///
+ /// If is elevated
+ static internal bool IsAdmin() {
+ WindowsIdentity id = WindowsIdentity.GetCurrent();
+ WindowsPrincipal p = new WindowsPrincipal(id);
+ return p.IsInRole(WindowsBuiltInRole.Administrator);
+ }
+
+ ///
+ /// Add a shield icon to a button
+ ///
+ /// The button
+ static internal void AddShieldToButton(Button b) {
+ b.FlatStyle = FlatStyle.System;
+ SendMessage(b.Handle, BCM_SETSHIELD, 0, 0xFFFFFFFF);
+ }
+
+ ///
+ /// Restart the current process with administrator credentials
+ ///
+ internal static void RestartElevated() {
+ ProcessStartInfo startInfo = new ProcessStartInfo();
+ startInfo.UseShellExecute = true;
+ startInfo.WorkingDirectory = Environment.CurrentDirectory;
+ startInfo.FileName = Application.ExecutablePath;
+ startInfo.Verb = "runas";
+ try {
+ Process p = Process.Start(startInfo);
+ } catch (System.ComponentModel.Win32Exception ex) {
+ return; //If cancelled, do nothing
+ }
+
+ Application.Exit();
+ }
+ }
+}
diff --git a/src/windows2go.switcher/iconfinder_microsoft_246000.ico b/src/windows2go.switcher/windows.ico
similarity index 100%
rename from src/windows2go.switcher/iconfinder_microsoft_246000.ico
rename to src/windows2go.switcher/windows.ico
diff --git a/src/windows2go.switcher/windows2go.switcher.csproj b/src/windows2go.switcher/windows2go.switcher.csproj
index b039d0b..229826f 100644
--- a/src/windows2go.switcher/windows2go.switcher.csproj
+++ b/src/windows2go.switcher/windows2go.switcher.csproj
@@ -33,7 +33,7 @@
4
- iconfinder_microsoft_246000.ico
+ windows.ico
@@ -57,6 +57,7 @@
+
FormMain.cs
@@ -83,7 +84,7 @@
-
+
\ No newline at end of file