s

wpf download file from website from wpf application edit button Edit

author
Pooran Prasad Rajanna | calendar 08 September 2020 | 3186

simple.. below function downloads the file from the website, reads the content assuming it is a text file, and returns the content.

WPF Sample Code

private string DownloadTemplate(string baseurl, string filename)
{
            string strtemplatedata = "";
            try
            {

                WebClient Client = new WebClient();
                Client.DownloadFile(baseurl   filename, filename);

                StreamReader re = File.OpenText(filename);
                string input = null;
                while ((input = re.ReadLine()) != null)
                {
                    strtemplatedata = strtemplatedata   input;
                }
                re.Close();

            }
            catch (Exception ex)
            {
               MessageBox.Show("Error while fetching the file" filename "\r\n"   ex.Message   "\r\n"   ex.StackTrace);                              
            }

            return strtemplatedata;
}

Use System.Net namespace before executing.