Category Archives: C#
Download file from FTP
I’ve created a simple method for you that will download a file from an FTP server and save it in the location you wish.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | private void DownloadFtpFile(string url, string savePath) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(url); request.Method = WebRequestMethods.Ftp.DownloadFile; request.Credentials = new NetworkCredential("ftp username", "ftp password"); request.UseBinary = true; using (FtpWebResponse response = (FtpWebResponse)request.GetResponse()) { using (Stream rs = response.GetResponseStream()) { using (FileStream ws = new FileStream(savePath, FileMode.Create)) { byte[] buffer = new byte[2048]; int bytesRead = rs.Read(buffer, 0, buffer.Length); while (bytesRead > 0) { ws.Write(buffer, 0, bytesRead); bytesRead = rs.Read(buffer, 0, buffer.Length); } } } } } |
Keep in mind that ideally you will want to do some error catching for cases that the file does not exist or the server is down and so on.
Simply call the method as follows:
1 | DownloadFtpFile("ftp://example.com/path_to_my_image_or_file.jpg", "C:\\location_and_name_of_file.jpg"); |
Posted in C#.
Tagged C#, csharp, ftp, ftp download file, FtpWebRequest, FtpWebResponse, snippet, winforms
Custom form background color
Winforms by default don’t offer much customization as far as coloring, especially when it comes down to gradient patterns.
If you want to change the background of your form to something a bit more unique to make it look like this
then simply follow steps below.
First handle the form’s Paint
event.
1 2 3 4 5 6 7 8 | private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; Rectangle rect = new Rectangle(0, 0, this.Size.Width, this.Size.Height); LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Green, Color.White, LinearGradientMode.ForwardDiagonal); g.FillRectangle(brush, rect); brush.Dispose(); } |
Feel free to change the colors and the gradient direction to what you would like.
Posted in C#.
Tagged C#, csharp, custom form background color, form background color, snippet, winforms
Benchmarking your code
Benchmarking your code is very important since it allows you to pinpoint bottlenecks in your application and allows you to improve the overall performance of your software as well as learning which techniques are more taxing than others.
For this example we are going to be using the Stopwatch
class. This class will enable us to see how many milliseconds it required for our code to be executed so we can compare different methods or techniques.
As an example, the code below checks the performance difference of a try/catch block in a loop vs the performance when not using one. Keep in mind that try/catch blocks are not that performance heavy if they don’t actually catch something but the performance gets hit a lot in cases a lot of exceptions are being thrown.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | Stopwatch sw = new Stopwatch(); int test; //test 1 sw.Start(); test = 0; for (int i = 0; i < int.MaxValue; i++) { test = i; } Console.WriteLine(sw.ElapsedMilliseconds); // 7644 sw.Reset(); //test 2 sw.Start(); test = 0; for (int i = 0; i < int.MaxValue; i++) { try { test = i; } catch { } } Console.WriteLine(sw.ElapsedMilliseconds); // 8429 sw.Reset(); |
For even better results I would advise running each test 3 to 5 times and simply calculating the average time of execution.