2013-06-26 73 views
1

我正在運行帶有「dir」命令的命令提示符並在我的web應用程序中讀取響應...我正在工作但我需要先執行另一個命令,即我需要使用CD才能到達正確的目錄。通過asp.net應用程序通過多個命令行克服cmd提示符

我可以得到一個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

你可以用這個命令連接字符

Dim _info As New ProcessStartInfo("cmd", "/C C: & CD C:\TEMP & DIR") 
0

爲什麼這麼複雜試試?您可以使用此一班輪:

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

在給定目錄中獲取所有文件的數組(在這種情況下,所有的.bmp文件)。擴展參數是可選的。

+0

我只是把這個問題作爲一個簡單的例子。我需要運行一個位於我的Web目錄中的文件夾中的可執行文件,但我遇到各種安全問題。所以我在想的是啓動一個命令提示符進程,運行可執行文件(不是做一個dir)。 – thegunner

+0

哦,好的!關於將命令的輸出發送到文件中,例如「dir/s> output.txt」並在執行後讀取/解析該文件? – Robert

+0

那麼它看起來像史蒂夫的答案的作品。我主持這與託管公司,它看起來像網站文件夾是在E:\ Domains \ s \ www.mywebsite.com \用戶\ htdocs \,我似乎無法CD到....所以這可能都是完全錯誤的方法。 – thegunner