Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

system() to c# without calling cmd.exe

Tags:

c

c#

cmd

how to translate system("") to C# without calling cmd.exe? edit: i need to throw something like "dir"

like image 200
user302823 Avatar asked Dec 05 '22 02:12

user302823


2 Answers

If I correctly understood your question, you're looking for Process.Start.

See this example (from the docs):

// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
{
    // url's are not considered documents. They can only be opened
    // by passing them as arguments.
    Process.Start("IExplore.exe", "www.northwindtraders.com");

    // Start a Web page using a browser associated with .html and .asp files.
    Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm");
    Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp");
 }

Edit

As you said you needed something like the "dir" command, I would suggest you to take a look at DirectoryInfo. You can use it to create your own directory listing. For example (also from the docs):

// Create a DirectoryInfo of the directory of the files to enumerate.
DirectoryInfo DirInfo = new DirectoryInfo(@"\\archives1\library");

DateTime StartOf2009 = new DateTime(2009, 01, 01);

// LINQ query for all files created before 2009.
var files = from f in DirInfo.EnumerateFiles()
           where DirInfo.CreationTimeUtc < StartOf2009
           select f;

// Show results.
foreach (var f in files)
{
    Console.WriteLine("{0}", f.Name);
}
like image 183
Fernando Avatar answered Dec 07 '22 23:12

Fernando


As other folks noted, it's Process.Start. Example:

using System.Diagnostics;

// ...

Process.Start(@"C:\myapp\foo.exe");
like image 40
John Feminella Avatar answered Dec 08 '22 01:12

John Feminella