如何检查FTP服务器中其他文件夹中是否存在文件 [英] How To Check If a File Exists In FTP Server in different folder
问题描述
我有两个变量,它们包含ftp中文件的路径:
第一个名为 file1的变量包含该值:ds/product/Jan/09122016_product.csv
第二个名为file2的变量包含该值:ds/subproduct/Jan/09122016_subproduct.csv
第三个名为 file3的变量包含该值:ds/category/Jan/09122016_category.csv
每个文件位于不同的文件夹中,但位于同一服务器上
我想要的是检查ftp中是否存在3个文件
谢谢
Hi,
I have two variable that contains the path of the file in the ftp:
the first variable called file1 contains that value : ds/product/Jan/09122016_product.csv
the second variable called file2 contains that value : ds/subproduct/Jan/09122016_subproduct.csv
the third variable called file3 contains that value : ds/category/Jan/09122016_category.csv
each file is in different folder but the same server
what i want is to check if the 3 files exists in the ftp
Thanks
推荐答案
嗨YassirCool,
Hi YassirCool,
谢谢您在这里发布.
对于您的问题,请尝试以下代码.
For your question, please try the following code.
private bool CheckIfFileExistsOnServer(string fileName)
{
var request = (FtpWebRequest)WebRequest.Create("ftp://www.server.com/" + fileName);
request.Credentials = new NetworkCredential("username", "password");
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
return true;
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
return false;
}
return false;
}
我希望这会对您有所帮助.
I hope this would be helpful to you.
最好的问候,
温迪
这篇关于如何检查FTP服务器中其他文件夹中是否存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!