How to make this code choose the next item in the array when the last one was already used?

StackOverflow https://stackoverflow.com/questions/23162250

  •  05-07-2023
  •  | 
  •  

Question

How to make this code choose the next item in the array when the last one was already used?
With the links it does fine (it seems). It only adds the first filename correctly, others have filename as "System.String[]"

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Diagnostics;
    using System.ComponentModel;

    namespace J98_MP_CS
    {
        class Download
        {
            private static Queue<string> _items = new Queue<string>();
            static string dl_loc = "C:\\ProgramData\\jyrka98\\";        
            public static void Start()
            {
                string[] file_link_array = { 
                                               "LINK1" 
                                               "LINK2"
                                           };       
                foreach (string i in file_link_array) { _items.Enqueue(i); }
                DownloadItem();
            }      
            private static void DownloadItem()
            {
                string[] file_name_array = { 
                                                  "xvm_main_files.zip",
                                                  "jyrka98_xvm.zip",
                                                  "premium_hangar.zip",
                                                  "J1mB0_crosshair.zip",
                                                  "J1mB0_contour_icons_v1.zip",
                                                  "J1mB0_contour_icons_v2.zip",
                                                  "multiline_tank_carousel.zip",
                                                  "white_dead_tanks.zip"
                                              };
                if (_items.Any())
                {
                    var nextItem = _items.Dequeue();

                    WebClient WC = new WebClient();
                    WC.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0");
                    WC.DownloadFileCompleted += new AsyncCompletedEventHandler(WC_DownloadFileCompleted);
                    WC.DownloadFileAsync(new Uri(nextItem), Path.Combine(dl_loc + file_name_array));
                    return;
                }
            }

            private static void WC_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
            {
                DownloadItem();   
            }
        }
    }

*note: i put only two link in here instead of 8 to make code shorter.

Was it helpful?

Solution

Does this help? Introduced a class to hold both the link and the local filename. I assume this is what you want. Then queued up an entry for each link/filename combination. I declared the class at the top just to keep it all together.

    class LinkAndFileName
    {
        public LinkAndFileName(string initLink, string initFileName)
        {
            link = initLink;
            fileName = initFileName;
        }

        public string link { get; set; }
        public string fileName { get; set; }
    }


    private static Queue<LinkAndFileName> _items = new Queue<LinkAndFileName>();
    private static List<string> _results = new List<string>();
    static string dl_loc = "C:\\ProgramData\\jyrka98\\";
    public static void Start()
        {
            LinkAndFileName[] file_link_array = new []
            {
                new LinkAndFileName("LINK1", "xvm_main_files.zip"),
                new LinkAndFileName("LINK2", "jyrka98_xvm.zip"),
                new LinkAndFileName("LINK3", "premium_hangar.zip"),
                new LinkAndFileName("LINK4", "J1mB0_crosshair.zip"),
                new LinkAndFileName("LINK5", "J1mB0_contour_icons_v1.zip"),
                new LinkAndFileName("LINK6", "J1mB0_contour_icons_v2.zip"),
                new LinkAndFileName("LINK7", "multiline_tank_carousel.zip"),
                new LinkAndFileName("LINK8", "white_dead_tanks.zip"),
            };

            foreach (LinkAndFileName fileLink in file_link_array) { _items.Enqueue(fileLink); }
            DownloadItem();
        }

    private static void DownloadItem()
    {
        if (_items.Any())
        {
            LinkAndFileName nextItem = _items.Dequeue();

            WebClient WC = new WebClient();
            WC.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0");
            WC.DownloadFileCompleted += new AsyncCompletedEventHandler(WC_DownloadFileCompleted);
            WC.DownloadFileAsync(new Uri(nextItem.link), Path.Combine(dl_loc + nextItem.fileName));
            return;
        }
    }

    private static void WC_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        DownloadItem();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top