FDiri
7/23/2015 - 9:09 AM

upload or delete files over ftp

protected void UploadToFTP(string path, string name)
        {
            Logger("Executing UploadToFTP Function . . .");
            Logger("UploadToFTP Report :: " + path + "," + name);
            String sourcefilepath = path;
            String ftpurl = "ftp://cnetiran.com";
            String ftpusername = "cnetiran.com";
            String ftppassword = "************";
            try
            {
                string filename = Path.GetFileName(sourcefilepath);
                string ftpfullpath = ftpurl + "//rasool//" + name;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
                ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
 
                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
 
                FileStream fs = File.OpenRead(sourcefilepath);
                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();
            }
            catch (Exception ex)
            {
                Logger("UploadToFTP Function Says : " + ex.ToString());
            }
            Logger("End of UploadToFTP . . .");
        }
Uri myUri =new Uri("ftp://DMS:DMS@91.93.139.85/test.txt");
Console.Write(myUri.UserInfo);

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(myUri);

request.Method = WebRequestMethods.Ftp.DeleteFile;
request.GetResponse();

string filepath=@"D:\test.txt";
using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential("DMS", "DMS");
    client.UploadFile("ftp://91.93.139.85/test.txt", "STOR", filepath);
}