c# - WebClient DownloadFileAsync() does not work -
webclient downloadfileasync() not work same url , credentials...
any clue?
static void main(string[] args) { try { var urladdress = "http://mywebsite.com/msexceldoc.xlsx"; using (var client = new webclient()) { client.credentials = new networkcredential("username", "password"); // works fine. client.downloadfile(urladdress, @"d:\1.xlsx"); } /*using (var client = new webclient()) { client.credentials = new networkcredential("username", "password"); // y creats file 0 bytes. dunow why it. client.downloadfileasync(new uri(urladdress), @"d:\1.xlsx"); //client.downloadfilecompleted += new asynccompletedeventhandler(completed); }*/ } catch (exception ex) { } }
you need keep program running while async download completes, runs in thread.
try this, , wait completed before hit enter end program:
static void main(string[] args) { try { var urladdress = "http://mywebsite.com/msexceldoc.xlsx"; using (var client = new webclient()) { client.credentials = new networkcredential("username", "password"); client.downloadfileasync(new uri(urladdress), @"d:\1.xlsx"); client.downloadfilecompleted += new asynccompletedeventhandler(completed); } catch (exception ex) { } console.readline(); } public static void completed(object o, asynccompletedeventargs args) { console.writeline("completed"); }
depending kind of app you're using in, main thread needs keep running while background thread downloads file.
Comments
Post a Comment