hadi.k hadi.k - 3 months ago 23
C# Question

connect client to server and get a text file from it in C#

I would like to connect automatically from a client to a server with an IP address in C# and get a text file from the server.

What would be the best way to achieve this ?



The simplest way to do so is using "WebClient". See https://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.110).aspx

This class has a function called

public string DownloadString(string address)

You can use this to download the text file to memory. For more Methods (e.g. DownloadFile) visit the given link. Note: This Method might hang the window if executed in the UI-Thread while downloading the content. Either use a second Thread to do the stuff or use the asynchronous methods if possible.

In this case you would rather use this:

public Task<string> DownloadStringTaskAsync(string address)

More Information about Async: https://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx