Bạn có thể gọi lệnh CMD trong C# bằng cách dùng lớp System.Diagnostics.Process. Dưới đây là các cách phổ biến (từ đơn giản → nâng cao).
1. Chạy 1 lệnh CMD đơn giản (không cần lấy kết quả)
Ví dụ: chạy lệnh ipconfig
using System.Diagnostics;
Process.Start("cmd.exe", "/c ipconfig");
👉 /c nghĩa là: chạy lệnh xong thì đóng CMD.
2. Chạy lệnh CMD và lấy kết quả trả về
Ví dụ: lấy kết quả của dir
using System;
using System.Diagnostics;
class Program
{
static void Main()
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c dir";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
}
}
3. Chạy nhiều lệnh CMD cùng lúc
Dùng &&:
Process.Start("cmd.exe", "/c cd C:\\ && dir");
4. Mở CMD và giữ cửa sổ không tắt
Process.Start("cmd.exe");
Hoặc:
Process.Start("cmd.exe", "/k dir");
👉 /k = chạy lệnh nhưng không đóng CMD.
5. Chạy file .bat
Process.Start("cmd.exe", "/c test.bat");
Hoặc trực tiếp:
Process.Start("test.bat");
Lưu ý quan trọng ⚠️
- Nếu chương trình cần quyền admin, bạn phải chạy app C# bằng quyền Administrator.
- Không nên chạy lệnh CMD từ input người dùng nếu không kiểm soát (dễ lỗi & mất an toàn).
Nếu bạn muốn:
- chạy CMD ngầm
- chạy PowerShell
- hoặc chạy trong WinForms / WPF
👉 nói mình biết bạn đang dùng loại project nào nhé 👍
Toi muon an cmd duoc khong
Có nhé 👍 Bạn hoàn toàn có thể ẩn cửa sổ CMD khi gọi lệnh từ C#.
Dưới đây là cách chuẩn và hay dùng nhất.
✅ Ẩn CMD khi chạy lệnh (khuyến nghị)
using System.Diagnostics;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c ipconfig"; // lệnh CMD
p.StartInfo.CreateNoWindow = true; // ẨN CMD
p.StartInfo.UseShellExecute = false; // BẮT BUỘC để ẩn
p.Start();
p.WaitForExit();
👉 CMD sẽ chạy ngầm, người dùng không thấy gì cả.
✅ Ẩn CMD + lấy kết quả
using System.Diagnostics;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c dir";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
❌ Cách KHÔNG ẩn được CMD
Process.Start("cmd.exe", "/c dir");
⚠️ Vì mặc định UseShellExecute = true → CMD sẽ hiện ra.
CÁCH 1 (NHANH – PHỔ BIẾN): Chạy ngầm bằng Process (giống service)
👉 Phù hợp nếu:
- App đã là Worker / Web API
- Không cần tích hợp sâu với Windows Service
- Muốn start / stop bằng C#
🔹 Code C# chạy ngầm
using System.Diagnostics;
Process serviceProcess;
void StartService()
{
serviceProcess = new Process();
serviceProcess.StartInfo.FileName = "dotnet";
serviceProcess.StartInfo.Arguments = "Storage.GiamKichSan.dll";
serviceProcess.StartInfo.WorkingDirectory = @"C:\App\Storage";
serviceProcess.StartInfo.UseShellExecute = false;
serviceProcess.StartInfo.CreateNoWindow = true;
serviceProcess.StartInfo.RedirectStandardOutput = true;
serviceProcess.StartInfo.RedirectStandardError = true;
serviceProcess.Start();
}
void StopService()
{
if (serviceProcess != null && !serviceProcess.HasExited)
{
serviceProcess.Kill(); // dừng service
}
}
📌 Ứng dụng sẽ:
- chạy ngầm
- không hiện CMD
- chạy liên tục cho tới khi bị kill
⚠️ Nhược điểm:
- Không tự chạy khi Windows boot
- Không thấy trong
services.msc
✅ CÁCH 2 (CHUẨN WINDOWS): Chạy như Windows Service thật
👉 Khuyến nghị nếu chạy lâu dài / production
🔹 Bước 1: App phải là Worker Service
Nếu Storage.GiamKichSan.dll là Worker Service (.NET) → OK luôn
Nếu chưa, cần tạo dạng Worker:
dotnet new worker -n Storage.GiamKichSan
🔹 Bước 2: Cho phép chạy như Windows Service
Trong Program.cs:
using Microsoft.Extensions.Hosting;
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.Build()
.Run();
🔹 Bước 3: Publish
dotnet publish -c Release -o C:\Services\Storage
🔹 Bước 4: Tạo Windows Service
Mở CMD Run as Administrator:
sc create StorageGiamKichSan binPath= "dotnet C:\Services\Storage\Storage.GiamKichSan.dll"
Start service:
sc start StorageGiamKichSan
Stop service:
Để lại một phản hồi