首页 > 解决方案 > 代码在 Windows 窗体应用程序中未正确执行

问题描述

我正在尝试制作一个 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)
                    {

                    }
                }
            }

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

标签: c#winforms

解决方案


首先,您在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方法中的内部 catch)并且只显示成功文本。

所以我建议你去掉那个内部的陷阱,看看会发生什么。


推荐阅读