Console Application run FTP

G

Guest

Not sure what I am missing.

How can I run the FTP from command line in a console app.

Console.WriteLine("ftp -s:Idx.scr"); <-- Thought that would start it but it
does it.

I know in a vbscript I could write:

'The following code executes the FTP script. It creates a Shell
'object and run FTP program on top of it.
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run ("ftp -s:" & chr(34) & strFTPScriptFileName & chr(34))
Set objShell = Nothing

How do I do this in .net?
 
W

William DePalo [MVP VC++]

Moojjoo said:
Not sure what I am missing.
:)

How can I run the FTP from command line in a console app.

Console.WriteLine("ftp -s:Idx.scr"); <-- Thought that would start it but
it
does it.

All this does is display text. It might as well say happy birthday.

If you had a process which was running the command interpreter you could
force feed it a command but that's a subject for another day.

This, I think, is as easy (and cheesy) a solution to your question as there
is:

using System;
using System.Diagnostics;

class Class1
{

[STAThread]
static void Main(string[] args)
{
ProcessStartInfo startInfo;

startInfo = new ProcessStartInfo("cmd.exe");
startInfo.Arguments = "/C YourFTPCommandGoesHere";
startInfo.CreateNoWindow = true;

Process.Start(startInfo);
}

}

Regards,
Will
 
G

Guest

William,

startInfo.Arguments = "/C YourFTPCommandGoesHere";

What does the /C mean?

I replaced it with

ProcessStartInfo startInfo;

startInfo = new ProcessStartInfo("cmd.exe");
startInfo.Arguments = "/C ftp -s:Idx.scr";
startInfo.CreateNoWindow = true;

Process.Start(startInfo);

And it just keeps poping a CMD window over and over.

William DePalo said:
Moojjoo said:
Not sure what I am missing.
:)

How can I run the FTP from command line in a console app.

Console.WriteLine("ftp -s:Idx.scr"); <-- Thought that would start it but
it
does it.

All this does is display text. It might as well say happy birthday.

If you had a process which was running the command interpreter you could
force feed it a command but that's a subject for another day.

This, I think, is as easy (and cheesy) a solution to your question as there
is:

using System;
using System.Diagnostics;

class Class1
{

[STAThread]
static void Main(string[] args)
{
ProcessStartInfo startInfo;

startInfo = new ProcessStartInfo("cmd.exe");
startInfo.Arguments = "/C YourFTPCommandGoesHere";
startInfo.CreateNoWindow = true;

Process.Start(startInfo);
}

}

Regards,
Will
 
W

William DePalo [MVP VC++]

Moojjoo said:
startInfo.Arguments = "/C YourFTPCommandGoesHere";

What does the /C mean?

It tells the command interpreter to carry out the command and then quickly
exit.
I replaced it with

ProcessStartInfo startInfo;

startInfo = new ProcessStartInfo("cmd.exe");
startInfo.Arguments = "/C ftp -s:Idx.scr";
startInfo.CreateNoWindow = true;

Process.Start(startInfo);

And it just keeps poping a CMD window over and over.

Change /C to /K and try it again. /K keeps the command window open so that
you can see what FTP is doing.

Here I used this FTP script:

open localhost
user ***** *******
binary
get file.ext
quit

where the asterisks represent my user name and password and this source:

using System;
using System.Diagnostics;

class Class1
{

[STAThread]
static void Main(string[] args)
{
ProcessStartInfo startInfo;

startInfo = new ProcessStartInfo("cmd.exe");
startInfo.Arguments = "/C ftp -n -s:c:\\ftp.scr";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;

Process.Start(startInfo);
}

}

It works for me, here.

Just by the way, I use a hidden window style so that I don't get that
annoying flash of the command window. You'll want to see the command window
while testing.

Regards,
Will
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top