Uploading folders in web using ftp in c# with replacing existing folders
Hi 
I have a C# project for uploading files and folders using FTP.Can any one suggest a code for replacing existing folder while uploading.

See All Responses Below...
Author
Message Rating
Posted on: 16/07/2013
You cannot replace folders with a same folder name, but you can replace files. In order to replace files, issue a delete command and then issue add file command, this is a simple method..

Tom

Posted on: 16/07/2013
Try this:        
string destination = "ftp://something.com/";
        string file = "test.jpg";
        string extention = Path.GetExtension(file);
        string fileName = file.Remove(file.Length - extention.Length);
        string fileNameCopy = fileName;
        int attempt = 1;

        while (!CheckFileExists(GetRequest(destination + "//" + fileNameCopy + extention)))
        {
            fileNameCopy = fileName + " (" + attempt.ToString() + ")";
            attempt++;
        }

        // do your upload, we've got a name that's OK
    }

    private static FtpWebRequest GetRequest(string uriString)
    {
        var request = (FtpWebRequest)WebRequest.Create(uriString);
        request.Credentials = new NetworkCredential("", "");
        request.Method = WebRequestMethods.Ftp.GetFileSize;

        return request;
    }

    private static bool CheckFileExists(FtpWebRequest request)
    {
        try
        {
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            return true;
        }
        catch (WebException ex)
        {
            FtpWebResponse response = (FtpWebResponse)ex.Response;
            if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
            {
                return false;
            }
            else
            {
                return false;
            }
        }
    }
Share this page across other social networking sites



Please Login to Post Your Comments
Name (Max. 100 characters)
Please post your comments here
Select Language
Comments
Attach File(Max. Size: 2 MB)
A few simple rules when posting your Comments,
  1. Please post only answers relevant to the topic of discussion.
  2. Please dont misuse this site or do not be abusive, offensive, inappropriate,harass anyone on the boards or post ads or spam. Doing so will delete your inappropriate messages and will block or delete your account on this site.