{"id":179,"date":"2025-12-23T15:13:32","date_gmt":"2025-12-23T15:13:32","guid":{"rendered":"https:\/\/blogs.giamkichsan.com\/?p=179"},"modified":"2026-01-03T06:33:22","modified_gmt":"2026-01-03T06:33:22","slug":"179","status":"publish","type":"post","link":"https:\/\/blogs.giamkichsan.com\/index.php\/2025\/12\/23\/179\/","title":{"rendered":"C\u00e1ch ch\u1ea1y l\u1ec7nh cmd tr\u00ean c#"},"content":{"rendered":"\n<p>B\u1ea1n c\u00f3 th\u1ec3 <strong>g\u1ecdi l\u1ec7nh CMD trong C#<\/strong> b\u1eb1ng c\u00e1ch d\u00f9ng l\u1edbp <strong><code>System.Diagnostics.Process<\/code><\/strong>. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c c\u00e1ch ph\u1ed5 bi\u1ebfn (t\u1eeb \u0111\u01a1n gi\u1ea3n \u2192 n\u00e2ng cao).<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. Ch\u1ea1y 1 l\u1ec7nh CMD \u0111\u01a1n gi\u1ea3n (kh\u00f4ng c\u1ea7n l\u1ea5y k\u1ebft qu\u1ea3)<\/h2>\n\n\n\n<p>V\u00ed d\u1ee5: ch\u1ea1y l\u1ec7nh <code>ipconfig<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Diagnostics;\n\nProcess.Start(\"cmd.exe\", \"\/c ipconfig\");\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 <code>\/c<\/code> ngh\u0129a l\u00e0: ch\u1ea1y l\u1ec7nh xong th\u00ec <strong>\u0111\u00f3ng CMD<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Ch\u1ea1y l\u1ec7nh CMD v\u00e0 <strong>l\u1ea5y k\u1ebft qu\u1ea3 tr\u1ea3 v\u1ec1<\/strong><\/h2>\n\n\n\n<p>V\u00ed d\u1ee5: l\u1ea5y k\u1ebft qu\u1ea3 c\u1ee7a <code>dir<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Diagnostics;\n\nclass Program\n{\n    static void Main()\n    {\n        Process p = new Process();\n        p.StartInfo.FileName = \"cmd.exe\";\n        p.StartInfo.Arguments = \"\/c dir\";\n        p.StartInfo.RedirectStandardOutput = true;\n        p.StartInfo.UseShellExecute = false;\n        p.StartInfo.CreateNoWindow = true;\n\n        p.Start();\n\n        string output = p.StandardOutput.ReadToEnd();\n        p.WaitForExit();\n\n        Console.WriteLine(output);\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. Ch\u1ea1y nhi\u1ec1u l\u1ec7nh CMD c\u00f9ng l\u00fac<\/h2>\n\n\n\n<p>D\u00f9ng <code>&amp;&amp;<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Process.Start(\"cmd.exe\", \"\/c cd C:\\\\ &amp;&amp; dir\");\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. M\u1edf CMD v\u00e0 <strong>gi\u1eef c\u1eeda s\u1ed5 kh\u00f4ng t\u1eaft<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Process.Start(\"cmd.exe\");\n<\/code><\/pre>\n\n\n\n<p>Ho\u1eb7c:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Process.Start(\"cmd.exe\", \"\/k dir\");\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 <code>\/k<\/code> = ch\u1ea1y l\u1ec7nh nh\u01b0ng <strong>kh\u00f4ng \u0111\u00f3ng CMD<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Ch\u1ea1y file <code>.bat<\/code><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Process.Start(\"cmd.exe\", \"\/c test.bat\");\n<\/code><\/pre>\n\n\n\n<p>Ho\u1eb7c tr\u1ef1c ti\u1ebfp:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Process.Start(\"test.bat\");\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">L\u01b0u \u00fd quan tr\u1ecdng \u26a0\ufe0f<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>N\u1ebfu ch\u01b0\u01a1ng tr\u00ecnh <strong>c\u1ea7n quy\u1ec1n admin<\/strong>, b\u1ea1n ph\u1ea3i ch\u1ea1y app C# b\u1eb1ng quy\u1ec1n Administrator.<\/li>\n\n\n\n<li>Kh\u00f4ng n\u00ean ch\u1ea1y l\u1ec7nh CMD t\u1eeb input ng\u01b0\u1eddi d\u00f9ng n\u1ebfu kh\u00f4ng ki\u1ec3m so\u00e1t (d\u1ec5 l\u1ed7i &amp; m\u1ea5t an to\u00e0n).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>N\u1ebfu b\u1ea1n mu\u1ed1n:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ch\u1ea1y CMD <strong>ng\u1ea7m<\/strong><\/li>\n\n\n\n<li>ch\u1ea1y <strong>PowerShell<\/strong><\/li>\n\n\n\n<li>ho\u1eb7c ch\u1ea1y trong <strong>WinForms \/ WPF<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udc49 n\u00f3i m\u00ecnh bi\u1ebft b\u1ea1n \u0111ang d\u00f9ng lo\u1ea1i project n\u00e0o nh\u00e9 \ud83d\udc4d<\/p>\n\n\n\n<p>Toi muon an cmd duoc khong<\/p>\n\n\n\n<p>C\u00f3 nh\u00e9 \ud83d\udc4d <strong>B\u1ea1n ho\u00e0n to\u00e0n c\u00f3 th\u1ec3 \u1ea9n c\u1eeda s\u1ed5 CMD khi g\u1ecdi l\u1ec7nh t\u1eeb C#<\/strong>.<\/p>\n\n\n\n<p>D\u01b0\u1edbi \u0111\u00e2y l\u00e0 <strong>c\u00e1ch chu\u1ea9n v\u00e0 hay d\u00f9ng nh\u1ea5t<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 \u1ea8n CMD khi ch\u1ea1y l\u1ec7nh (khuy\u1ebfn ngh\u1ecb)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Diagnostics;\n\nProcess p = new Process();\np.StartInfo.FileName = \"cmd.exe\";\np.StartInfo.Arguments = \"\/c ipconfig\"; \/\/ l\u1ec7nh CMD\np.StartInfo.CreateNoWindow = true;      \/\/ \u1ea8N CMD\np.StartInfo.UseShellExecute = false;    \/\/ B\u1eaeT BU\u1ed8C \u0111\u1ec3 \u1ea9n\n\np.Start();\np.WaitForExit();\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 CMD s\u1ebd ch\u1ea1y <strong>ng\u1ea7m<\/strong>, ng\u01b0\u1eddi d\u00f9ng <strong>kh\u00f4ng th\u1ea5y g\u00ec c\u1ea3<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 \u1ea8n CMD + l\u1ea5y k\u1ebft qu\u1ea3<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Diagnostics;\n\nProcess p = new Process();\np.StartInfo.FileName = \"cmd.exe\";\np.StartInfo.Arguments = \"\/c dir\";\np.StartInfo.CreateNoWindow = true;\np.StartInfo.UseShellExecute = false;\np.StartInfo.RedirectStandardOutput = true;\n\np.Start();\nstring output = p.StandardOutput.ReadToEnd();\np.WaitForExit();\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u274c C\u00e1ch KH\u00d4NG \u1ea9n \u0111\u01b0\u1ee3c CMD<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>Process.Start(\"cmd.exe\", \"\/c dir\");\n<\/code><\/pre>\n\n\n\n<p>\u26a0\ufe0f V\u00ec m\u1eb7c \u0111\u1ecbnh <code>UseShellExecute = true<\/code> \u2192 CMD s\u1ebd hi\u1ec7n ra.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">C\u00c1CH 1 (NHANH \u2013 PH\u1ed4 BI\u1ebeN): Ch\u1ea1y ng\u1ea7m b\u1eb1ng Process (gi\u1ed1ng service)<\/h1>\n\n\n\n<p>\ud83d\udc49 Ph\u00f9 h\u1ee3p n\u1ebfu:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>App \u0111\u00e3 l\u00e0 <strong>Worker \/ Web API<\/strong><\/li>\n\n\n\n<li>Kh\u00f4ng c\u1ea7n t\u00edch h\u1ee3p s\u00e2u v\u1edbi Windows Service<\/li>\n\n\n\n<li>Mu\u1ed1n <strong>start \/ stop b\u1eb1ng C#<\/strong><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udd39 Code C# ch\u1ea1y ng\u1ea7m<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Diagnostics;\n\nProcess serviceProcess;\n\nvoid StartService()\n{\n    serviceProcess = new Process();\n    serviceProcess.StartInfo.FileName = \"dotnet\";\n    serviceProcess.StartInfo.Arguments = \"Storage.GiamKichSan.dll\";\n    serviceProcess.StartInfo.WorkingDirectory = @\"C:\\App\\Storage\";\n    serviceProcess.StartInfo.UseShellExecute = false;\n    serviceProcess.StartInfo.CreateNoWindow = true;\n    serviceProcess.StartInfo.RedirectStandardOutput = true;\n    serviceProcess.StartInfo.RedirectStandardError = true;\n\n    serviceProcess.Start();\n}\n\nvoid StopService()\n{\n    if (serviceProcess != null &amp;&amp; !serviceProcess.HasExited)\n    {\n        serviceProcess.Kill(); \/\/ d\u1eebng service\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\ud83d\udccc \u1ee8ng d\u1ee5ng s\u1ebd:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>ch\u1ea1y <strong>ng\u1ea7m<\/strong><\/li>\n\n\n\n<li><strong>kh\u00f4ng hi\u1ec7n CMD<\/strong><\/li>\n\n\n\n<li>ch\u1ea1y <strong>li\u00ean t\u1ee5c<\/strong> cho t\u1edbi khi b\u1ecb kill<\/li>\n<\/ul>\n\n\n\n<p>\u26a0\ufe0f Nh\u01b0\u1ee3c \u0111i\u1ec3m:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Kh\u00f4ng t\u1ef1 ch\u1ea1y khi Windows boot<\/li>\n\n\n\n<li>Kh\u00f4ng th\u1ea5y trong <code>services.msc<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 C\u00c1CH 2 (CHU\u1ea8N WINDOWS): Ch\u1ea1y nh\u01b0 <strong>Windows Service th\u1eadt<\/strong><\/h1>\n\n\n\n<p>\ud83d\udc49 <strong>Khuy\u1ebfn ngh\u1ecb n\u1ebfu ch\u1ea1y l\u00e2u d\u00e0i \/ production<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 B\u01b0\u1edbc 1: App ph\u1ea3i l\u00e0 Worker Service<\/h2>\n\n\n\n<p>N\u1ebfu <code>Storage.GiamKichSan.dll<\/code> l\u00e0 <strong>Worker Service (.NET)<\/strong> \u2192 OK lu\u00f4n<br>N\u1ebfu ch\u01b0a, c\u1ea7n t\u1ea1o d\u1ea1ng Worker:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dotnet new worker -n Storage.GiamKichSan\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 B\u01b0\u1edbc 2: Cho ph\u00e9p ch\u1ea1y nh\u01b0 Windows Service<\/h2>\n\n\n\n<p>Trong <code>Program.cs<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using Microsoft.Extensions.Hosting;\n\nHost.CreateDefaultBuilder(args)\n    .UseWindowsService()\n    .ConfigureServices(services =&gt;\n    {\n        services.AddHostedService&lt;Worker&gt;();\n    })\n    .Build()\n    .Run();\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 B\u01b0\u1edbc 3: Publish<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>dotnet publish -c Release -o C:\\Services\\Storage\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 B\u01b0\u1edbc 4: T\u1ea1o Windows Service<\/h2>\n\n\n\n<p>M\u1edf CMD <strong>Run as Administrator<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sc create StorageGiamKichSan binPath= \"dotnet C:\\Services\\Storage\\Storage.GiamKichSan.dll\"\n<\/code><\/pre>\n\n\n\n<p>Start service:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sc start StorageGiamKichSan\n<\/code><\/pre>\n\n\n\n<p>Stop service:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><\/pre>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>B\u1ea1n c\u00f3 th\u1ec3 g\u1ecdi l\u1ec7nh CMD trong C# b\u1eb1ng c\u00e1ch d\u00f9ng l\u1edbp System.Diagnostics.Process. D\u01b0\u1edbi \u0111\u00e2y l\u00e0 c\u00e1c c\u00e1ch ph\u1ed5 bi\u1ebfn (t\u1eeb \u0111\u01a1n gi\u1ea3n \u2192 n\u00e2ng <a class=\"mh-excerpt-more\" href=\"https:\/\/blogs.giamkichsan.com\/index.php\/2025\/12\/23\/179\/\" title=\"C\u00e1ch ch\u1ea1y l\u1ec7nh cmd tr\u00ean c#\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-179","post","type-post","status-publish","format-standard","hentry","category-netcore-lap-trinh-netframework"],"_links":{"self":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/comments?post=179"}],"version-history":[{"count":2,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions"}],"predecessor-version":[{"id":189,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/posts\/179\/revisions\/189"}],"wp:attachment":[{"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/media?parent=179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/categories?post=179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.giamkichsan.com\/index.php\/wp-json\/wp\/v2\/tags?post=179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}