Windows Forms: How to Upload files with FTP in C#
By FoxLearn 6/20/2017 9:07:05 PM 15.31K
How to upload a file to an FTP server in C#
Step 1: Click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "FTPUpload" and then click OK
Step 2: Design your form as below
Step 3: Add code to handle your form
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FTPUpload { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //Define a struct to store login struct FtpSetting { public string Server { get; set; } public string Username { get; set; } public string Password { get; set; } public string FileName { get; set; } public string FullName { get; set; } } FtpSetting _inputParameter; private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { string fileName = ((FtpSetting)e.Argument).FileName; string fullName = ((FtpSetting)e.Argument).FullName; string userName = ((FtpSetting)e.Argument).Username; string password = ((FtpSetting)e.Argument).Password; string server = ((FtpSetting)e.Argument).Server; FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(string.Format("{0}/{1}", server, fileName))); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(userName, password); Stream ftpStream = request.GetRequestStream(); FileStream fs = File.OpenRead(fullName); byte[] buffer = new byte[1024]; double total = (double)fs.Length; int byteRead = 0; double read = 0; do { if (!backgroundWorker.CancellationPending) { //Upload file & update process bar byteRead = fs.Read(buffer, 0, 1024); ftpStream.Write(buffer, 0, byteRead); read += (double)byteRead; double percentage = read / total * 100; backgroundWorker.ReportProgress((int)percentage); } } while (byteRead != 0); fs.Close(); ftpStream.Close(); } private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { lblStatus.Text = $"Uploaded {e.ProgressPercentage} %"; progressBar.Value = e.ProgressPercentage; progressBar.Update(); } private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { lblStatus.Text = "Upload complete !"; } private void btnUpload_Click(object sender, EventArgs e) { using (OpenFileDialog ofd = new OpenFileDialog() { Multiselect = false, ValidateNames = true, Filter = "All files|*.*" }) { if (ofd.ShowDialog() == DialogResult.OK) { FileInfo fi = new FileInfo(ofd.FileName); _inputParameter.Username = txtUserName.Text; _inputParameter.Password = txtPassword.Text; _inputParameter.Server = txtServer.Text; _inputParameter.FileName = fi.Name; _inputParameter.FullName = fi.FullName; backgroundWorker.RunWorkerAsync(_inputParameter); } } } } }
VIDEO TUTORIALS
- How to save files using SaveFileDialog in C#
- How to make an Alarm clock with sound in C#
- How to Display Images in DataGridView in C#
- How to Print DataGridView with Header & Footer with Landscape in C#
- How to Create a custom Progress Bar with Percentage in C#
- How to read an image file in C#
- How to use BackgroundWorker in C#
- How to protect .NET code from reverse engineering
Categories
Popular Posts
Admin Tailwind CSS Admin Dashboard Template
11/18/2024
Regal Admin Dashboard Template
11/18/2024
Flat Able Admin Dashboard Template
11/18/2024
AdminKit Bootstrap 5 HTML5 UI Kits Template
11/17/2024