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


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Login to FTP Server with proper credentials and download File


Posted Date: 02 May 2007    Resource Type: Code Snippets    Category: FTP

Posted By: Balamurali Balaji       Member Level: Diamond
Rating:     Points: 10



The following code uses the NetworkCredentials class to supply the required username and password to the FTP Site and reads the response from a shared file.

This sample code shows how to connect to an ftp server and download files using C# syntax.



System.Net.NetworkCredential nc = new System.Net.NetworkCredential("bbmurali", "pass@word1");
CredentialCache cc = new CredentialCache();
cc.Add(new Uri("ftp://server01/sharebala/mydoc.doc"), "Basic", nc);
System.Net.FtpWebRequest wr = (FtpWebRequest) System.Net.FtpWebRequest.Create("ftp://server01/sharebala/mydoc.doc");
wr.Credentials = cc;
System.Net.FileWebResponse fwr = (FileWebResponse) wr.GetResponse();
Stream dataStream = fwr.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);






Responses

Author: santhoshkumar    28 Apr 2008Member Level: Gold   Points : 2
Your code was some what understood please try to give a comments and some explation about the code.


Author: Balamurali Balaji    03 May 2008Member Level: Diamond   Points : 2
Hi santhosh,

Assume that you have a ftp server and few folders that are shared for access. The files in the folder are then available for download and upload. This snippet uses networkcredential to acquire an authentication object to the ftp server. Credential cache is the object that stores the urls, authentication, mode of authentication.

With these two objects, networkcredential and credentialcache, FtpWebRequest object can do the actual uploading or downloading of a particular file.

Hope this is clear.

Goodluck



Author: mahendra    05 May 2008Member Level: Gold   Points : 2
Yeah i also understood code snippet.Its good article which will help me in future.



Author: santhoshkumar    08 May 2008Member Level: Gold   Points : 2
Thank you balaji, now its clear, thank you very much for ur comment


Author: subbarayudu    14 Aug 2008Member Level: Silver   Points : 2
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO.Ports;
using System.Net.Mail;
using System.Text;

public partial class FTP1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Net.NetworkCredential nc = new System.Net.NetworkCredential("asif", "2july2008");
System.Net.CredentialCache cc = new System.Net.CredentialCache();
cc.Add(new Uri("ftp://192.168.3.210/Rayudu/Rayudu.txt"), "Basic", nc);
System.Net.FtpWebRequest wr = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create("ftp://192.168.3.210/Rayudu/Rayudu.txt");
wr.Credentials = cc;
System.Net.FileWebResponse fwr = (System.Net.FileWebResponse)wr.GetResponse();
System.IO.Stream dataStream = fwr.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Response.Write(responseFromServer);
}
}

FTP server IP 192.168.3.210 system have
Rayudu folder is there now
my system ip 192.168.2.83 I want to copy the Rayudu.txt file into FTP server Rayudu folder
I given all permission to Rayudu folder
but it is not executing what is ther problem
it is giving error at line

webexception unhandled by exception
error is coming to this line what i have to do tell me how to recttify this problem
System.Net.FileWebResponse fwr = (System.Net.FileWebResponse)wr.GetResponse();




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.
Previous Resource: Send Email from Visual Basic Using OLE Messaging
Return to Discussion Resource Index
Post New Resource
Category: FTP


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

audio conferencing services

Contact Us    Privacy Policy    Terms Of Use