Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, December 9, 2016 4:01 PM
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
All replies (4)
Friday, December 9, 2016 4:12 PM
MSDN has some samples
For example, https://msdn.microsoft.com/en-us/library/ms229716%28v=vs.110%29.aspx
Monday, December 12, 2016 10:19 AM
Hi YassirCool,
Thank you for posting here.
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.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Thursday, December 15, 2016 3:31 AM
Hi YassirCool,
I configure a FTP and make a simple code to test.
Please try the code I provide from MSDN article.
I have a .txt file like the following. I show the file content in ftp.
I use the code with UserName and PassWord.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace FTP_Check_Files
{
class Program
{
static void Main(string[] args)
{
CheckIfFileExistsOnServer("N/N1/N1.txt");//Here is the path in ftp, you could refer to the screenshot of the .txt file.
}
private static bool CheckIfFileExistsOnServer(string fileName)
{
var request = (FtpWebRequest)WebRequest.Create("ftp://localhost/" + fileName);
request.Credentials = new NetworkCredential("xxxx(UserName)", "xxxx(PassWord)");
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("The file exists."); return true;
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
return false;
}
return false;
}
}
}
Here is the output.
I hope this would be helpful. Please have a try.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].
Friday, December 16, 2016 1:46 AM
Hi YassirCool,
If you question has been solved, please mark the useful reply as answer.
If you have something else, please feel free to contact us.
This will make answer searching easier in the forum and be beneficial to community members as well.
Thanks for your understanding and cooperation.
Best Regards,
Wendy
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].