csharp 通过ftp上传或删除文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 通过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);
}

以上是关于csharp 通过ftp上传或删除文件的主要内容,如果未能解决你的问题,请参考以下文章

怎么通过PHP来支持FTP和HTTP上传

#yyds干货盘点# java通过sftp模式实现FTP的文件上传下载删除功能

C# 使用 FTP 上传整个目录

dos命令下 怎么把文件上传到FTP?

为啥FTP上传的文件是0字节,FTP上传文件0字节的解决办法

如何删除FTP文件