system()到c#而不调用cmd.exe

use*_*823 4 c c# cmd

如何在不调用cmd.exe的情况下将系统("")转换为C#?编辑:我需要抛出像"dir"这样的东西

Fer*_*ndo 7

如果我正确理解了您的问题,那么您正在寻找Process.Start.

请参阅此示例(来自文档):

// 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");
 }
Run Code Online (Sandbox Code Playgroud)

编辑

正如你所说,你需要类似"dir"命令的东西,我建议你看一下DirectoryInfo.您可以使用它来创建自己的目录列表.例如(也来自文档):

// 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);
}
Run Code Online (Sandbox Code Playgroud)

  • 并且您应该首先在原始C应用程序中避免使用system(). (3认同)

Joh*_*lla 5

正如其他人指出的那样,它是Process.Start。例子:

using System.Diagnostics;

// ...

Process.Start(@"C:\myapp\foo.exe");
Run Code Online (Sandbox Code Playgroud)