2016-11-17 12 views
-2

我知道這個網站上有很多關於我的主題的現有文章,並且已經閱讀了其中的一些文章,但並未回答我的問題。使用c#每天從網站下載文件

很簡單:我想一個解決方案,它可以讓我從網站上下載一個文件,這個約束:

  • 之前下載的文件,我需要指定一個開始和結束日期爲兩個文本框,所以我不能直接使用URL,因爲這個URL會一直改變。

  • 此文件需要每天下載4次,且無需人工干預。

所以,最好的辦法就是像scrpit那樣,例如使用windows scheduler,這個腳本在特定的時間執行並下載文件。

我的問題是:什麼是與C#使用的最好的技術? 我對c#沒有太多的經驗......從我讀的Selenium Framework對WebDriven來說很好。我可以使用它並自動下載嗎? 因爲從我讀的內容來看,用戶需要運行他的項目,但它可以像腳本一樣自動化......我是對的嗎?

還是知道另一種方法呢?

回答

0

我無法測試這個,因爲我的工作機器上沒有C#,但我認爲它會是這樣的。

下面是覆蓋這些選項的例子:

WebClient client = new WebClient(); 
DateTime yesterday = DateTime.Today.AddDays(-1); 
DateTime tomorrow = DateTime.Today.AddDays(1); 
Uri ur = new Uri(http://remotehost.do/images/" + yesterday + "to" + tomorrow + "/excel.xls"); 
client.Credentials = new NetworkCredential("username", "password"); 
client.DownloadProgressChanged += WebClientDownloadProgressChanged; 
client.DownloadDataCompleted += WebClientDownloadCompleted; 
client.DownloadFileAsync(ur, @"C:\path\excel.xls"); 

並實施了回調的功能如下:

void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
{ 
    Console.WriteLine("Download status: {0}%.", e.ProgressPercentage); 
} 

void WebClientDownloadCompleted(object sender, DownloadDataCompletedEventArgs e) 
{ 
    Console.WriteLine("Download finished!"); 
}