locked
C# Wait for download to finish and check folder for files and display in list box ( form a dll ) RRS feed

  • Question

  • ok i have a program where it connects to my ftp server downloads a file once downloaded and extracted i want to check the dir path that has just been created and add all the files in there to list box 

    but the download i have created is done in a dll, and my program creates the dir if does not exist and adds a zip once downloaded will extract this zip file and from there i want to show those files in a list box only issue i have here is i am trying to find a way of waiting for the download to complete before i look for those files, 

    the code i have in the dll is this.

      private void wc_completed(object sender, AsyncCompletedEventArgs e)
            {
                _MessageResult.Instance.Append("Download Completed!");
    
                if (!Directory.Exists(Application.StartupPath + "\\Downloaded\\" + "//" +  Kaori_Base.SelectedDownloadName))
                {
                    Directory.CreateDirectory(Application.StartupPath + "\\Downloaded\\" + "\\" + Kaori_Base.SelectedDownloadName + "\\" + Kaori_Base.SelectedDownloadName + ".zip");
                    unzipupdate(Application.StartupPath + "//Downloaded//" + "//" + Kaori_Base.SelectedDownloadName + "\\" + Kaori_Base.SelectedDownloadName + ".zip");
                    richTextBox1.Text = _MessageResult.Instance.Merge();
                    XtraMessageBox.Show("Added : " + Kaori_Base.SelectedDownloadName + " To : " + Application.StartupPath + "//Downloaded//" + " And Unzipped.");
                }
                else
                {
                    unzipupdate(Application.StartupPath + "\\Downloaded\\"  + Kaori_Base.SelectedDownloadName + "\\" + Kaori_Base.SelectedDownloadName + ".zip");
                    richTextBox1.Text = _MessageResult.Instance.Merge();
                    XtraMessageBox.Show("Added : " + Kaori_Base.SelectedDownloadName + " To : " + Application.StartupPath + "//Downloaded//" + " And Unzipped.");
                }
    
    
    
    
                listcollection();
    
                
    
                richTextBox1.Text = _MessageResult.Instance.Merge();
                this.Close();
            }

    this is just what is triggered by the download form in the dll at the end of the download.

    and in the main form i have this, 

                setname();
                frmCDGPlayer cdgplayer = new frmCDGPlayer(@"C:\Users\elfen\Desktop\Karaoke Program\elfenliedtopfan5_update_fix\UpdateTest\bin\Debug\Downloaded\VSHXR\VSHXR\VSHXR-104 - Bloodhound Gang - The Bad Touch.zip");
                cdgplayer.Play();

    have tryied to create a filewatcher class to watch for files and add them to listbox as it downloads does not work, comes up with the following error, 

    System.InvalidOperationException: 'Cross-thread operation not valid: Control 'checkedListBoxControl1' accessed from a thread other than the thread it was created on.'

            public void filemonitor(string path)
            {
                FileSystemWatcher elfensystem = new FileSystemWatcher(path);
    
                elfensystem.EnableRaisingEvents = true;
                elfensystem.IncludeSubdirectories = true;
    
                elfensystem.Created += Elfensystem_Created;
                elfensystem.Changed += Elfensystem_Changed;
    
                
            }
    
            private void Elfensystem_Changed(object sender, FileSystemEventArgs e)
            {
                string paths = Application.StartupPath + "\\Downloaded\\" + Kaori_Base.SelectedDownloadName;
                var fileNames = Directory.GetFiles(paths);
                foreach (var fileName in fileNames)
                {
                    // cbListBox.Items.Add(fileName); // Full path
                    //elfenlist.Add(fileName.Split('\\').Last());
                    add(fileName);
                     // Just filename
                }
            }
    
    
    
            public void add(string fileName)
            {
                checkedListBoxControl1.Items.Add(fileName.Split('\\').Last());
            }

    Ideally i would like to access the run a function once the download has completed but all that stuff is accessed in the dll i created but not sure how to access the function downloaded complete form a dll if that is even possible 

    any help on this would be much appreciated 

    thanks in advance elfenliedtopfan5 

    Sunday, December 13, 2020 8:29 PM

All replies

  • Hi

    The Directory.Exists method checks if the specified directory exists.

    string download= @"C:\MyFiles";        
    // If directory does not exist, don't even try   
    if (Directory.Exists(download))  
    {  
        Directory.Delete(download);  
    }  

    Best Regards.

    Please click the Mark as answer button and vote as helpful if this reply solves your problem.

    Monday, December 14, 2020 12:52 AM