c# - Check if file exists on FTP - Don't know the name of the file -
I get a file on the FTP server, the name of the file is dynamically generated I have a program on the server I am trying to write a program to see whether or not I have.
string userName = Dts.Variables ["User :: SFTPUsername"]. Value.ToString (); String Password = Dts.Variables ["User: SFTPPassword"]. Value.ToString (); ** string filename = Dts.Variables ["User :: FilePattern"]. Value.ToString (); ** string ftpURL = String.Format ("ftp: //11.11.11/upload/ {0}", filename); WebClient Request = New Webclient (); request. Credential = new network credentials (username, password); FtpWebRequest ftpRequest = (FtpWebRequest) WebRequest.Create (ftpURL); FtpRequest.Method = WebRequestMethods.Ftp.GetFileSize; FtpRequest.Credentials = New network credentials (username, password); (FtpWebResponse ftpResponse = (FtpWebResponse) using ftpRequest.GetResponse ()) {byte [] newFileData = request.DownloadData (ftpURL.ToString ()); String fileString = System.Text.Encoding.UTF8.GetString (newFileData); String strxist = string.format ("exist"); MessageBox.Show (strexist); Dts.Variables ["User :: FileExists"]. Value = true; }
This only works well when I specify "filename". Anyway I can search a wildcard ("* .txt") or search file upload folder Is in or not?
Any help appreciated !!
You can list filenames from FTP
. Like below ...
FtpWebRequest request = (FtpWebRequest) WebRequest.Create (ftpURL); Request.Method = WebRequestMethods.Ftp.List directory; FtpWebResponse response = (FtpWebResponse) request.GetResponse (); (StreamRisstream = Feedback.GetrespanSstream ()) using {StreamReader Reader = New StreamMaster (respStream); // Read the name of each file for response (string fname = reader.ReadLine (); fname! = Null; fname = reader.ReadLine ()) {// Add file name to a list}}
< / Pre>If the list count is 0 then no file is available, apart from this you will get the name of each file in a list from a single request. Repeat list values using
foreach loop
. And make the above code as a methodNote: Pass the file name in this file.
You can also ensure that the special filename exists or not in the list.
Note: file name
Comments
Post a Comment