关于.net 下ftp操作的心得

主要利用了FtpWebRequset和FtpWebResponse两个类,实现了Ftp操作总创建目录,上传文件,删除文件,删除目录的功能的封装。

void CreateUploadDirectory(string uri, string user, string password)
    {
        try
        {
            string mapUri = string.Format("{0}Maps/", uri);
            string photoUri = string.Format("{0}Photos/", uri);
            CreateDirectory(uri, user, password);
            CreateDirectory(photoUri, user, password);
            CreateDirectory(mapUri, user, password);
        }
        catch(Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    void CreateDirectory(string uri, string user, string password)
    {
        try
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            ftpRequest.Credentials = new NetworkCredential(user, password);
            ftpRequest.KeepAlive = false;
            ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    void DeleteUploadDirectory(string uri, string user, string password)
    {
        try
        {
            string mapUri = string.Format("{0}Maps/", uri);
            string photoUri = string.Format("{0}Photos/", uri);
            DeleteDirectory(mapUri, user, password);
            DeleteDirectory(photoUri, user, password);
            DeleteDirectory(uri, user, password);
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    void DeleteDirectory(string uri, string user, string password)
    {
        try
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            ftpRequest.Credentials = new NetworkCredential(user, password);
            ftpRequest.KeepAlive = false;
            ftpRequest.Method = WebRequestMethods.Ftp.RemoveDirectory;
            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        }
        catch(Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    void DeleteFile(sting uri, string user, string password)
    {
        try
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            ftpRequest.Credentials = new NetworkCredential(user, password);
            ftpRequest.KeepAlive = false;
            ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    void DeleteFiles(string uri, string user, string password)
    {
        try
        {
            FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            ftpRequest.Credentials = new NetworkCredential(user, password);
            ftpRequest.KeepAlive = false;
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            Stream stream = ftpResponse.GetResponseStream();
            StreamReader reader = new StreamReader();
            string filename = reader.ReadLine();
            if(filename != null)
            {

            }
        }
    }

你可能感兴趣的:(关于.net 下ftp操作的心得)