Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to do ftp using C#

Status
Not open for further replies.

jhnancy

IS-IT--Management
May 14, 2003
5
US
Hello Friends, I want to create a function/class that will take ftp arguments from a .txt file and then execute them on a command line prompt that i will create thru process.Start.
So basically I want to do something line this

process.Start (cmd.exe )
Read one line (arguments) from file and
execute these arguments in the above cmd line process.

my txt file is like this:
ftp ftp.yahoo.com
user user1
password passord@2333
cd pub/shared/...
bin
mget *.txt
cd /
----
and exit out.
Appreciate your help !!
 
I'm not sure if there is an already made class for FTP access...

What you want to do will require to use sockets and you'd have to be familiar with the FTP protocol.
Then again search arround on google I'd bet you can find a good tutorial on this subject.
 
The other thing you might want to search for is some kind of TERM program written in C#.

This would give you the FTP interface you need without writing all your own FTP stuff.
 
Unless I've misunderstood, jhnancy is using the built-in ftp program:

process.Start (cmd.exe )

and simply requires assistance in parsing the text file to send the necessary values to the program.

Since I don't use C anything I wont attempt a code example, however what might be possible solution is a slight modification to the text file as follows:

remove -
ftp from the first line - just leaving the ftp address
user from the second line - just leaving the user name
password form the third line - just leaving the password

This would then leave a file that it should be possible to 'pipe' as STDIN to the ftp program using somethng along the lines of:

process.Start (cmd.exe ftp < ftptextfie.txt)

Hope this helps.
 
Yes EarthAndfFire. Thats what exactly I am trying to get done here. My text file is flexible and I can have one command on each line and then I can pipe the file to ftp command prompt. Appreciate your response..
Any help is highly appreciated.
I am trying to do this in C# and i beleive I can use Process.Start to start the ftp command prompt but I need to pass the parameters to the file.
-Jhnancy
 
jhnancy, I'm afraid the C# code for this is way out of my league. If no one comes up with an example over the weekend, I'll put some VB.Net code together for you - which hopefully you will be able to convert.
 
I'm surprised that no C# gurus have come up with a solution however:


This assumes that the host is the first line of the file and is in the format:
ftp hostname

Code:
    Dim sr As New IO.StreamReader("c:\DoFTP\Source.txt")
    Dim sw As New IO.StreamWriter("c:\DoFTP\Source1.txt")

    Dim host As String
    Dim CommandTail As String
    Dim ALine As String
    ALine = sr.ReadLine
    host = ALine.Replace("ftp ", "")
    Do
      ALine = sr.ReadLine
      sw.WriteLine(ALine)
    Loop Until ALine Is Nothing
    sr.Close()
    sw.Close()

    CommandTail = "/c ftp -n -s:c:\DoFTP\Source1.txt " + host + " >> c:\DoFTP\FTPLog.txt"
    Dim p As New System.Diagnostics.Process

    p.Start("cmd.exe ", CommandTail)

A couple of points:

The host is stripped out of the file and inserted into the CommandTail - the rest of the file is simply copied.

The word password must be changed to pwd

In the commandTail:
/c means run the command then exit the command window
-n means don't use interactive login
-s specifies the source file for the ftp login and commands

I've also added redirected STDOUT so that you can have a report of the activity:
>> [CREATE/APPEND] logfilename
or
> [CREATE/OVERWRITE] logfilename


Hope this helps.
 
jhnancy, I was just wondering whether or not you had solved the ftp problem.
 
EarthAndFire,
I used the above code you provided and modified it little bit and it worked perfectly for me. Thanks a lot for your help.
 
Getting the command line to work was quite fiddly since I didn't have an ftp site to check it all against, so I'm glad it worked ok
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top