C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...









Upload files using FTP with .NET


Posted Date: 03 Jul 2008    Resource Type: Code Snippets    Category: FTP

Posted By: Ashokkumar       Member Level: Gold
Rating:     Points: 10



This code sample shows how to use C# code to programmatically upload files to an ftp server.

// get the contents of the file you want to upload

byte[] fileContents;

string FinalFile = @"c:\MyFile.txt";
using(System.IO.FileStream fs = new System.IO.FileStream(FinalFile, System.IO.FileMode.Open)) {

fileContents = new byte[fs.Length];
fs.Read(fileContents, 0, (int)fs.Length);

}

//get the filename, append it to the URL
System.IO.FileInfo fi = new System.IO.FileInfo(FinalFile);

//FTPServerURI must be in the format of:

//ftp://user:password@ftp.myserver.com:21/path/to/upload/folder/
string uri = string.Format("{0}{1}", FTPServerURI, fi.Name);

//now we have:

//ftp://user:password@ftp.myserver.com:21/path/to/upload/folder/autoexec.bat

//create the ftp request
System.Net.WebRequest request = System.Net.FtpWebRequest.Create(uri);

//state that we uploading the file
request.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

//get the request stream
using (System.IO.Stream stm = request.GetRequestStream()) {

//write the contents of the file up

stm.Write(zipContents, 0, zipContents.Length);

}




Responses

Author: subbarayudu    14 Aug 2008Member Level: Silver   Points : 2
Hi
I am also using your code and all but i am unable to follow just give me confirmation regarding
FTPServerURI --- instead of
I am giving
like this
ftp://vamsi:krishna@192.168.1.91:D:/vamsi/
// what u given in code i written like this , this is source or destination IP address i dont know

//ftp://user:password@ftp.myserver.com:21/path/to/upload/folder/
string uri = string.Format("{0}{1}", FTPServerURI, fi.Name);1

bottom of this code u given
stm.Write(zipContents, 0, zipContents.Length);
what is zipContents it is given error does not exist like this it is giving error


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Uploading files to an FTP server
Previous Resource: FTP using Microsoft Inet in VB 6
Return to Discussion Resource Index
Post New Resource
Category: FTP


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use