C# FTP İşlemleri

private string host = "ftp://ftp.example.com";
private string user = "example.com";
private string pass = "sifre";
private FtpWebResponse ftpResponse = null;
private Stream ftpStream = null;
private int bufferSize = 2048;
private FtpWebRequest FTP=null;

public bool download(string sunucu_yolu, string kayitedilecek_yer)
        {
            if (NetworkKontrol.InternetKontrol())
            {
                try
                {
                    FTP = (FtpWebRequest)WebRequest.Create(host + "/" + sunucu_yolu);
                    FTP.Credentials = new NetworkCredential(user, pass);
                    FTP.UseBinary = true;
                    FTP.UsePassive = true;
                    FTP.KeepAlive = true;
                    FTP.Method = WebRequestMethods.Ftp.DownloadFile;
                    ftpResponse = (FtpWebResponse)FTP.GetResponse();
                    ftpStream = ftpResponse.GetResponseStream();
                    FileStream localFileStream = new FileStream(kayitedilecek_yer, FileMode.Create);
                    byte[] byteBuffer = new byte[bufferSize];
                    int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                    try
                    {
                        while (bytesRead > 0)
                        {
                            localFileStream.Write(byteBuffer, 0, bytesRead);
                            bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                        }

                        return true;
                    }
                    catch (Exception ex) { MessageBox.Show(ex.Message, "Hata"); }
                    
                    localFileStream.Close();
                    ftpStream.Close();
                    ftpResponse.Close();
                    FTP = null;
                }
                catch (Exception ex) { MessageBox.Show(ex.Message, "Hata"); }
                return false;
            }
            else
                return false;
        }
        
        public bool upload(string dosya_yolu)
        {
            if (NetworkKontrol.InternetKontrol())
            {
                try
                {
                    FileInfo FI = new FileInfo(dosya_yolu);
                    string uri = host + "/" + FI.Name;
                    FTP = (FtpWebRequest)WebRequest.Create(uri);
                    FTP.Credentials = new NetworkCredential(user, pass);
                    FTP.UsePassive = true;
                    FTP.UseBinary = true;
                    FTP.KeepAlive = true;
                    FTP.Method = WebRequestMethods.Ftp.UploadFile;
                    FTP.ContentLength = FI.Length;
                    int buffLength = 2048;
                    byte[] buff = new byte[buffLength];
                    int contentLen;
                    FileStream FS = FI.OpenRead();

                    Stream strm = FTP.GetRequestStream();
                    contentLen = FS.Read(buff, 0, buffLength);
                    while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);
                        contentLen = FS.Read(buff, 0, buffLength);
                    }
                    strm.Close();
                    FS.Close();
                    return true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Hata");
                    return false;
                }

            }
            else
                return false;
        }
        
        public bool delete(string deleteFile)
        {
            if (NetworkKontrol.InternetKontrol())
            {
                try
                {
                    FTP = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
                    FTP.Credentials = new NetworkCredential(user, pass);
                    FTP.UseBinary = true;
                    FTP.UsePassive = true;
                    FTP.KeepAlive = true;
                    FTP.Method = WebRequestMethods.Ftp.DeleteFile;
                    ftpResponse = (FtpWebResponse)FTP.GetResponse();
                    ftpResponse.Close();
                    FTP = null;
                    return true;
                }
                catch (Exception ex) { MessageBox.Show(ex.Message, "Hata"); }
                return false;
            }
            else
                return false;
        }

 

Bir cevap yazın

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