hi all,
i am new to the forum and also new to c# and windows forms.i am trying to develop a windows form which can download multiple zip files by taking URL and filename as input from user.
but getting an exception,

Exception detail,

 System.Reflection.TargetInvocationException was unhandled
  Message="Exception has been thrown by the target of an invocation."
  Source="mscorlib"
  StackTrace:
       at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at downloadmanager.Program.Main() in C:\winformsexp\downloadmanager\downloadmanager\Program.cs:line 17
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

The source code,

using System;
using System.Threading;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using FolderZipper;
using System.Collections;

namespace downloadmanager
{

    public partial class DownloadManagerForm : Form
    {
        int i = 0;

        List<WebClient> wc = new List<WebClient>();
        List<ProgressBar> dwnprogress = new List<ProgressBar>();
        List<Label> dwnpercentage = new List<Label>();
        List<Label> dwnfile = new List<Label>();

        public DownloadManagerForm()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Downloadbutton_Click(object sender, EventArgs e)
        {
            string[] remoteUri =new string[20];
            remoteUri[i]= URLtextBox.Text;
            string[] fileName = new string[20];
            fileName[i]= FileNametextBox.Text;
            string[] myStringWebResource = new string[20];
            string[] mysrcpath = new string[20];
            //mysrcpath[i]= Application.StartupPath + "\\" + fileName;
            char[] separator ={ '.' };
            string[] dir = fileName[i].Split(separator);
            string mydestinationpath = Application.StartupPath + "\\" + dir[0];


            if (i == 0)
            {
                Label downloadingfiles = new Label();
                downloadingfiles.Text = "Downloading Files";
                downloadingfiles.Left = 191;
                downloadingfiles.Top = 230;
                this.Controls.Add(downloadingfiles);
            }
            wc.Add(new WebClient());
            wc[i].DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            wc[i].DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

            // Concatenate the domain with the Web resource filename.
            myStringWebResource[i] = remoteUri[i] + fileName[i];


            wc[i].BaseAddress = "";
            //progressBar1.Visible = true;
            //label1.Text = fileName;


            dwnprogress.Add(new ProgressBar());
            dwnprogress[i].Left = 108;
            dwnprogress[i].Top = this.Size.Height + 30;


            dwnpercentage.Add( new Label());
            dwnpercentage[i].Left = 2;
            dwnpercentage[i].Top = this.Size.Height + 37;


            dwnfile.Add( new Label());
            dwnfile[i].Text = fileName[i] + "//n" + remoteUri[i];
            dwnfile[i].Left = 108;
            dwnfile[i].Top = dwnprogress[i].Top - 4;


            this.Controls.Add(dwnpercentage[i]);
            this.Controls.Add(dwnprogress[i]);
            this.Controls.Add(dwnfile[i]);

            wc[i].DownloadFileAsync(new Uri(myStringWebResource[i]), fileName[i]);
            i++;

            //ZipUtil.ZipFiles("C:\\Documents and Settings\\user1\\Desktop\\tower", "tower", "");

            //Directory.CreateDirectory(mydestinationpath);
            //ZipUtil.UnZipFiles(mysrcpath, mydestinationpath, "


        }


        private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            dwnprogress[i].Value = e.ProgressPercentage;
            dwnpercentage[i].Text = e.ProgressPercentage.ToString() + "%";
        }

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            dwnprogress[i].Visible = false;
            dwnpercentage[i].Text += " Download completed!";
            MessageBox.Show("Download completed!");
        }
    }
}

please do help.i would be very thankful for u.

Edited 3 Years Ago by pyTony: fixed formatting

Try running it in debug mode, and it will show you the line as well as perhaps a slightly more useful error message.

Line 17 it refers to is the { after the form class declaration so is of no help.

You've made a number of assumptions in your code, so, it could be any number of issues.

This article has been dead for over six months. Start a new discussion instead.