在Windows Forms应用程序中代码未正确执行

时间:2019-01-20 22:20:01

标签: c# winforms

我正在尝试使Windows窗体应用程序执行与控制台应用程序相同的操作。设计只是一个带有两个标签的盒子(稍后将尝试添加进度条),但是由于某种原因,代码无法正常工作。

            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)
                    {

                    }
                }
            }

问题在于,它会像预期的那样成功输出标签中的文本。但是,它不会创建新文件夹或复制任何文件。我不知道为什么,而且没有给我任何错误!

1 个答案:

答案 0 :(得分:0)

首先,您在 CopyAllFiles 方法中捕获了异常。及其代码

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

不执行任何操作,因为它在内存中创建了标签 infoBoxErr ,并且根本不显示它。

因此,以下代码

    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;
    }

永远不会到达 catch 块(由于 CopyAllFiles 方法中的内部捕获),只会显示成功文本。

所以我建议您删除该内部陷阱,然后看看会发生什么。