Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/svn/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC-如何检查ftp服务器上是否存在文件_C#_Asp.net Mvc_Ftp - Fatal编程技术网

C# ASP.NET MVC-如何检查ftp服务器上是否存在文件

C# ASP.NET MVC-如何检查ftp服务器上是否存在文件,c#,asp.net-mvc,ftp,C#,Asp.net Mvc,Ftp,我正在开发一个ASP.NETMVC5.1网站,如果它存在于ftp服务器(CDN)中,我需要显示一张图片。Foldername和filename与规则绑定,因此我事先就知道它们。如果此文件不存在,我需要显示一条消息 如何检查此文件是否存在 没有帮助,因为我需要检查文件是否存在于远程服务器上而不是本地文件夹上。请尝试以下代码 string destination = "ftp://something.com/"; string file = "test.jpg"; string

我正在开发一个ASP.NETMVC5.1网站,如果它存在于ftp服务器(CDN)中,我需要显示一张图片。Foldername和filename与规则绑定,因此我事先就知道它们。如果此文件不存在,我需要显示一条消息

如何检查此文件是否存在

没有帮助,因为我需要检查文件是否存在于远程服务器上而不是本地文件夹上。

请尝试以下代码

  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;
    }
}
请尝试以下代码

  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;
    }
}
请尝试以下代码

  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;
    }
}
请尝试以下代码

  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;
    }
}

这是您的示例问题:这是您的示例问题:这是您的示例问题:这是您的示例问题:谢谢,我也会在需要上传文件时使用此选项。谢谢,我也会在需要上传文件时使用此选项。谢谢,如果我需要上传文件,我也会用这个。谢谢,如果我需要上传文件,我也会用这个。