Process question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ask

    #1

    Process question

    Hi NG

    I'm a bit new to programming c# and have a question regarding ftp by the
    command prompt. As far as I can see it should be possible to start a process
    and pipe command streams into it. But I cant make it work, it always stalls
    when I try to read the output:

    /// code 1
    Process ftpProcess = new Process();

    ftpProcess.Star tInfo.FileName = "cmd";
    ftpProcess.Star tInfo.CreateNoW indow = true;
    ftpProcess.Star tInfo.RedirectS tandardInput = true;
    ftpProcess.Star tInfo.RedirectS tandardOutput = true;
    ftpProcess.Star tInfo.UseShellE xecute = false;

    ftpProcess.Star t();

    string output = ftpProcess.Stan dardOutput.Read ToEnd();
    string error = ftpProcess.Stan dardError.ReadT oEnd();

    ftpProcess.Stan dardInput.Write Line("ftp localhost");
    ftpProcess.Stan dardInput.Write Line();

    output = ftpProcess.Stan dardOutput.Read ToEnd();
    error = ftpProcess.Stan dardError.ReadT oEnd();

    ftpProcess.Stan dardInput.Write Line("username" );
    ftpProcess.Stan dardInput.Write Line();

    //... further ftp action - but I never get this far....

    output = ftpProcess.Stan dardOutput.Read ToEnd();
    error = ftpProcess.Stan dardError.ReadT oEnd();

    ftpProcess.Wait ForExit();
    ///////////

    I also tried:

    /// code2
    char [] output = new char [1000];
    string all = "";

    Process ftpProcess = new Process();
    ftpProcess.Star tInfo.FileName = "cmd";
    ftpProcess.Star tInfo.CreateNoW indow = true;
    ftpProcess.Star tInfo.RedirectS tandardInput = true;
    ftpProcess.Star tInfo.RedirectS tandardOutput = true;
    ftpProcess.Star tInfo.UseShellE xecute = false;
    ftpProcess.Star t();
    int i = ftpProcess.Stan dardOutput.Read (output, 0, 1000);
    ftpProcess.Stan dardInput.Write Line("ftp localhost");
    i = ftpProcess.Stan dardOutput.Read (output, i, 1000-i);
    ftpProcess.Stan dardInput.Write Line("username" );
    ftpProcess.Stan dardInput.Write Line("password" );
    ftpProcess.Stan dardInput.Write Line("mkdir test");
    ftpProcess.Stan dardInput.Write Line("ls");
    ftpProcess.Stan dardInput.Write Line("bye");
    all = ftpProcess.Stan dardOutput.Read ToEnd();


    thanx

    ps. And yes - I did connect manually by the command prompt so it should
    work.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Process question

    The reason it is hanging is that it is probably waiting for input of
    some kind (username probably).

    However, one has to ask, why not use a third party library to FTP, or
    the WinInet functions for FTP through the P/Invoke layer? It would be a
    much cleaner solution and give you much more control over the process.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nick(dot)paldin o=at=exisconsul ting<dot>com

    "ask" <a@creepREM.d k> wrote in message
    news:3f7180d8$0 $265$4d4eb98e@n ews.dk.uu.net.. .[color=blue]
    > Hi NG
    >
    > I'm a bit new to programming c# and have a question regarding ftp by the
    > command prompt. As far as I can see it should be possible to start a[/color]
    process[color=blue]
    > and pipe command streams into it. But I cant make it work, it always[/color]
    stalls[color=blue]
    > when I try to read the output:
    >
    > /// code 1
    > Process ftpProcess = new Process();
    >
    > ftpProcess.Star tInfo.FileName = "cmd";
    > ftpProcess.Star tInfo.CreateNoW indow = true;
    > ftpProcess.Star tInfo.RedirectS tandardInput = true;
    > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
    > ftpProcess.Star tInfo.UseShellE xecute = false;
    >
    > ftpProcess.Star t();
    >
    > string output = ftpProcess.Stan dardOutput.Read ToEnd();
    > string error = ftpProcess.Stan dardError.ReadT oEnd();
    >
    > ftpProcess.Stan dardInput.Write Line("ftp localhost");
    > ftpProcess.Stan dardInput.Write Line();
    >
    > output = ftpProcess.Stan dardOutput.Read ToEnd();
    > error = ftpProcess.Stan dardError.ReadT oEnd();
    >
    > ftpProcess.Stan dardInput.Write Line("username" );
    > ftpProcess.Stan dardInput.Write Line();
    >
    > //... further ftp action - but I never get this far....
    >
    > output = ftpProcess.Stan dardOutput.Read ToEnd();
    > error = ftpProcess.Stan dardError.ReadT oEnd();
    >
    > ftpProcess.Wait ForExit();
    > ///////////
    >
    > I also tried:
    >
    > /// code2
    > char [] output = new char [1000];
    > string all = "";
    >
    > Process ftpProcess = new Process();
    > ftpProcess.Star tInfo.FileName = "cmd";
    > ftpProcess.Star tInfo.CreateNoW indow = true;
    > ftpProcess.Star tInfo.RedirectS tandardInput = true;
    > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
    > ftpProcess.Star tInfo.UseShellE xecute = false;
    > ftpProcess.Star t();
    > int i = ftpProcess.Stan dardOutput.Read (output, 0, 1000);
    > ftpProcess.Stan dardInput.Write Line("ftp localhost");
    > i = ftpProcess.Stan dardOutput.Read (output, i, 1000-i);
    > ftpProcess.Stan dardInput.Write Line("username" );
    > ftpProcess.Stan dardInput.Write Line("password" );
    > ftpProcess.Stan dardInput.Write Line("mkdir test");
    > ftpProcess.Stan dardInput.Write Line("ls");
    > ftpProcess.Stan dardInput.Write Line("bye");
    > all = ftpProcess.Stan dardOutput.Read ToEnd();
    >
    >
    > thanx
    >
    > ps. And yes - I did connect manually by the command prompt so it should
    > work.
    >
    >[/color]


    Comment

    • Arild Bakken

      #3
      Re: Process question

      Hi,

      I've tried this with the FTP command myself using VBScripting, but never got
      it to work, so I don't think it's C# problem, but might be something with
      the FTP tool. What I ended up doing was to write a script that generated an
      FTP script, and then used the -s option of the ftp tools to read that script
      instead.


      Arild

      "ask" <a@creepREM.d k> wrote in message
      news:3f7180d8$0 $265$4d4eb98e@n ews.dk.uu.net.. .[color=blue]
      > Hi NG
      >
      > I'm a bit new to programming c# and have a question regarding ftp by the
      > command prompt. As far as I can see it should be possible to start a[/color]
      process[color=blue]
      > and pipe command streams into it. But I cant make it work, it always[/color]
      stalls[color=blue]
      > when I try to read the output:
      >
      > /// code 1
      > Process ftpProcess = new Process();
      >
      > ftpProcess.Star tInfo.FileName = "cmd";
      > ftpProcess.Star tInfo.CreateNoW indow = true;
      > ftpProcess.Star tInfo.RedirectS tandardInput = true;
      > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
      > ftpProcess.Star tInfo.UseShellE xecute = false;
      >
      > ftpProcess.Star t();
      >
      > string output = ftpProcess.Stan dardOutput.Read ToEnd();
      > string error = ftpProcess.Stan dardError.ReadT oEnd();
      >
      > ftpProcess.Stan dardInput.Write Line("ftp localhost");
      > ftpProcess.Stan dardInput.Write Line();
      >
      > output = ftpProcess.Stan dardOutput.Read ToEnd();
      > error = ftpProcess.Stan dardError.ReadT oEnd();
      >
      > ftpProcess.Stan dardInput.Write Line("username" );
      > ftpProcess.Stan dardInput.Write Line();
      >
      > //... further ftp action - but I never get this far....
      >
      > output = ftpProcess.Stan dardOutput.Read ToEnd();
      > error = ftpProcess.Stan dardError.ReadT oEnd();
      >
      > ftpProcess.Wait ForExit();
      > ///////////
      >
      > I also tried:
      >
      > /// code2
      > char [] output = new char [1000];
      > string all = "";
      >
      > Process ftpProcess = new Process();
      > ftpProcess.Star tInfo.FileName = "cmd";
      > ftpProcess.Star tInfo.CreateNoW indow = true;
      > ftpProcess.Star tInfo.RedirectS tandardInput = true;
      > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
      > ftpProcess.Star tInfo.UseShellE xecute = false;
      > ftpProcess.Star t();
      > int i = ftpProcess.Stan dardOutput.Read (output, 0, 1000);
      > ftpProcess.Stan dardInput.Write Line("ftp localhost");
      > i = ftpProcess.Stan dardOutput.Read (output, i, 1000-i);
      > ftpProcess.Stan dardInput.Write Line("username" );
      > ftpProcess.Stan dardInput.Write Line("password" );
      > ftpProcess.Stan dardInput.Write Line("mkdir test");
      > ftpProcess.Stan dardInput.Write Line("ls");
      > ftpProcess.Stan dardInput.Write Line("bye");
      > all = ftpProcess.Stan dardOutput.Read ToEnd();
      >
      >
      > thanx
      >
      > ps. And yes - I did connect manually by the command prompt so it should
      > work.
      >
      >[/color]


      Comment

      • Arild Bakken

        #4
        Re: Process question

        Hi,

        I've tried this with the FTP command myself using VBScripting, but never got
        it to work, so I don't think it's C# problem, but might be something with
        the FTP tool. What I ended up doing was to write a script that generated an
        FTP script, and then used the -s option of the ftp tools to read that script
        instead.


        Arild

        "ask" <a@creepREM.d k> wrote in message
        news:3f7180d8$0 $265$4d4eb98e@n ews.dk.uu.net.. .[color=blue]
        > Hi NG
        >
        > I'm a bit new to programming c# and have a question regarding ftp by the
        > command prompt. As far as I can see it should be possible to start a[/color]
        process[color=blue]
        > and pipe command streams into it. But I cant make it work, it always[/color]
        stalls[color=blue]
        > when I try to read the output:
        >
        > /// code 1
        > Process ftpProcess = new Process();
        >
        > ftpProcess.Star tInfo.FileName = "cmd";
        > ftpProcess.Star tInfo.CreateNoW indow = true;
        > ftpProcess.Star tInfo.RedirectS tandardInput = true;
        > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
        > ftpProcess.Star tInfo.UseShellE xecute = false;
        >
        > ftpProcess.Star t();
        >
        > string output = ftpProcess.Stan dardOutput.Read ToEnd();
        > string error = ftpProcess.Stan dardError.ReadT oEnd();
        >
        > ftpProcess.Stan dardInput.Write Line("ftp localhost");
        > ftpProcess.Stan dardInput.Write Line();
        >
        > output = ftpProcess.Stan dardOutput.Read ToEnd();
        > error = ftpProcess.Stan dardError.ReadT oEnd();
        >
        > ftpProcess.Stan dardInput.Write Line("username" );
        > ftpProcess.Stan dardInput.Write Line();
        >
        > //... further ftp action - but I never get this far....
        >
        > output = ftpProcess.Stan dardOutput.Read ToEnd();
        > error = ftpProcess.Stan dardError.ReadT oEnd();
        >
        > ftpProcess.Wait ForExit();
        > ///////////
        >
        > I also tried:
        >
        > /// code2
        > char [] output = new char [1000];
        > string all = "";
        >
        > Process ftpProcess = new Process();
        > ftpProcess.Star tInfo.FileName = "cmd";
        > ftpProcess.Star tInfo.CreateNoW indow = true;
        > ftpProcess.Star tInfo.RedirectS tandardInput = true;
        > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
        > ftpProcess.Star tInfo.UseShellE xecute = false;
        > ftpProcess.Star t();
        > int i = ftpProcess.Stan dardOutput.Read (output, 0, 1000);
        > ftpProcess.Stan dardInput.Write Line("ftp localhost");
        > i = ftpProcess.Stan dardOutput.Read (output, i, 1000-i);
        > ftpProcess.Stan dardInput.Write Line("username" );
        > ftpProcess.Stan dardInput.Write Line("password" );
        > ftpProcess.Stan dardInput.Write Line("mkdir test");
        > ftpProcess.Stan dardInput.Write Line("ls");
        > ftpProcess.Stan dardInput.Write Line("bye");
        > all = ftpProcess.Stan dardOutput.Read ToEnd();
        >
        >
        > thanx
        >
        > ps. And yes - I did connect manually by the command prompt so it should
        > work.
        >
        >[/color]


        Comment

        • Gang Peng

          #5
          Re: Process question

          This is a classic you will see if you use Process class incorrectly.
          You can read the document about Process class to understand why this
          happens.
          Always read the document :)
          In our next release, you will be able to get the output asynchrously.

          Gang Peng
          [MS]

          "ask" <a@creepREM.d k> wrote in message
          news:3f7180d8$0 $265$4d4eb98e@n ews.dk.uu.net.. .[color=blue]
          > Hi NG
          >
          > I'm a bit new to programming c# and have a question regarding ftp by the
          > command prompt. As far as I can see it should be possible to start a[/color]
          process[color=blue]
          > and pipe command streams into it. But I cant make it work, it always[/color]
          stalls[color=blue]
          > when I try to read the output:
          >
          > /// code 1
          > Process ftpProcess = new Process();
          >
          > ftpProcess.Star tInfo.FileName = "cmd";
          > ftpProcess.Star tInfo.CreateNoW indow = true;
          > ftpProcess.Star tInfo.RedirectS tandardInput = true;
          > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
          > ftpProcess.Star tInfo.UseShellE xecute = false;
          >
          > ftpProcess.Star t();
          >
          > string output = ftpProcess.Stan dardOutput.Read ToEnd();
          > string error = ftpProcess.Stan dardError.ReadT oEnd();
          >
          > ftpProcess.Stan dardInput.Write Line("ftp localhost");
          > ftpProcess.Stan dardInput.Write Line();
          >
          > output = ftpProcess.Stan dardOutput.Read ToEnd();
          > error = ftpProcess.Stan dardError.ReadT oEnd();
          >
          > ftpProcess.Stan dardInput.Write Line("username" );
          > ftpProcess.Stan dardInput.Write Line();
          >
          > //... further ftp action - but I never get this far....
          >
          > output = ftpProcess.Stan dardOutput.Read ToEnd();
          > error = ftpProcess.Stan dardError.ReadT oEnd();
          >
          > ftpProcess.Wait ForExit();
          > ///////////
          >
          > I also tried:
          >
          > /// code2
          > char [] output = new char [1000];
          > string all = "";
          >
          > Process ftpProcess = new Process();
          > ftpProcess.Star tInfo.FileName = "cmd";
          > ftpProcess.Star tInfo.CreateNoW indow = true;
          > ftpProcess.Star tInfo.RedirectS tandardInput = true;
          > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
          > ftpProcess.Star tInfo.UseShellE xecute = false;
          > ftpProcess.Star t();
          > int i = ftpProcess.Stan dardOutput.Read (output, 0, 1000);
          > ftpProcess.Stan dardInput.Write Line("ftp localhost");
          > i = ftpProcess.Stan dardOutput.Read (output, i, 1000-i);
          > ftpProcess.Stan dardInput.Write Line("username" );
          > ftpProcess.Stan dardInput.Write Line("password" );
          > ftpProcess.Stan dardInput.Write Line("mkdir test");
          > ftpProcess.Stan dardInput.Write Line("ls");
          > ftpProcess.Stan dardInput.Write Line("bye");
          > all = ftpProcess.Stan dardOutput.Read ToEnd();
          >
          >
          > thanx
          >
          > ps. And yes - I did connect manually by the command prompt so it should
          > work.
          >
          >[/color]


          Comment

          • Gang Peng

            #6
            Re: Process question

            This is a classic you will see if you use Process class incorrectly.
            You can read the document about Process class to understand why this
            happens.
            Always read the document :)
            In our next release, you will be able to get the output asynchrously.

            Gang Peng
            [MS]

            "ask" <a@creepREM.d k> wrote in message
            news:3f7180d8$0 $265$4d4eb98e@n ews.dk.uu.net.. .[color=blue]
            > Hi NG
            >
            > I'm a bit new to programming c# and have a question regarding ftp by the
            > command prompt. As far as I can see it should be possible to start a[/color]
            process[color=blue]
            > and pipe command streams into it. But I cant make it work, it always[/color]
            stalls[color=blue]
            > when I try to read the output:
            >
            > /// code 1
            > Process ftpProcess = new Process();
            >
            > ftpProcess.Star tInfo.FileName = "cmd";
            > ftpProcess.Star tInfo.CreateNoW indow = true;
            > ftpProcess.Star tInfo.RedirectS tandardInput = true;
            > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
            > ftpProcess.Star tInfo.UseShellE xecute = false;
            >
            > ftpProcess.Star t();
            >
            > string output = ftpProcess.Stan dardOutput.Read ToEnd();
            > string error = ftpProcess.Stan dardError.ReadT oEnd();
            >
            > ftpProcess.Stan dardInput.Write Line("ftp localhost");
            > ftpProcess.Stan dardInput.Write Line();
            >
            > output = ftpProcess.Stan dardOutput.Read ToEnd();
            > error = ftpProcess.Stan dardError.ReadT oEnd();
            >
            > ftpProcess.Stan dardInput.Write Line("username" );
            > ftpProcess.Stan dardInput.Write Line();
            >
            > //... further ftp action - but I never get this far....
            >
            > output = ftpProcess.Stan dardOutput.Read ToEnd();
            > error = ftpProcess.Stan dardError.ReadT oEnd();
            >
            > ftpProcess.Wait ForExit();
            > ///////////
            >
            > I also tried:
            >
            > /// code2
            > char [] output = new char [1000];
            > string all = "";
            >
            > Process ftpProcess = new Process();
            > ftpProcess.Star tInfo.FileName = "cmd";
            > ftpProcess.Star tInfo.CreateNoW indow = true;
            > ftpProcess.Star tInfo.RedirectS tandardInput = true;
            > ftpProcess.Star tInfo.RedirectS tandardOutput = true;
            > ftpProcess.Star tInfo.UseShellE xecute = false;
            > ftpProcess.Star t();
            > int i = ftpProcess.Stan dardOutput.Read (output, 0, 1000);
            > ftpProcess.Stan dardInput.Write Line("ftp localhost");
            > i = ftpProcess.Stan dardOutput.Read (output, i, 1000-i);
            > ftpProcess.Stan dardInput.Write Line("username" );
            > ftpProcess.Stan dardInput.Write Line("password" );
            > ftpProcess.Stan dardInput.Write Line("mkdir test");
            > ftpProcess.Stan dardInput.Write Line("ls");
            > ftpProcess.Stan dardInput.Write Line("bye");
            > all = ftpProcess.Stan dardOutput.Read ToEnd();
            >
            >
            > thanx
            >
            > ps. And yes - I did connect manually by the command prompt so it should
            > work.
            >
            >[/color]


            Comment

            Working...