1

我希望在我的网络应用程序中使用“dir”命令运行命令提示符并阅读响应...我已经开始工作但我需要先执行另一个命令,即我需要使用 CD 来获取到正确的目录。

我可以通过基本上使用此链接获得 DIR 响应并将其写入屏幕:

http://weblogs.asp.net/guystarbuck/archive/2008/02/06/invoking-cmd-exe-from-net.aspx

或者这里是代码:

        If Not IsPostBack Then


        Dim _info As New ProcessStartInfo("cmd", "/C " & "DIR")

        ' The following commands are needed to redirect the
        ' standard output.  This means that it will be redirected
        ' to the Process.StandardOutput StreamReader.
        _info.RedirectStandardOutput = True

        ' Set UseShellExecute to false.  This tells the process to run
        ' as a child of the invoking program, instead of on its own.
        ' This allows us to intercept and redirect the standard output.
        _info.UseShellExecute = False

        ' Set CreateNoWindow to true, to supress the creation of
        ' a new window
        _info.CreateNoWindow = True

        ' Create a process, assign its ProcessStartInfo and start it
        Dim _p As New Process()
        _p.StartInfo = _info
        _p.Start()

        ' Capture the results in a string
        Dim _processResults As String = _p.StandardOutput.ReadToEnd()

        ' Close the process to release system resources
        _p.Close()

        ' Return the output stream to the caller
        Response.Write(_processResults)


    End If

麻烦它在错误的目录中......我怎样才能先做一个CD otherdir

有人知道吗?

谢谢,

4

2 回答 2

0

您可以尝试使用此命令连接字符

    Dim _info As New ProcessStartInfo("cmd", "/C C: & CD C:\TEMP & DIR")
于 2013-06-26T10:02:04.930 回答
0

为什么这么复杂?您可以使用此单线:

string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp");

获取给定目录中所有文件(在本例中为所有 .bmp 文件)的数组。扩展参数是可选的。

于 2013-06-26T10:04:01.020 回答