C# FTP Dosya Alma Verme Programı

4 dk okuma süresi
5,017

Bu makalemizde C# programlama dili ile kodlanmış FTP Dosya Alma Verme Programını sizlerle paylaşacağız. Bu programda tasarım için Flat.UI.dll dosyası kullanılmıştır ve bu dosyayı makalenin sonundaki indirme linkinden temin edebilirsiniz. İlk olarak Flat.UI.dll dosyasını referance olarak ekliyoruz. Daha sonra ToolBox’a Flat.UI.dll dosyasındaki nesneleri eklemek için ToolBox’a farenin sağ tuşu ile tıklayıb Choose Item… seçeneğini seçerek açılan pencereden dll dosyasını ekleyebilirsiniz. Sonra form penceresinin dizaynını aşağıdaki resimdeki gibi yapıyoruz ve bir adet saveFileDialog1 bir adet de openFileDialog1 nesneleri ekliyoruz.

Kod kısmı aşağıdaki gibidir.

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace FTP_Dosya_Alma_Verme
{
 public partial class Form1 : Form
 {
 public Form1()
 {
 InitializeComponent();
 }
 void DosyaIndir()
 {
 try
 {
 progressBar1.Value += 5;
 //Web istemcisi çıkartıyoruz.
 WebClient istemci = new WebClient();

 //(Gerekiyorsa)FTP giriş bilgilerimizi ayarlıyoruz.
 istemci.Credentials = new NetworkCredential(textEditKullaniciAd.Text, textEditParola.Text);
 progressBar1.Value += 15;
 //İstediğimiz data'yı bir bayt dizisine yüklüyoruz.
 byte[] veriDosya = istemci.DownloadData(textEditDosyaLink.Text);
 progressBar1.Value += 15;
 //Bayt dizisini yazacağımız bir FileStream oluşturuyoruz.
 FileStream dosya = File.Create(textEditKayitYeri.Text);
 progressBar1.Value += 15;
 //Bayt dizinimizdeki tüm veriyi dosyamıza yazdırıyoruz.
 dosya.Write(veriDosya, 0, veriDosya.Length);
 progressBar1.Value += 50;
 //Dosyayı diğer işlemlerin dosyaya ulaşabilmesi için kapatıyoruz.
 dosya.Close();
 MessageBox.Show("İndirme tamamlandı. Dosya " + textEditKayitYeri.Text + " adresine kaydedildi.");
 progressBar1.Value = 0;
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 progressBar1.Value = 0;
 }
 }
 void DosyaYukle()
 {
 try
 {
 FileInfo filei = new FileInfo(textEditYuklenecekDosya.Text);
 string adres = textEditYuklenecekLink.Text;
 string path = adres + filei.Name;
 progressBar2.Value += 5;
 FtpWebRequest FTP;
 FTP = (FtpWebRequest)FtpWebRequest.Create(path);
 FTP.Credentials = new NetworkCredential(textEditKullaniciAd.Text, textEditParola.Text);
 progressBar2.Value += 15;
 FTP.KeepAlive = false;
 FTP.Method = WebRequestMethods.Ftp.UploadFile;
 FTP.UseBinary = true;
 FTP.ContentLength = filei.Length;
 progressBar2.Value += 15;
 int buffLength = 1024;
 byte[] buff = new byte[buffLength];
 int contentLen;
 FileStream FS = filei.OpenRead();
 progressBar2.Value += 15;
 try
 {
 Stream strm = FTP.GetRequestStream(); contentLen = FS.Read(buff, 0, buffLength); while (contentLen != 0)
 {
 strm.Write(buff, 0, contentLen); contentLen = FS.Read(buff, 0, buffLength);
 }
 progressBar2.Value += 50;
 strm.Close();
 FS.Close();
 MessageBox.Show("Yükleme işlemi başarılı. Dosya " + path + " adresine yüklendi.");
 }
 catch
 {
 }
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message);
 progressBar2.Value = 0;
 }
 }
 private void checkBox1_CheckedChanged(object sender, EventArgs e)
 {
 if (checkBox1.Checked == true)
 {
 Properties.Settings.Default.Sunucu = textEditSunucu.Text;
 Properties.Settings.Default.KullaniciAd = textEditKullaniciAd.Text;
 Properties.Settings.Default.Parola = textEditParola.Text;
 Properties.Settings.Default.CheckDurum = true;
 Properties.Settings.Default.Save();
 }
 else
 {
 Properties.Settings.Default.Sunucu = null;
 Properties.Settings.Default.KullaniciAd = null;
 Properties.Settings.Default.Parola = null;
 Properties.Settings.Default.CheckDurum = false;
 Properties.Settings.Default.Save();
 }
 }

 private void button2_Click(object sender, EventArgs e)
 {
 DosyaYukle();
 }

 private void button1_Click(object sender, EventArgs e)
 {
 openFileDialog1.ShowDialog();
 }

 private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
 {
 textEditYuklenecekDosya.Text = openFileDialog1.FileName;
 }

 private void button3_Click(object sender, EventArgs e)
 {
 DosyaIndir();
 }

 private void Form1_Load(object sender, EventArgs e)
 {
 textEditSunucu.Text = Properties.Settings.Default.Sunucu;
 textEditKullaniciAd.Text = Properties.Settings.Default.KullaniciAd;
 textEditParola.Text = Properties.Settings.Default.Parola;
 checkBox1.Checked = Properties.Settings.Default.CheckDurum;
 }

 private void button4_Click(object sender, EventArgs e)
 {
 saveFileDialog1.ShowDialog();
 }

 private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
 {
 textEditKayitYeri.Text = saveFileDialog1.FileName;
 }

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
 {
 if (checkBox1.Checked == true)
 {
 Properties.Settings.Default.Sunucu = textEditSunucu.Text;
 Properties.Settings.Default.KullaniciAd = textEditKullaniciAd.Text;
 Properties.Settings.Default.Parola = textEditParola.Text;
 Properties.Settings.Default.CheckDurum = true;
 Properties.Settings.Default.Save();
 }
 else
 {
 Properties.Settings.Default.Sunucu = null;
 Properties.Settings.Default.KullaniciAd = null;
 Properties.Settings.Default.Parola = null;
 Properties.Settings.Default.CheckDurum = false;
 Properties.Settings.Default.Save();
 }
 }
 }
} 

Yazılması gereken kod satırları yukaridakilerdir. İsterseniz Aşağıdaki bağlantıdan kod satırlarının nasıl
yazılması ve görünümü hakkında bilgi alabilirsiniz.
KOD SATIRLARI – BİLGİ

Programı burdan indirebilirsiniz…


Hasan Adıgüzel – hasanadiguzel@muhendisarsivi.com

Daha Fazla İlgili Makale Yükleyin
Daha Fazla Yük c#

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Ayrıca Kontrol Edin

FastReport Kullanımı ve Örnek Tasarım

Herkese merhaba. Uzun bir aradan sonra yazılım ile ilgili makale yazmaya tekrar başladık. …