Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,899 questions

51,830 answers

573 users

How to call a Windows shell command using C#

2 Answers

0 votes
class Program
{
    static void Main()
    {
        System.Diagnostics.Process.Start("cmd.exe", "/c dir");
    }
}



/*
run:

 Volume in drive C has no label.
 Volume Serial Number is 97QZ-OIE1

 Directory of C:\C#_ConsoleApp1\bin\Debug\net7.0

01/08/2026  10:50 AM    <DIR>          .
01/08/2026  10:50 AM    <DIR>          ..
10/31/2025  09:22 AM               434 C#_ConsoleApp1.deps.json
01/08/2026  10:50 AM             5,120 C#_ConsoleApp1.dll
01/08/2026  10:50 AM           154,624 C#_ConsoleApp1.exe
01/08/2026  10:50 AM            10,464 C#_ConsoleApp1.pdb
10/31/2025  09:22 AM               147 C#_ConsoleApp1.runtimeconfig.json
               5 File(s)        170,789 bytes
               2 Dir(s)  324,851,265,536 bytes free

*/

 



answered Jan 8 by avibootz
edited Jan 8 by avibootz
0 votes
using System.Diagnostics;

class Program
{
    public static string RunCommand(string command)
    {
        var startInfo = new ProcessStartInfo
        {
            FileName = "cmd.exe",
            Arguments = $"/c {command}",
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using (var process = new Process { StartInfo = startInfo })
        {
            process.Start();

            string output = process.StandardOutput.ReadToEnd();
            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();

            return string.IsNullOrEmpty(error) ? output : $"Error: {error}";
        }
    }
    static void Main()
    {
        string result = RunCommand("dir"); 
        
        Console.WriteLine(result);
    }
}



/*
run:

 Volume in drive C has no label.
 Volume Serial Number is 97QZ-OIE1

 Directory of C:\C#_ConsoleApp1\bin\Debug\net7.0

01/08/2026  10:50 AM    <DIR>          .
01/08/2026  10:50 AM    <DIR>          ..
10/31/2025  09:22 AM               434 C#_ConsoleApp1.deps.json
01/08/2026  10:50 AM             5,120 C#_ConsoleApp1.dll
01/08/2026  10:50 AM           154,624 C#_ConsoleApp1.exe
01/08/2026  10:50 AM            10,464 C#_ConsoleApp1.pdb
10/31/2025  09:22 AM               147 C#_ConsoleApp1.runtimeconfig.json
               5 File(s)        170,789 bytes
               2 Dir(s)  324,851,265,536 bytes free

*/

 



answered Jan 8 by avibootz

Related questions

1 answer 112 views
1 answer 142 views
2 answers 171 views
171 views asked Sep 25, 2019 by avibootz
...