{"id":692,"date":"2017-03-02T21:38:52","date_gmt":"2017-03-02T21:38:52","guid":{"rendered":"https:\/\/ibex.tech\/windows-iot\/?p=523"},"modified":"2017-03-02T21:38:52","modified_gmt":"2017-03-02T21:38:52","slug":"executing-command-line-commands","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/uwp-programming-in-c\/command-line-uwp-programming-in-c\/executing-command-line-commands","title":{"rendered":"Executing Command Line Commands"},"content":{"rendered":"<h4>\n\tEnable Command Line<br \/>\n<\/h4>\n<p>\n\tBefore 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\n<\/p>\n<p>\n\tEnable them <a href=\"https:\/\/ibex.tech\/windows-iot\/windows-iot\/command-line\/enabling-command-line\">as shown here<\/a>.\n<\/p>\n<h4>\n\tExecuting Command Line Strings Programatically<br \/>\n<\/h4>\n<pre>\n<code>\n\n\t\/\/*************************************************\n\t\/\/*************************************************\n\t\/\/********** EXECUTE COMMAND LINE STRING **********\n\t\/\/*************************************************\n\t\/\/*************************************************\n\tprivate async Task&lt;string&gt; ExecuteCommandLineString(string CommandString)\n\t{\n\t\tconst string CommandLineProcesserExe = &quot;c:\\\\windows\\\\system32\\\\cmd.exe&quot;;\n\t\tconst uint CommandStringResponseBufferSize = 8192;\n\t\tstring currentDirectory = &quot;C:\\\\&quot;;\n\n\t\tStringBuilder textOutput = new StringBuilder((int)CommandStringResponseBufferSize);\n\t\tuint bytesLoaded = 0;\n\n\t\tif (string.IsNullOrWhiteSpace(CommandString))\n\t\t\treturn(&quot;&quot;);\n\n\t\tvar commandLineText = CommandString.Trim();\n\t\t\t\n\t\tvar standardOutput = new Windows.Storage.Streams.InMemoryRandomAccessStream();\n\t\tvar standardError = new Windows.Storage.Streams.InMemoryRandomAccessStream();\n\t\tvar options = new Windows.System.ProcessLauncherOptions\n\t\t{\n\t\t\tStandardOutput = standardOutput,\n\t\t\tStandardError = standardError\n\t\t};\n\t\t\t\n\t\ttry\n\t\t{\n\t\t\tvar args = &quot;\/C \\&quot;cd \\&quot;&quot; + currentDirectory + &quot;\\&quot; &amp; &quot; + commandLineText + &quot;\\&quot;&quot;;\n\t\t\tvar result = await Windows.System.ProcessLauncher.RunToCompletionAsync(CommandLineProcesserExe, args, options);\n\n\t\t\t\/\/First write std out\n\t\t\tusing (var outStreamRedirect = standardOutput.GetInputStreamAt(0))\n\t\t\t{\n\t\t\t\tusing (var dataReader = new Windows.Storage.Streams.DataReader(outStreamRedirect))\n\t\t\t\t{\n\t\t\t\t\twhile ((bytesLoaded = await dataReader.LoadAsync(CommandStringResponseBufferSize)) &gt; 0)\n\t\t\t\t\t\ttextOutput.Append(dataReader.ReadString(bytesLoaded));\n\n\t\t\t\t\tnew System.Threading.ManualResetEvent(false).WaitOne(10);\n\t\t\t\t\tif ((bytesLoaded = await dataReader.LoadAsync(CommandStringResponseBufferSize)) &gt; 0)\n\t\t\t\t\t\ttextOutput.Append(dataReader.ReadString(bytesLoaded));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t\/\/Then write std err\n\t\t\tusing (var errStreamRedirect = standardError.GetInputStreamAt(0))\n\t\t\t{\n\t\t\t\tusing (var dataReader = new Windows.Storage.Streams.DataReader(errStreamRedirect))\n\t\t\t\t{\n\t\t\t\t\twhile ((bytesLoaded = await dataReader.LoadAsync(CommandStringResponseBufferSize)) &gt; 0)\n\t\t\t\t\t\ttextOutput.Append(dataReader.ReadString(bytesLoaded));\n\n\t\t\t\t\tnew System.Threading.ManualResetEvent(false).WaitOne(10);\n\t\t\t\t\tif ((bytesLoaded = await dataReader.LoadAsync(CommandStringResponseBufferSize)) &gt; 0)\n\t\t\t\t\t\ttextOutput.Append(dataReader.ReadString(bytesLoaded));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn (textOutput.ToString());\n\t\t}\n\t\tcatch (UnauthorizedAccessException uex)\n\t\t{\n\t\t\treturn(&quot;ERROR - &quot; + uex.Message + &quot;\\n\\nCmdNotEnabled&quot;);\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\treturn(&quot;ERROR - &quot; + ex.Message + &quot;\\n&quot;);\n\t\t}\n\t}\n<\/code><\/pre>\n<p>\n\t&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-692","post","type-post","status-publish","format-standard","hentry","category-command-line-uwp-programming-in-c"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/692","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=692"}],"version-history":[{"count":0,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/692\/revisions"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=692"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=692"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=692"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}