- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
[C#] Giới thiệu thư viện Fluent FTP Awesome dùng để làm việc với FTP
Xin chào các bạn, bài viết hôm nay mình xin giới thiệu đến các bạn thư viện Fluent FTP trong lập trình C#, dùng để thực hiện các lệnh về FTP trên NET: Download, Upload, GetListFile, Delete files...
[C#] Introduce Library Fluent FTP
Trong .NET, để làm việc với FTP Server, nghĩa là chúng ta viết một ứng dụng FTP Client dùng để thực hiện các kết nối đến máy chủ FTP.
Thực hiện các nghiệp vụ: Upload, Download, Create File, Create Folder, Rename File, Get List Files...
Thì thư viện Fluent FTP sẽ giúp các bạn thực hiện một cách nhanh chóng và đơn giản.
Fluent FTP còn support hỗ trợ làm việc bất động bộ Async, giúp cho việc upload hay download file dung lượng lớn một cách dễ dàng mà không làm treo giao diện ứng dụng.
Cài đặt thư viện Fluent FTP vào Project bằng Nuget:
PM> Install-Package FluentFTP -Version 33.0.3
1. Sử dụng Fluent FTP đồng bộ Synchronous
// create an FTP client
FtpClient client = new FtpClient("123.123.123.123");
// specify the login credentials, unless you want to use the "anonymous" user account
client.Credentials = new NetworkCredential("david", "pass123");
// begin connecting to the server
client.Connect();
// get a list of files and directories in the "/htdocs" folder
foreach (FtpListItem item in client.GetListing("/htdocs")) {
// if this is a file
if (item.Type == FtpFileSystemObjectType.File){
// get the file size
long size = client.GetFileSize(item.FullName);
// calculate a hash for the file on the server side (default algorithm)
FtpHash hash = client.GetChecksum(item.FullName);
}
// get modified date/time of the file or folder
DateTime time = client.GetModifiedTime(item.FullName);
}
// upload a file
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");
// rename the uploaded file
client.Rename("/htdocs/MyVideo.mp4", "/htdocs/MyVideo_2.mp4");
// download the file again
client.DownloadFile(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4");
// compare the downloaded file with the server
if (client.CompareFile(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4") == FtpCompareResult.Equal){ }
// delete the file
client.DeleteFile("/htdocs/MyVideo_2.mp4");
// upload a folder and all its files
client.UploadDirectory(@"C:\website\videos\", @"/public_html/videos", FtpFolderSyncMode.Update);
// upload a folder and all its files, and delete extra files on the server
client.UploadDirectory(@"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror);
// download a folder and all its files
client.DownloadDirectory(@"C:\website\logs\", @"/public_html/logs", FtpFolderSyncMode.Update);
// download a folder and all its files, and delete extra files on disk
client.DownloadDirectory(@"C:\website\dailybackup\", @"/public_html/", FtpFolderSyncMode.Mirror);
// delete a folder recursively
client.DeleteDirectory("/htdocs/extras/");
// check if a file exists
if (client.FileExists("/htdocs/big2.txt")){ }
// check if a folder exists
if (client.DirectoryExists("/htdocs/extras/")){ }
// upload a file and retry 3 times before giving up
client.RetryAttempts = 3;
client.UploadFile(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpRemoteExists.Overwrite, false, FtpVerify.Retry);
// disconnect! good bye!
client.Disconnect();
2. Sử dụng bất đồng bộ Async (Support từ phiên bản Net Framework 4.5 trở lên)
// create an FTP client
FtpClient client = new FtpClient("123.123.123.123");
// specify the login credentials, unless you want to use the "anonymous" user account
client.Credentials = new NetworkCredential("david", "pass123");
// begin connecting to the server
await client.ConnectAsync();
// get a list of files and directories in the "/htdocs" folder
foreach (FtpListItem item in await client.GetListingAsync("/htdocs")) {
// if this is a file
if (item.Type == FtpFileSystemObjectType.File){
// get the file size
long size = await client.GetFileSizeAsync(item.FullName);
// calculate a hash for the file on the server side (default algorithm)
FtpHash hash = await client.GetChecksumAsync(item.FullName);
}
// get modified date/time of the file or folder
DateTime time = await client.GetModifiedTimeAsync(item.FullName);
}
// upload a file
await client.UploadFileAsync(@"C:\MyVideo.mp4", "/htdocs/MyVideo.mp4");
// rename the uploaded file
await client.RenameAsync("/htdocs/MyVideo.mp4", "/htdocs/MyVideo_2.mp4");
// download the file again
await client.DownloadFileAsync(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4");
// compare the downloaded file with the server
if (await client.CompareFileAsync(@"C:\MyVideo_2.mp4", "/htdocs/MyVideo_2.mp4") == FtpCompareResult.Equal){ }
// delete the file
await client.DeleteFileAsync("/htdocs/MyVideo_2.mp4");
// upload a folder and all its files
await client.UploadDirectoryAsync(@"C:\website\videos\", @"/public_html/videos", FtpFolderSyncMode.Update);
// upload a folder and all its files, and delete extra files on the server
await client.UploadDirectoryAsync(@"C:\website\assets\", @"/public_html/assets", FtpFolderSyncMode.Mirror);
// download a folder and all its files
await client.DownloadDirectoryAsync(@"C:\website\logs\", @"/public_html/logs", FtpFolderSyncMode.Update);
// download a folder and all its files, and delete extra files on disk
await client.DownloadDirectoryAsync(@"C:\website\dailybackup\", @"/public_html/", FtpFolderSyncMode.Mirror);
// delete a folder recursively
await client.DeleteDirectoryAsync("/htdocs/extras/");
// check if a file exists
if (await client.FileExistsAsync("/htdocs/big2.txt")){ }
// check if a folder exists
if (await client.DirectoryExistsAsync("/htdocs/extras/")){ }
// upload a file and retry 3 times before giving up
client.RetryAttempts = 3;
await client.UploadFileAsync(@"C:\MyVideo.mp4", "/htdocs/big.txt", FtpRemoteExists.Overwrite, false, FtpVerify.Retry);
// disconnect! good bye!
await client.DisconnectAsync();
Chi tiết về thư viện Fluent FTP, các bạn có thể tham khảo thêm ở trên trang github.
Thanks for watching!