1

Web アプリで「dir」コマンドを使用してコマンド プロンプトを実行し、応答を読み取ろうとしています...これは機能していますが、最初に別のコマンドを実行する必要があります。適切なディレクトリに。

基本的にこのリンクを使用して、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

なぜそんなに複雑なのですか?このワンライナーを使用できます:

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

指定されたディレクトリ内のすべてのファイル (この場合はすべての .bmp ファイル) の配列を取得します。拡張パラメーターはオプションです。

于 2013-06-26T10:04:01.020 に答える