System () in C # without calling cmd.exe

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

+3
source share
6 answers

As other people have noted, it is Process.Start . Example:

using System.Diagnostics;

// ...

Process.Start(@"C:\myapp\foo.exe");
+3
source

If I understand your question correctly, you are looking for Process.Start .

See this example (from the docs):

// Opens urls and .html documents using Internet Explorer.
void OpenWithArguments()
{
    // url 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 need something like the "dir" command, I would suggest you take a look at DirectoryInfo . You can use it to create your own directory listing. For example (also from documents):

// 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);
}
+6

, . Process.Start?

+2
I need to throw something like "dir"

DIR, cmd.exe, dir cmd.exe

+2

? , .

, copy #, File.Copy, dir Directory ( 2 ). , , #, , / , , , , .

"invoke this" - , , , , System.Diagnostics.Process .

+1
source

If you want to execute a command line command (cmd.exe), for example, "dir" or "time" or "mkdir", pass the command as an argument to cmd.exe with the / C flag.

For instance,

cmd.exe /C dir

or

cmd.exe /C mkdir "New Dir"
0
source

All Articles