2012-07-06 36 views
0

如何使用c#啓用目錄瀏覽功能從URL路徑中查找帶有「* .zip」的文件。 例如: 如果我的網址是:http://www.example.com/myfolder/myfiles 和路徑包含文件:ex1.htm,ex2.zip,ex.pdf,ex.swf, 那麼我如何才能找到具有擴展名* .zip的文件名。 請幫我看看如何在URL中找到壓縮文件的文件名在給定的url路徑中查找zip文件的文件名

+0

這SO後可能會幫助你,它說明了如何解析URL的目錄列表,HTTP://計算器。 com/questions/124492/c-sharp-httpwebrequest-command-to-get-directory-listing。分析是第一步,然後一旦你解析它,你可以過濾.zip文件 – Despertar 2012-07-06 06:19:07

+0

Tinoy,通過「查找文件」,你是否意味着要下載它?如果下載是允許的,那麼我有建議,我想。 – RAJ 2012-07-06 06:34:15

+0

不,我只需要該文件夾中存在的zip文件的文件名。我認爲我們可以使用正則表達式在URL的HTML文檔中查找zip文件。 – 2012-07-06 06:41:35

回答

0

正如邁克爾建議我得到了問題的解決如下:

string urlpath = "http://www.example.com/folder/" 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlpath); 
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
    { 
     using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
     { 
      string html = reader.ReadToEnd(); 
      Regex regEx = new Regex(@".*/(?<filename>.*?)\.zip"); 
      MatchCollection matches = regEx.Matches(html); 
      if (matches.Count > 0) 
      { 
       foreach (Match match in matches) 
       { 
        if (match.Success) 
        { 

        Console.WriteLine(match.Groups["filename"].Value); 
        } 
      } 
     } 

    } 
+0

謝謝邁克爾,Despertar,Akhil&拉吉你的寶貴意見.. – 2012-07-06 09:24:10

-1

你可以用GetFileName來獲取文件名。

例如:

System.IO.Path.GetFileName(path);

OR

你可以嘗試以下從目錄中獲取文件名:

var filenames = String.Join(", ", Directory.GetFiles(@"c:\", "*.zip").Select(filename => Path.GetFileNameWithoutExtension(filename)).ToArray());

+0

其實我的路徑是一個文件夾路徑(http://www.example.com/folderpath/),我需要該目錄路徑中存在的zip文件的文件名。如何獲得zip文件的文件名? – 2012-07-06 05:40:33

+1

所以你可以試試: var filenames = String.Join(「,」,Directory.GetFiles(@「c:\」,「* .zip」)。Select(filename => Path.GetFileNameWithoutExtension(filename))。 ToArray的()); – RAJ 2012-07-06 05:46:11

+0

Directory.GetFiles()函數僅返回目錄中的文件。但是,在這裏我使用的URL路徑中的文件不是目錄..當我們使用URL路徑(http://www.example.com/filderpath/)在Directory.GetFiles..it返回異常無效的URI格式.. – 2012-07-06 05:49:10

0
string[] s = Directory.GetFiles(path); 
     int i = 0; 
     while (i < s.Length) 
     { 
      if (s[i].Substring((s[i].IndexOf(".") + 1), 3).Equals("zip")) 
      { 
       Response.Write(s[i].ToString()); 
       i = i + 1; 
      } 
     } 

試試這個。

編輯

int slashIndex = url.lastIndexOf('/'); 
int dotIndex = url.lastIndexOf('.zip', slashIndex); 
String filenameWithoutExtension; 
if (dotIndex == -1) 
{ 
    filenameWithoutExtension = url.substring(slashIndex + 1); 
} 
else 
{ 
    filenameWithoutExtension = url.substring(slashIndex + 1, dotIndex); 
} 

這適用於在文件名附加在最後的URL/ 我將離開處理你的錯誤/

+0

我們不能使用Directory.GetFiles,因爲這裏使用的是不是目錄的url路徑。我在使用Directory.GetFiles時遇到以下異常:URI格式不受支持。 – 2012-07-06 05:53:03

+0

我不認爲你可以訪問網址內的文件。看看我編輯的答案 – akhil 2012-07-06 06:03:30

+0

嗨Akhil ..我的url路徑不包含zip文件。例如:如果我的網址是http://example.com/folder/那麼這個文件夾路徑中有很多文件...我不知道這個文件夾的文件名..我可以得到這個文件夾中的文件的文件名在收藏列表中? – 2012-07-06 06:08:15

1

除非一個FTP服務器也可以,我認爲你將不得不求助於使用HttpWebRequest加載目錄列表,然後解析結果以提取出<A>標籤中的所有hrefs。

下面是一些示例代碼:

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 
     using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) 
     { 
      using (StreamReader reader = new StreamReader(response.GetResponseStream())) 
      { 
       string html = reader.ReadToEnd(); 
       Console.WriteLine("Parsing {0}", html); 
       Regex regex = new Regex("href=\\\"([^\\\"]*)", RegexOptions.IgnoreCase); 
       MatchCollection matches = regex.Matches(html); 
       if (matches.Count > 0) 
       { 
        foreach (Match match in matches) 
        { 
         if (match.Success) 
         { 
          Console.WriteLine("Found {0}", match.Captures[0]); 
         } 
        } 
       } 
      } 
     } 

注意,你得到的HREF值將是相對於當前目錄。