检查FTP上是否存在文件 - 不知道文件的名称 [英] Check if file exists on FTP - Don't know the name of the file
问题描述
我在FTP服务器上收到一个文件,文件的名字是动态生成的。我试图编写一个程序来检查服务器上是否存在任何文件。
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请求=新的WebClient();
request.Credentials = new NetworkCredential(userName,password);
FtpWebRequest ftpRequest =(FtpWebRequest)WebRequest.Create(ftpURL);
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ftpRequest.Credentials = new NetworkCredential(userName,password);
using(FtpWebResponse ftpResponse =(FtpWebResponse)ftpRequest.GetResponse())
{
byte [] newFileData = request.DownloadData(ftpURL.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
string strexist = String.Format(exist);
MessageBox.Show(strexist);
Dts.Variables [User :: FileExists]。Value = true;
}
只有在指定文件名时才能正常工作。无论如何,我可以做一个通配符搜索(* .txt)或搜索任何文件是否在上传文件夹?
任何帮助赞赏!!
您可以从 FTP
列出文件名。像下面这样...
FtpWebRequest请求=(FtpWebRequest)WebRequest.Create(ftpURL);
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response =(FtpWebResponse)request.GetResponse();
using(Stream respStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(respStream);
//从响应
读取每个文件的名称(string fname = reader.ReadLine(); fname!= null; fname = reader.ReadLine())
{
//将文件名添加到列表中
code
如果列表计数为0,那么没有可用的文件。
使用 foreach循环迭代列表值 foreach循环
。并将上面的代码作为一种方法。将文件名传递给方法。
您也可以确保特定文件名存在或不存在于列表中。
注意:在上面的代码中,不需要向 Url 提供文件名。
I receive a file on the FTP server, the name of the file is generated dynamically. I am trying to write a program to check if any file exists on the server.
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.Credentials = new NetworkCredential(userName, password);
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(ftpURL);
ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
ftpRequest.Credentials = new NetworkCredential(userName, password);
using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse())
{
byte[] newFileData = request.DownloadData(ftpURL.ToString());
string fileString = System.Text.Encoding.UTF8.GetString(newFileData);
string strexist = String.Format("exist");
MessageBox.Show(strexist);
Dts.Variables["User::FileExists"].Value = true;
}
This works well only when I specify the "fileName". Is there anyway I can do a wildcard search ("*.txt") or search if anyfile is in the upload folder?
Any help appreciated!!
解决方案 You can list out the file names from the FTP
. Like Below...
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpURL);
request.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse) request.GetResponse();
using (Stream respStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(respStream);
//Read each file name from the response
for (string fname = reader.ReadLine(); fname != null; fname = reader.ReadLine())
{
// Add the file name into a list
}
}
If the list count is 0 then there is no file available. Also you will get the each file name in a list from the single request.
Iterate the list values using foreach loop
. And make the above code as a method. Pass the File name to the method.
You can also do make sure particular file name is exists or not in the list.
Note: In the above code no need to provide the file name to the Url.
这篇关于检查FTP上是否存在文件 - 不知道文件的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!