Upload file on FTP

Please make sure your ftp path is set as shown below. string CompleteDPath = “ftp://www.example.com/wwwroot/videos/”; string FileName = “sample.mp4”; WebRequest reqObj = WebRequest.Create(CompleteDPath + FileName); The following script work great with me for uploading files and videos to another servier via ftp. FtpWebRequest ftpClient = (FtpWebRequest)FtpWebRequest.Create(ftpurl + “” + username + “_” + filename); ftpClient.Credentials … Read more

How to detect working internet connection in C#?

Just use the plain function System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() that return true of false if the connection is up. From MSDN: A network connection is considered to be available if any network interface is marked “up” and is not a loopback or tunnel interface. Keep in mind connectivity is not all, you can be connected to a local … Read more

FTP directory partial listing with wildcards

The most recent update to the FTP specification (RFC 3659) explicitly forbids it. From section 2.2.2 of that specification, titled “Wildcarding” (emphasis mine): For the commands defined in this specification, all pathnames are to be treated literally. That is, for a pathname given as a parameter to a command, the file whose name is identical … Read more

Upload file and download file from FTP

Both WebClient.UploadFile and WebClient.DownloadFile work correctly for FTP as well as binary files. Upload WebClient client = new WebClient(); client.Credentials = new NetworkCredential(“username”, “password”); client.UploadFile( “ftp://ftp.example.com/remote/path/file.zip”, @”C:\local\path\file.zip”); If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, transfer resuming, etc), use FtpWebRequest. Easy way is to just copy … Read more

FtpWebRequest FTP download with ProgressBar

Trivial example of FTP download using FtpWebRequest with WinForms progress bar: private void button1_Click(object sender, EventArgs e) { // Run Download on background thread Task.Run(() => Download()); } private void Download() { try { const string url = “ftp://ftp.example.com/remote/path/file.zip”; var credentials = new NetworkCredential(“username”, “password”); // Query size of the file to be downloaded WebRequest … Read more

How to check if file exists on FTP before FtpWebRequest

var request = (FtpWebRequest)WebRequest.Create (“ftp://ftp.domain.com/doesntexist.txt”); request.Credentials = new NetworkCredential(“user”, “pass”); request.Method = WebRequestMethods.Ftp.GetFileSize; try { FtpWebResponse response = (FtpWebResponse)request.GetResponse(); } catch (WebException ex) { FtpWebResponse response = (FtpWebResponse)ex.Response; if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { //Does not exist } } As a general rule it’s a bad idea to use Exceptions for functionality in your code like … Read more

How can we show progress bar for upload with FtpWebRequest

The easiest is to use BackgroundWorker and put your code into DoWork event handler. And report progress with BackgroundWorker.ReportProgress. The basic idea: private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var ftpWebRequest = (FtpWebRequest)WebRequest.Create(“ftp://example.com”); ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile; using (var inputStream = File.OpenRead(fileName)) using (var outputStream = ftpWebRequest.GetRequestStream()) { var buffer = new byte[1024 * 1024]; int … Read more