1

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

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

例如,当客户访问地址 www.MYWEBSITE.com 时,他可以在列表中看到所有文件名!

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

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

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

4

4 回答 4

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;                           
                        }
                    }
                }
            }
于 2012-10-10T13:52:10.490 回答
0

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

您可以使用DirectoryInfoFileInfo对象相同。

于 2012-10-10T13:19:53.110 回答
0

不要使用 FTP。

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

    <system.web> <asp enableParentPaths="true"> </system.web>

  2. 获取路径:string strFolderPath = Server.MapPath("~/../myfiles");

  3. 使用 Arshad 提供的代码

于 2012-10-10T14:08:58.307 回答
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;
 }

}

于 2012-10-10T14:11:28.683 回答