当前位置: 首页> 游戏> 单机 > C#中获取FTP服务器文件

C#中获取FTP服务器文件

时间:2025/7/13 9:17:12来源:https://blog.csdn.net/qq_41760419/article/details/139118831 浏览次数:0次

1、从ftp下载pdf的方法

public static void DownloadPdfFileFromFtp(string ftpUrl,string user,string password string localPath)

{

// 创建FtpWebRequest对象
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUrl);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(username, password);
// 使用WebResponse获取响应
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
    // 获取文件流
    Stream responseStream = response.GetResponseStream();

    // 创建文件流写入器,将数据写入本地文件
    using (FileStream fileStream = new FileStream(localPath, FileMode.Create))
    {
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            fileStream.Write(buffer, 0, bytesRead);
        }
    }
}

}

2、创建指定路径的文件夹

string tempPath = "C:\\ftpfile\\";

//用DirectoryInfo拼接路径
DirectoryInfo di = new DirectoryInfo(string.Format(@"{0}\{1}", tempPath, "图纸PDF"));
if (!di.Exists)
{
    di.Create();
}
string filepdf = di.FullName + "\\" + "图纸_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";

3、调用

string user="用户名";

string password="密码";
DownloadPdfFileFromFtp(ftpServerUrl, user,password,filepdf);

关键字:C#中获取FTP服务器文件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: