본문 바로가기

Programing (프로그래밍)/WPF & C# (C Sharp)

[ WPF & C# (C Sharp)] - FTP 업로드 / 다운로드

728x90
반응형

【 WPF & C# (C Sharp) 】 - FTP 업로드 / 다운로드



@ FTP 업로드 / 다운로드 Upload / Download


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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        // FTP 정보
        private string FtpAddress = "";
        private string FtpId = "";
        private string FtpPw = "";
        private string FtpPath = "";
 
        // FILE 정보
        private string FileName = "";
        private string localPath = @"./";
        
        // 다운로드
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FtpWebRequest requestFileDownload =
                (FtpWebRequest)WebRequest.Create(FtpAddress + FtpPath + FileName);
 
                requestFileDownload.Credentials = new NetworkCredential(FtpId, FtpPw);
                requestFileDownload.Method = WebRequestMethods.Ftp.DownloadFile;
 
                FtpWebResponse responseFileDownload = null;
                responseFileDownload = (FtpWebResponse)requestFileDownload.GetResponse();
 
                Stream responseStream = responseFileDownload.GetResponseStream();
                FileStream writeStream = new FileStream(localPath + FileName, FileMode.Create);
 
                int Length = 2048;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);
 
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }
 
                responseStream.Close();
                writeStream.Close();
 
                requestFileDownload = null;
                responseFileDownload = null;
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }
 
        // / 파일 업로드 버튼
        private void btnUP_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                FtpWebRequest requestFTPUploader =
                (FtpWebRequest)WebRequest.Create(FtpAddress + FtpPath + file_fullname);
 
                requestFTPUploader.Credentials = new NetworkCredential(FtpId, FtpPw);
                requestFTPUploader.Method = WebRequestMethods.Ftp.UploadFile;
 
                FileInfo fileInfo = new FileInfo(s);
                FileStream fileStream = fileInfo.OpenRead();
 
                int bufferLength = 2048;
                byte[] buffer = new byte[bufferLength];
 
                Stream uploadStream = requestFTPUploader.GetRequestStream();
                int contentLength = fileStream.Read(buffer, 0, bufferLength);
 
                while (contentLength != 0)
                {
                    uploadStream.Write(buffer, 0, contentLength);
                    contentLength = fileStream.Read(buffer, 0, bufferLength);
                }
                uploadStream.Close();
                fileStream.Close();
                requestFTPUploader = null;
            }
            catch (Exception error)
            {
                MessageBox.Show(error.Message);
            }
        }
cs



 @ FTP 업/다운로드 이다.

어디스 긁어온 소스인지 모르겠다.

FTP port 부분은 안보인다.

좀 더 정보를 찾아서 보완해야겠다.

일단 내가 가진 정보는 이게 다인듯하다.

잘 쓰겠습니다~ ^^





http://insurang.tistory.com


728x90
반응형