动态获取我的网站服务器(ftp)中的文件夹中的文件名

时间:2012-10-10 13:12:39

标签: c# asp.net vb.net

我上传了一些文件,例如file1.pdf和file2.pdf以及...在我的网络服务器上 - > ftp.MYWEBSITE.net\wwwroot\myfiles\

现在我想获取所有这些文件名并动态地在我的网站上显示它们,

例如,当客户去往www.MYWEBSITE.com时,他可以看到列表中的所有文件名!

请注意,MYWEBSITE已上传到wwwroot\mywebsite\default.aspx

我应该使用ftp连接ftp用户名和密码吗?或者我可以直接转到\ myfiles路径而不创建新的ftp连接?

我应该在asp.net中使用任何控件吗?

4 个答案:

答案 0 :(得分:2)

您可以使用以下代码:

 void GetFiles()
        {
            DirectoryInfo d= new DirectoryInfo(strFolderPath);
            var files = d.GetFiles("*.pdf*");
            FileInfo[] subfileInfo = files.ToArray<FileInfo>();

            if (subfileInfo.Length > 0)
            {
                for (int j = 0; j < subfileInfo.Length; j++)
                {
                    bool isHidden = ((File.GetAttributes(subfileInfo[j].FullName) & FileAttributes.Hidden) == FileAttributes.Hidden);
                    if (!isHidden)
                    {
                        string strExtention = th.GetExtension(subfileInfo[j].FullName);
                        if (strExtention.Contains("pdf"))
                        {                            
                            string path = subfileInfo[j].FullName;
                            string name = bfileInfo[j].Name;                           
                        }
                    }
                }
            }

答案 1 :(得分:0)

如果您的ftp和网站都在同一台服务器上,您可以使用.net库中的文件对象读取文件列表,并将其绑定到ASP.NET中的转发器控件

您可以使用DirectoryInfoFileInfo个对象。

答案 2 :(得分:0)

不要使用FTP。

  1. 配置启用父路径:http://msdn.microsoft.com/en-us/library/ms524697%28v=vs.90%29.aspx

    &LT;&的System.Web GT; &lt; asp enableParentPaths =“true”&gt; &LT; /system.web>

  2. 获取路径: string strFolderPath = Server.MapPath(“〜/../ myfiles”);

  3. 使用Arshad提供的代码

答案 3 :(得分:0)

您可以尝试使用此代码

class Program
{
    static void Main(string[] args)
    {
        FTPClient client = new FTPClient("ftp://localhost", "ftpUser", "ftpPass");
        List<string> files = client.DirectoryListing();
        foreach (string s in files)
        {
            Console.WriteLine(s);
        }
        Console.ReadLine();
    }
}

public class FTPClient
{
  // The hostname or IP address of the FTP server
  private string _remoteHost;

  // The remote username
  private string _remoteUser;

  // Password for the remote user
  private string _remotePass;

  public FTPClient(string remoteHost, string remoteUser, string remotePassword)
  {
    _remoteHost = remoteHost;
    _remoteUser = remoteUser;
    _remotePass = remotePassword;
  }


  public List<string> DirectoryListing()
  {
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(_remoteHost);
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    request.Credentials = new NetworkCredential(_remoteUser, _remotePass);
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    Stream responseStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(responseStream);

    List<string> result = new List<string>();

    while (!reader.EndOfStream)
    {
        result.Add(reader.ReadLine());
    }

    reader.Close();
    response.Close();
    return result;
 }

}