Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI,

i need to copy the contents of one text file (which is in my local system) to another textfile (which is in a sever) by logging into the sever(that is by giving the username and password of the server).i need to implement this through a c# code.can anyone help me in doing this????
Posted
Comments
Sandeep Mewara 23-May-12 10:30am    
What kind of help you are looking for? What have you tried? Where are you stuck?

This could be done either with the unc or ftp. Here is the sample code to perform FTP operation:

C#
public void ftpfile(string ftpfilepath, string inputfilepath)   
{   
    string ftphost = "127.0.0.1";   
    //here correct hostname or IP of the ftp server to be given   
  
    string ftpfullpath = "ftp://" + ftphost + ftpfilepath;   
    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);   
    ftp.Credentials = new NetworkCredential("userid", "password");   
    //userid and password for the ftp server to given   
  
    ftp.KeepAlive = true;   
    ftp.UseBinary = true;   
    ftp.Method = WebRequestMethods.Ftp.UploadFile;   
    FileStream fs = File.OpenRead(inputfilepath);   
    byte[] buffer = new byte[fs.Length];   
    fs.Read(buffer, 0, buffer.Length);   
    fs.Close();   
    Stream ftpstream = ftp.GetRequestStream();   
    ftpstream.Write(buffer, 0, buffer.Length);   
    ftpstream.Close();   
}
 
Share this answer
 
Comments
amaljosep 24-May-12 3:22am    
thanks a lot for helping me....but here how to specify the ftp path.my ftphost is 149.223.26.33 and my ftp file path is D:\Developers\muralim\TRW.HRM.Headcount_LMSFeed\Log.to this path i need to copy the content from my local system which is placed in C:\Documents and Settings\aj99823\Desktop\project\employeedetails1.txt.i changed the code accordingly .but its still throwing me one error saying "The underlying connection was closed: An unexpected error occurred on a receive.".can u plss help me..?
amaljosep 24-May-12 3:25am    
The code i wrote is,

public void ftpfile(string ftpfilepath, string inputfilepath)
{
string ftphost = "149.223.26.33";

string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential("amaljose", "amal*03");
ftp.Timeout = 10000000;
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(inputfilepath);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
ftp.Proxy = null;
Stream ftpstream = ftp.GetRequestStream();

FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
Console.WriteLine("Upload File Complete, status{0}", response.StatusDescription);
response.Close();
}

and in the main function

public static void Main(string[] args)
{
Class1 b1 = new Class1();
b1.ftpfile(@"//D:/Developers/muralim/TRW.HRM.Headcount_LMSFeed/Log" + @"/" + "employeedetails1.txt", @"C:/Documents and Settings/aj99823/Desktop/project/employeedetails1.txt");
}
Another scenario is same userID/password even across the domains.

All you need is for the username and password to be the same and this will work, even if they are in different domains. The thing to make sure of is that your .NET application is running as this user.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900