How can I use FTP to move files between directories?

后端 未结 9 1896
日久生厌
日久生厌 2020-12-05 02:47

I have a program that needs to move a file from one directory to another on an FTP server. For example, the file is in:

ftp://1.1.1.1/MAIN/Dir1
9条回答
  •  执念已碎
    2020-12-05 03:37

    U can use this code:

    File original location "ftp://example.com/directory1/Somefile.file".

    File to be moved, "/newDirectory/Somefile.file".

    Note that destination Url not need start with: ftp://example.com

    NetworkCredential User = new NetworkCredential("UserName", "password");
    FtpWebRequest Wr =FtpWebRequest)FtpWebRequest.Create("ftp://Somwhere.com/somedirectory/Somefile.file");
    Wr.UseBinary = true;
    Wr.Method = WebRequestMethods.Ftp.Rename;
    Wr.Credentials = User;
    Wr.RenameTo = "/someotherDirectory/Somefile.file";
    back = (FtpWebResponse)Wr.GetResponse();
    bool Success = back.StatusCode == FtpStatusCode.CommandOK || back.StatusCode == FtpStatusCode.FileActionOK;
    

提交回复
热议问题