{"id":529,"date":"2016-07-29T17:04:22","date_gmt":"2016-07-29T17:04:22","guid":{"rendered":"37f0e884fbad9667e38940169d0a3c95"},"modified":"2016-07-29T17:04:22","modified_gmt":"2016-07-29T17:04:22","slug":"","status":"publish","type":"post","link":"https:\/\/www.xiaobo.li\/notes\/archives\/529","title":{"rendered":"CommandLine Parse Code Fragment"},"content":{"rendered":"<p><b>Command Line Parse Code Fragment<\/b><\/p>\n<p>&nbsp;<\/p>\n<p><pre class=\"brush:c#; toolbar: true; auto-links: true;\">internal class CommandLine\r\n    {\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ get process command line info\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"processId\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;command line info, array length is 2, ex: [\"C:\\Windows\\system32\\svchost.exe\",\"-k LocalService\"]&lt;\/returns&gt;\r\n        public static String[] GetCommandLineInfo(int processId)\r\n        {\r\n            String[] info = new string[2];\r\n            info[0] = \"\";\r\n            info[1] = \"\";\r\n            \/\/\r\n            var commandLine = Wmi.GetCommandLine(processId);\r\n            if (commandLine != \"\")\r\n            {\r\n                info = ParseCommandLine(commandLine);\r\n            }\r\n            return info;\r\n        }\r\n\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ get process command line info\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"commandLine\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;command line info, array length is 2, ex: [\"C:\\Windows\\system32\\svchost.exe\",\"-k LocalService\"]&lt;\/returns&gt;\r\n        public static string[] ParseCommandLine(string commandLine)\r\n        {\r\n            string[] info = new String[2];\r\n\r\n            \/\/C:\\Windows\\system32\\svchost.exe -k LocalService\r\n            \/\/\"C:\\Program Files (x86)\\Avira\\AntiVir Desktop\\avgnt.exe\" \/min\r\n\r\n            if (commandLine[0] == '\"')\r\n            {\r\n                int end = commandLine.IndexOf('\"', 1);\r\n                if (end == -1)\r\n                {\r\n                    info[0] = commandLine;\r\n                    info[1] = \"\";\r\n                }\r\n                else if (end + 1 &lt; commandLine.Length)\r\n                {\r\n                    info[0] = commandLine.Substring(1, end + 1);\r\n                    info[1] = commandLine.Substring(end + 1).TrimStart(' ');\r\n                }\r\n                else\r\n                {\r\n                    info[0] = commandLine;\r\n                    info[1] = \"\";\r\n                }\r\n            }\r\n            else\r\n            {\r\n                int pos = commandLine.IndexOf(' ');\r\n                if (pos == -1)\r\n                {\r\n                    info[0] = commandLine;\r\n                    info[1] = \"\";\r\n                }\r\n                else if (pos + 1 &lt; commandLine.Length)\r\n                {\r\n                    info[0] = commandLine.Substring(0, pos);\r\n                    info[1] = commandLine.Substring(pos + 1).TrimStart(' ');\r\n                }\r\n                else\r\n                {\r\n                    info[0] = commandLine;\r\n                    info[1] = \"\";\r\n                }\r\n            }\r\n\r\n            return info;\r\n        }\r\n    }<\/pre>\n<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><b>Command Line Parse Code Fragment<\/b><\/p>\n<p>&nbsp;<\/p>\n<p><pre class=\"brush:c#; toolbar: true; auto-links: true;\">internal class CommandLine\r\n    {\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ get process command line info\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"processId\"&gt;&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;command line info, array length is 2, ex: [\"C:\\Windows\\system32\\svchost.exe\",\"-k LocalService\"]&lt;\/returns&gt;\r\n        public static String[] GetCommandLineInfo(int processId)\r\n        {\r\n            String[] info = new string[2];\r\n            info[0] = \"\";\r\n            info[1] = \"\";\r\n            \/\/\r\n            var command...<\/pre>\n<\/p>\n<p> <a href=\"https:\/\/www.xiaobo.li\/notes\/archives\/529\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[299],"tags":[],"class_list":["post-529","post","type-post","status-publish","format-standard","hentry","category-csharpcode"],"_links":{"self":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/529","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/comments?post=529"}],"version-history":[{"count":0,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/posts\/529\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/media?parent=529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/categories?post=529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xiaobo.li\/notes\/wp-json\/wp\/v2\/tags?post=529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}