FTP检查上传文件的时候存在,如果它在C#将其重命名将其、它在、上传文件、重命名

2023-09-03 20:39:40 作者:落花╰散尽一世繁华

我有一个关于上传到FTP使用C#的问题。

如果该文件存在,那么我想补充像复制或1在文件名后,因此它不会替换该文件我想要做的就是。任何想法?

  VAR请求=(的FtpWebRequest)WebRequest.Create(+目标+文件);
request.Credentials =新的NetworkCredential(,);
request.Method = WebRequestMethods.Ftp.GetFileSize;

尝试
{
    FtpWebResponse响应=(FtpWebResponse)request.GetResponse();
}
赶上(WebException前)
{
    FtpWebResponse响应=(FtpWebResponse)ex.Response;
    如果(response.Status code == FtpStatus code.ActionNotTakenFileUnavailable)
    {

    }
}
 

解决方案

这不是特别优雅,因为我只是把它扔在一起,但我想这是pretty的多少你需要什么?

简单好用的ftp上传工具大全

您只是想不断尝试你的请求,直到你得到一个ActionNotTakenFileUnavailable,让你知道你的文件名是好的,那么就上传了。

 字符串目标=ftp://something.com/;
        字符串文件=test.jpg放在;
        串延伸= Path.GetExtension(文件);
        字符串文件名= file.Remove(file.Length  -  extention.Length);
        字符串fileNameCopy =文件名;
        INT尝试= 1;

        而(!CheckFileExists(的GetRequest(目标+//+ fileNameCopy +延伸)))
        {
            fileNameCopy = +文件名(+ attempt.ToString()+);
            尝试++;
        }

        //做你的上传,我们已经有了一个名字没关系
    }

    私有静态的FtpWebRequest的GetRequest(字符串uriString中)
    {
        VAR请求=(的FtpWebRequest)WebRequest.Create(uriString中);
        request.Credentials =新的NetworkCredential(,);
        request.Method = WebRequestMethods.Ftp.GetFileSize;

        返回请求;
    }

    私有静态布尔checkFileExists(WebRequest的要求)
    {
        尝试
        {
            request.GetResponse();
            返回true;
        }
        抓住
        {
            返回false;
        }
    }
 

编辑:已更新,这将适用于任何类型的Web请求,是一个小更薄

I have a question about Uploading to a FTP with C#.

What I want to do is if the file exists then I want to add like Copy or a 1 after the filename so it doesn't replace the file. Any Ideas?

var request = (FtpWebRequest)WebRequest.Create(""+destination+file);
request.Credentials = new NetworkCredential("", "");
request.Method = WebRequestMethods.Ftp.GetFileSize;

try
{
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
    FtpWebResponse response = (FtpWebResponse)ex.Response;
    if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
    {

    }
}

解决方案

It's not particularly elegant as I just threw it together, but I guess this is pretty much what you need?

You just want to keep trying your requests until you get a "ActionNotTakenFileUnavailable", so you know your filename is good, then just upload it.

        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(WebRequest request)
    {
        try
        {
            request.GetResponse();
            return true;
        }
        catch
        {
            return false;
        }
    }

Edit: Updated so this will work for any type of web request and is a little slimmer.