{"id":1328,"date":"2024-06-19T12:48:21","date_gmt":"2024-06-19T11:48:21","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=1328"},"modified":"2024-06-19T17:24:25","modified_gmt":"2024-06-19T16:24:25","slug":"executing-command-line-commands-2","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/command-line-c-sharp\/executing-command-line-commands-2","title":{"rendered":"Executing Command Line Commands"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Executing Command Line Strings Programatically<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/*************************************************\n\t\/\/*************************************************\n\t\/\/********** EXECUTE COMMAND LINE STRING **********\n\t\/\/*************************************************\n\t\/\/*************************************************\n\tprivate string ExecuteCommandLineString(string CommandString)\n\t{\n\n\t\ttry\n\t\t{\n\t\t\tSystem.Diagnostics.Process Process1 = new System.Diagnostics.Process();\n\t\t\tSystem.Diagnostics.ProcessStartInfo StartInfo1 = new System.Diagnostics.ProcessStartInfo();\n\n\t\t\tStartInfo1.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;\n\t\t\tStartInfo1.FileName = \"cmd.exe\";\n\t\t\tStartInfo1.Arguments = \"\/C \" + CommandString;\n\t\t\tStartInfo1.RedirectStandardOutput = true;\n\t\t\tStartInfo1.UseShellExecute = false;\n\t\t\tProcess1.StartInfo = StartInfo1;\n\t\t\tProcess1.Start();\n\t\t\t\/\/Do not wait for the child process to exit before reading to the end of its redirected stream.\n\t\t\t\/\/p.WaitForExit();\n\t\t\t\/\/Read the output stream first and then wait.\n\t\t\tstring Output = Process1.StandardOutput.ReadToEnd();\n\t\t\tProcess1.WaitForExit();\n\t\t\t\t\n\t\t\treturn (Output);\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\treturn(\"ERROR - \" + ex.Message + \"\\n\");\n\t\t}\n\n\t}<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Example usage copying a file<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tstring Command = \"xcopy \\\"C:\\\\My Folder\\\\HelloWorld.txt\\\" \\\"C:\\\\My Folder\\\\HelloWorld2.txt\\\" \/-I \/Y \";\n\tstring Output = ExecuteCommandLineString(Command);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Executing Command Line Strings Programatically Example usage copying a file<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[154],"tags":[],"class_list":["post-1328","post","type-post","status-publish","format-standard","hentry","category-command-line-c-sharp"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1328","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=1328"}],"version-history":[{"count":3,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1328\/revisions"}],"predecessor-version":[{"id":1337,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/1328\/revisions\/1337"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=1328"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=1328"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=1328"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}