Code not executing correctly in windows forms app

52 views Asked by At

I'm trying to make a windows form application to do the same thing that I have a console application doing. The design is just a box with two labels (going to try to add a progress bar later on) but for some reason the code is not working correctly.

            using System;
            using System.Collections.Generic;
            using System.IO;
            using System.Linq;
            using System.Reflection;
            using System.Text;
            using System.Threading;
            using System.Threading.Tasks;
            using System.Diagnostics;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Windows.Forms;

            namespace WindowsFormsApp1
            {
                public partial class Form1 : Form
                {
                    public Form1()
                    {
                        InitializeComponent();
                    }

                    private void label1_Click(object sender, EventArgs e)
                    {

                    }

                    private void Form1_Load(object sender, EventArgs e)
                    {
                        string date = DateTime.Now.ToString("ddMMyy");
                        string Source = @"G:\Personal\A1";
                        string Destination = @"D:\_Lil USB Backup\" + "b";
                        if (System.IO.Directory.Exists(Source))
                        {
                            if (System.IO.Directory.Exists(Destination))
                            {
                                infoBox.Text = "Backup already exists for today. U rem";
                            }
                            else
                            {
                                System.IO.Directory.CreateDirectory(Destination);
                            }
                            try
                            {
                                CopyAllFiles(Source, Destination);
                                infoBox.Text = "Files backed up successfully.";
                                if (System.IO.Directory.Exists(@"D:\" + date + @"System Volume Information"))
                                {
                                    Directory.Delete(@"D:\" + date + @"System Volume Information", true);
                                    infoBox.Text = "Files backed up successfully.\n System Volume Information folder deleted.\n Backup successfull.";
                                }
                                else
                                {
                                    infoBox.Text = "System Volume Information folder does not exist and therefore was not deleted.\n Backup successfull.";
                                }
                            }
                            catch (Exception ez)
                            {
                                infoBox.Text = "Umm, something didn't work. Oh maybe it was this? " + ez.Message;
                            }
                        }
                        else
                        {
                            infoBox.Text = "Source does not exist, try plugging the USB in dipshit.";
                        }
                    }

                    private static void CopyAllFiles(string Source, string Destination)
                    {
                        try
                        {
                            // Get the subdirectories for the specified directory.
                            DirectoryInfo dir = new DirectoryInfo(Source);
                            DirectoryInfo[] dirs = dir.GetDirectories();
                            if (!dir.Exists)
                            {
                                Label infoBoxErr = new Label();
                                infoBoxErr.Text = "Directory could not be found.";
                            }
                            // If the destination directory doesn't exist, create it.
                            if (!Directory.Exists(Destination))
                            {
                                Directory.CreateDirectory(Destination);
                            }
                            // Get the files in the directory and copy them to the new location.
                            FileInfo[] files = dir.GetFiles();
                            foreach (FileInfo file in files)
                            {
                                string temppath = Path.Combine(Destination, file.Name);
                                file.CopyTo(temppath, true);
                            }
                            foreach (DirectoryInfo subdir in dirs)
                            {
                                string temppath = Path.Combine(Destination, subdir.Name);
                                CopyAllFiles(subdir.FullName, temppath);
                            }
                        }
                        catch (Exception ex)
                        {
                            Label infoBoxErr = new Label();
                            infoBoxErr.Text = "Oh. Something didn't work, sorry. Might have been this: " + ex.Message + " lol!";
                        }
                    }

                    private void progressBar1_Click(object sender, EventArgs e)
                    {

                    }
                }
            }

The problem is that it outputs the text in the labels as expected as if it was successful. Yet it does not create a new folder or copy any of the files. I have no idea why and it's not giving me any errors!

1

There are 1 answers

0
Alexander On BEST ANSWER

First of all you catch exception within CopyAllFiles method. And its code

    catch (Exception ex)
    {
        Label infoBoxErr = new Label();
        infoBoxErr.Text = "Oh. Something didn't work, sorry. Might have been this: " + ex.Message + " lol!";
    }

doesn't do anything because it creates label infoBoxErr in memory and doesn't display it at all.

So, the following code

    try
    {
        CopyAllFiles(Source, Destination);
        infoBox.Text = "Files backed up successfully.";
        if (System.IO.Directory.Exists(@"D:\" + date + @"System Volume Information")) 
        {
            Directory.Delete(@"D:\" + date + @"System Volume Information", true);
            infoBox.Text = "Files backed up successfully.\n System Volume Information folder deleted.\n Backup successfull.";
    }
        else
        {  
            infoBox.Text = "System Volume Information folder does not exist and therefore was not deleted.\n Backup successfull.";
        }
    }
    catch (Exception ez)
    {
        infoBox.Text = "Umm, something didn't work. Oh maybe it was this? " + ez.Message;
    }

never reaches the catch block (because of inner catch in CopyAllFiles method) and just displays success text.

So I'd suggest you to remove that inner catch and see what happens.