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");
		}
	}

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *