Skip to content

Instantly share code, notes, and snippets.

@JavierJF
Last active March 20, 2018 18:21
Show Gist options
  • Save JavierJF/e453bf81ace0d6e0c2cda342d14b87b3 to your computer and use it in GitHub Desktop.
Save JavierJF/e453bf81ace0d6e0c2cda342d14b87b3 to your computer and use it in GitHub Desktop.
Bug: Simple solution for setting the StartMenu layout using C# and PowerShell
var dotNetStartMenuSetter = edge.func({
source: path.join(__dirname, "dotnet/StartMenuSetter.csx"),
references: ["C:\\Program Files (x86)\\Reference Assemblies\\Microsoft\\WindowsPowerShell\\3.0\\System.Management.Automation.dll"]
});
gpii.windows.startMenuLayout.exportLayout = function () {
var promise = fluid.promise();
var result = dotNetStartMenuSetter("", true);
if (result === undefined) {
promise.reject({
isError: true,
message: "Undefined error"
});
} else if (result[0] === "Success") {
promise.resolve();
} else {
promise.reject({
isError: true,
message: result[1]
});
}
return promise;
};
/*!
* GPII Windows 10 Start Menu Layout Setter.
*
* Copyright 2018 Raising the Floor - International
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* The research leading to these results has received funding from the European Union's
* Seventh Framework Programme (FP7/2007-2013)
* under grant agreement no. 289016.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
public class Startup {
public async Task<object> Invoke(dynamic input) {
ArrayList result = new ArrayList();
try {
String dataPath = Path.Combine(Environment.GetEnvironmentVariable("LOCALAPPDATA"), "GPII\\menuLayouts\\current_layout.xml");
String _script =
@"$VerbosePreference = ""continue""
$ErrorActionPreference = ""Stop""
$CurrentLayout = '{0}'
function ExportLayout {{
Try {{
Export-StartLayout -Path $CurrentLayout -ErrorAction Stop
$error = ""Success""
$msg = """"
}} Catch {{
$error = ""Error""
$msg = $_.Exception.Message
}}
$error
$msg
}}
$return = ExportLayout
$return[0]
$return[1]
";
String script = string.Format(_script, dataPath);
using (PowerShell PowerShellInstance = PowerShell.Create()) {
PowerShellInstance.AddScript(script, false);
Collection<PSObject> psOutput = PowerShellInstance.Invoke();
foreach (var psObj in psOutput) {
result.Add(psObj.ToString());
}
}
} catch (Exception ex) {
result.Add("Error");
result.Add(ex.Message);
}
return result.ToArray();
}
static void Main(string[] args) {
doStuff().GetAwaiter().GetResult();
}
static async Task doStuff() {
Startup st = new Startup();
var output = (Array) await st.Invoke("");
foreach (var obj in output) {
System.Console.WriteLine(obj);
}
output = (Array) await st.Invoke("");
foreach (var obj in output) {
System.Console.WriteLine(obj);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment