我能不能在程序中调用dos命令,并截取dos操作的信息呢????

解决方案 »

  1.   

    用System.Diagnostics.Process这个类
      

  2.   

    使用System.Diagnostics.Process结合管道。
    我现在手头上只有Delphi的代码,楼主可以作下参考。帮助不大也就算了。unit uMain;interfaceuses Windows, SysUtils;function get_pipe_result(const cmd: PChar; ret: PChar): Integer; stdcall; export;implementationfunction get_pipe_result(const cmd: PChar; ret: PChar): Integer;
    const
        ReadBuffer = 2400;
    var
        Security: TSecurityAttributes;
        ReadPipe, WritePipe: THandle;
        start: TStartUpInfo;
        ProcessInfo: TProcessInformation;
        Buffer: PChar;
        BytesRead: DWord;
        Buf: string;
        retStr: string;
    begin
        with Security do begin
            nlength := SizeOf(TSecurityAttributes);
            binherithandle := true;
            lpsecuritydescriptor := nil;
        end;    retStr := '';     if Createpipe(ReadPipe, WritePipe, @Security, 0) then begin
            Buffer := AllocMem(ReadBuffer + 1);
            FillChar(Start, Sizeof(Start), #0);         with start do begin
                cb := SizeOf(start);
                start.lpReserved := nil;
                lpDesktop := nil;
                lpTitle := nil;
                dwX := 0;
                dwY := 0;
                dwXSize := 0;
                dwYSize := 0;
                dwXCountChars := 0;
                dwYCountChars := 0;
                dwFillAttribute := 0;
                cbReserved2 := 0;
                lpReserved2 := nil;
                hStdOutput := WritePipe;
                hStdInput := ReadPipe; 
                hStdError := WritePipe;
                dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
                wShowWindow := SW_HIDE;
            end;         try             if CreateProcess(nil, cmd, @Security, @Security, true,
                    NORMAL_PRIORITY_CLASS,
                    nil, nil, start, ProcessInfo) then begin                 WaitForSingleObject(ProcessInfo.hProcess, INFINITE);                 CloseHandle(WritePipe);
                    Buf := '';                 repeat
                        BytesRead := 0;
                        ReadFile(ReadPipe, Buffer[0], ReadBuffer, BytesRead, nil);
                        Buffer[BytesRead] := #0;
                        OemToAnsi(Buffer, Buffer);
                        Buf := Buf + string(Buffer);
                    until (BytesRead < ReadBuffer);                 while pos(#10, Buf) > 0 do begin
                        retStr := retStr + Copy(Buf, 1, pos(#10, Buf));
                        Delete(Buf, 1, pos(#10, Buf));
                    end;
                end;
            finally
                FreeMem(Buffer);
                CloseHandle(ProcessInfo.hProcess);
                CloseHandle(ProcessInfo.hThread);
                CloseHandle(ReadPipe);
            end;
            StrPCopy(ret, retStr);
            Result := 0;
            Exit;
        end;
        StrPCopy(ret, '');
        Result := -1;
    end;end.
      

  3.   

    谢谢回复。我也调用了Diagnostics.Process,但是dos出来后不能在上面操作,我想在上面操作并接受到操作的信息.???
    @acqy 谢谢你,是不怎么看懂  555
      

  4.   

    我估计.NET下面不要像delphi那么复杂。你这样试试,不知道是不是你要的:static void Main(string[] args)
    {
        ProcessStartInfo info = new ProcessStartInfo("cmd.exe", "/c \"dir c:\\*.*\"");
        info.CreateNoWindow = true;
        info.RedirectStandardOutput = true;
        info.UseShellExecute = false;
        Process p = Process.Start(info);
        StreamReader reader = p.StandardOutput;
        string l = reader.ReadToEnd();
        Console.WriteLine(l);
    }
      

  5.   

    PS:如果你需要重定位输入,那么你在上面的代码中,需要将info.RedirectStandardInput为true,然后设置p的StandardInput属性。
      

  6.   

    先谢谢回复!!!!System.Diagnostics.Process p=new Process();
    p.StartInfo.FileName="cmd.exe";
    p.StartInfo.UseShellExecute=false;
    p.StartInfo.RedirectStandardInput=true;
    p.StartInfo.RedirectStandardOutput=true;
    p.Start();
    p.StandardInput.WriteLine("dir");
    p.StandardInput.WriteLine("exit");
    p.WaitForExit();
    textBox1.Text=p.StandardOutput.ReadToEnd();我是这样写的。但是这样输入的dos窗口不能够输入,只是StandardInput,来固定输入,我现在是想我直接在起
    动的dos窗口里面自己输入,这样可以吗???
      

  7.   

    我把上面我的程序改成这样没有问题啊:static void Main(string[] args)
    {
        ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
        info.CreateNoWindow = true;
        info.RedirectStandardOutput = true;
        info.UseShellExecute = false;
        info.RedirectStandardInput = true;
        Process p = Process.Start(info);
        StreamReader reader = p.StandardOutput;
        p.StandardInput.WriteLine("dir");
        p.StandardInput.WriteLine("exit");
        string l = reader.ReadToEnd();
        Console.WriteLine(l);
      

  8.   

    哦,你要自己输入,那么你就先用Console.ReadLine读入用户的输入,然后再给p.StandardInput.WriteLine不就可以了么?
      

  9.   


     System.Diagnostics.Process p=new Process();
    p.StartInfo.FileName="cmd.exe";
    p.StartInfo.UseShellExecute=false;
    p.StartInfo.RedirectStandardInput=true;
    p.StartInfo.RedirectStandardOutput=true;
    p.Start();
        string tempstr=Console.ReadLine();
    p.StandardInput.WriteLine(tempstr);怎么不行!!!输入为null,根本没有第二个窗口