我正在寻找在我的网络应用程序中使用“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 ?
有人知道吗?
谢谢,
答案 0 :(得分:0)
您可以尝试使用此命令连接字符
Dim _info As New ProcessStartInfo("cmd", "/C C: & CD C:\TEMP & DIR")
答案 1 :(得分:0)
为什么这么复杂?你可以使用这个单行:
string[] filePaths = Directory.GetFiles(@"c:\MyDir\", "*.bmp");
获取给定目录中所有文件的数组(在本例中为所有.bmp文件)。扩展参数是可选的。