Enable Command Line

Before you are able to execute Command Line commands you need to enable them for the default user in just the same way as you have to before they are permitted in the default IoT CoreDefaultApp

Enable them as shown here.

Executing Command Line Strings Programatically



	//*************************************************
	//*************************************************
	//********** EXECUTE COMMAND LINE STRING **********
	//*************************************************
	//*************************************************
	private async Task<string> ExecuteCommandLineString(string CommandString)
	{
		const string CommandLineProcesserExe = "c:\\windows\\system32\\cmd.exe";
		const uint CommandStringResponseBufferSize = 8192;
		string currentDirectory = "C:\\";

		StringBuilder textOutput = new StringBuilder((int)CommandStringResponseBufferSize);
		uint bytesLoaded = 0;

		if (string.IsNullOrWhiteSpace(CommandString))
			return("");

		var commandLineText = CommandString.Trim();
			
		var standardOutput = new Windows.Storage.Streams.InMemoryRandomAccessStream();
		var standardError = new Windows.Storage.Streams.InMemoryRandomAccessStream();
		var options = new Windows.System.ProcessLauncherOptions
		{
			StandardOutput = standardOutput,
			StandardError = standardError
		};
			
		try
		{
			var args = "/C \"cd \"" + currentDirectory + "\" & " + commandLineText + "\"";
			var result = await Windows.System.ProcessLauncher.RunToCompletionAsync(CommandLineProcesserExe, args, options);

			//First write std out
			using (var outStreamRedirect = standardOutput.GetInputStreamAt(0))
			{
				using (var dataReader = new Windows.Storage.Streams.DataReader(outStreamRedirect))
				{
					while ((bytesLoaded = await dataReader.LoadAsync(CommandStringResponseBufferSize)) > 0)
						textOutput.Append(dataReader.ReadString(bytesLoaded));

					new System.Threading.ManualResetEvent(false).WaitOne(10);
					if ((bytesLoaded = await dataReader.LoadAsync(CommandStringResponseBufferSize)) > 0)
						textOutput.Append(dataReader.ReadString(bytesLoaded));
				}
			}

			//Then write std err
			using (var errStreamRedirect = standardError.GetInputStreamAt(0))
			{
				using (var dataReader = new Windows.Storage.Streams.DataReader(errStreamRedirect))
				{
					while ((bytesLoaded = await dataReader.LoadAsync(CommandStringResponseBufferSize)) > 0)
						textOutput.Append(dataReader.ReadString(bytesLoaded));

					new System.Threading.ManualResetEvent(false).WaitOne(10);
					if ((bytesLoaded = await dataReader.LoadAsync(CommandStringResponseBufferSize)) > 0)
						textOutput.Append(dataReader.ReadString(bytesLoaded));
				}
			}

			return (textOutput.ToString());
		}
		catch (UnauthorizedAccessException uex)
		{
			return("ERROR - " + uex.Message + "\n\nCmdNotEnabled");
		}
		catch (Exception ex)
		{
			return("ERROR - " + ex.Message + "\n");
		}
	}

 

UK Electronic Designer
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.