c#实现ftp功能

 string ftpServerIP = "apssdb";
        
string ftpUserID = "ebideai";
        
string ftpPassword = "ebideai";

        
private void Upload(string filename)
        
{
            FileInfo fileInf 
= new FileInfo(filename);
            
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
            FtpWebRequest reqFTP;

            reqFTP 
= (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

            reqFTP.Credentials 
= new NetworkCredential(ftpUserID, ftpPassword);

            reqFTP.KeepAlive 
= false;

            reqFTP.Method 
= WebRequestMethods.Ftp.UploadFile;

            reqFTP.UseBinary 
= true;

            reqFTP.ContentLength 
= fileInf.Length;

            
int buffLength = 2048;

            
byte[] buff = new byte[buffLength];
            
int contentLen;

            FileStream fs 
= fileInf.OpenRead();
            
try
            
{
                Stream strm 
= reqFTP.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();

                MessageBox.Show(
"success""Upload Error");
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message, 
"Upload Error");
            }

        }


        
private void Download(string filePath, string fileName)
        
{
            FtpWebRequest reqFTP;

            
try
            
{
                FileStream outputStream 
= new FileStream(filePath + "\\" + fileName, FileMode.Create);

                reqFTP 
= (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));

                reqFTP.Method 
= WebRequestMethods.Ftp.DownloadFile;

                reqFTP.UseBinary 
= true;

                reqFTP.Credentials 
= new NetworkCredential(ftpUserID, ftpPassword);

                FtpWebResponse response 
= (FtpWebResponse)reqFTP.GetResponse();

                Stream ftpStream 
= response.GetResponseStream();

                
long cl = response.ContentLength;

                
int bufferSize = 2048;

                
int readCount;

                
byte[] buffer = new byte[bufferSize];

                readCount 
= ftpStream.Read(buffer, 0, bufferSize);

                
while (readCount > 0)
                
{
                    outputStream.Write(buffer, 
0, readCount);

                    readCount 
= ftpStream.Read(buffer, 0, bufferSize);
                }


                ftpStream.Close();

                outputStream.Close();

                response.Close();
            }

            
catch (Exception ex)
            
{
                MessageBox.Show(ex.Message);
            }

        }


        
private void button3_Click(object sender, EventArgs e)
        
{
            OpenFileDialog op 
= new OpenFileDialog();
            op.RestoreDirectory
=true;
            
if (DialogResult.OK==op.ShowDialog())
            
{
            textBox1.Text
=op.FileName;
            }


            
//Upload(textBox1.Text);
            Upload(textBox1.Text);


        }


        
private void button4_Click(object sender, EventArgs e)
        
{
            Download(
@"d:""table 091901.sql");
        }
 
http://www.cnblogs.com/kerryking/archive/2007/07/10/813079.aspx
posted @ 2007-09-24 18:10  Nina  阅读(561)  评论(0编辑  收藏  举报