Click here to Skip to main content
15,896,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i have a problem.

i have this code in my C# Application to download all files from host and save that.


C#
Thread thread = new Thread(() => {
                WebClient client = new WebClient();
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
                client.DownloadFile(new Uri("http://pingservice.ir/update/Gaming-Turkey-Tcp.ovpn"), @"C:\\Program Files\\OpenVPN\\config\\Gaming-Turkey-Tcp.ovpn");
                client.DownloadFile(new Uri("http://pingservice.ir/update/Gaming-Turkey-Udp.ovpn"), @"C:\\Program Files\\OpenVPN\\config\\Gaming-Turkey-Udp.ovpn");
                client.DownloadFile(new Uri("http://pingservice.ir/update/Gaming-Turkey2-Tcp.ovpn"), @"C:\\Program Files\\OpenVPN\\config\\Gaming-Turkey2-Tcp.ovpn");

            });
            thread.Start();


What I have tried:

i want to save all the links in .txt files in my host, then Application download all the links in txt file and save them.

how can i do that?

thanks
Posted
Updated 9-Nov-20 1:27am

1 solution

Should be fairly simple:
C#
WebClient client = new WebClient();
client.DownloadProgressChanged += client_DownloadProgressChanged;
client.DownloadFileCompleted += client_DownloadFileCompleted;

foreach (string url in File.ReadLines(@"C:\Path\To\Your\TextFile.txt"))
{
    if (!Uri.TryCreate(url, UriKind.Absolute, out var uri))
    {
        Console.WriteLine("Invalid URL: '{0}'", url);
        continue;
    }
    
    string fileName = uri.Segments.Last();
    string filePath = System.IO.Path.Combine(@"C:\Program Files\OpenVPN\config", fileName);
    if (System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath);
    
    Console.WriteLine("Download '{0}' to '{1}'...", uri, filePath);
    client.DownloadFile(uri, filePath);
}
Just put each link to download on a new line in the text file.
 
Share this answer
 
Comments
Dariush Gavari 9-Nov-20 12:09pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900