c# - 代码在 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)
{
}
}
}
问题是它按预期输出标签中的文本,就好像它成功了一样。但它不会创建新文件夹或复制任何文件。我不知道为什么,它没有给我任何错误!
解决方案
首先,您在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)并且只显示成功文本。
所以我建议你去掉那个内部的陷阱,看看会发生什么。
推荐阅读
- python - 在waf中指定不同的编译器
- java - 在使用 Selenium Webdriver 之前无法找到并单击复选框::
- angular - 在承诺茉莉花之后检查“这个”值
- javascript - 迭代和映射 json 并将它们绘制成绘图图
- python - 文本中术语的二阶共现
- laravel-5.7 - Twilio - Laravel 问题 - 创建客户端需要凭据
- javascript - 为什么我在输入中看到 [object Object]?
- julia - Julia 中的 MATLAB interp2 函数
- nlp - 在 Python 中可视化文本数据集中最常见的单词
- cakephp - Cakephp 3: Countercache 与目标模型的条件