北大青鸟练习题之委托的职业应用

SocketUp 2012-06-15 11:57:05
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

namespace WindowsFormsApplication1
{
/// <summary>
/// 继续滥用委托
/// </summary>
/// <param name="sourceFile"></param>
/// <param name="targetFile"></param>
public delegate void CopyProgressHandler(String sourceFile, String targetFile);

/// <summary>
/// 拷贝文件夹类
/// </summary>
public class CopyDirectory
{
/// <summary>
/// 继续滥用事件
/// </summary>
public event CopyProgressHandler CopyProgress;

#region Win32 API

private const int MAX_PATH = 260;
private const int MAX_ALTERNATE = 14;

[StructLayout(LayoutKind.Sequential)]
private struct FILETIME
{
public uint dwLowDateTime;
public uint dwHighDateTime;
};

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct WIN32_FIND_DATA
{
public FileAttributes dwFileAttributes;
public FILETIME ftCreationTime;
public FILETIME ftLastAccessTime;
public FILETIME ftLastWriteTime;
public uint nFileSizeHigh; //changed all to uint from int, otherwise you run into unexpected overflow
public uint nFileSizeLow; //| http://www.pinvoke.net/default.aspx/Structures/WIN32_FIND_DATA.html
public uint dwReserved0; //|
public uint dwReserved1; //v
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string cFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_ALTERNATE)]
public string cAlternate;
}

[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);

[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool FindClose(IntPtr hFindFile);

#endregion

/// <summary>
/// 拷贝
/// </summary>
/// <param name="sourceDirectory">源文件</param>
/// <param name="targetDirectory">目标文件夹</param>
public void Copy(String sourceDirectory, String targetDirectory)
{
IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
WIN32_FIND_DATA findData;

IntPtr findHandle;

findHandle = FindFirstFile(sourceDirectory + "*.*", out findData);
if (findHandle != INVALID_HANDLE_VALUE)
{
do
{
if ((findData.dwFileAttributes & FileAttributes.Directory) != 0)
{
if (findData.cFileName != "." && findData.cFileName != "..")
{
Copy(String.Format("{0}{1}{2}", sourceDirectory, findData.cFileName, "\\"), targetDirectory);
}
}
else
{
String strFile = sourceDirectory + findData.cFileName;
if (File.Exists(strFile))
{
try
{
FileInfo fileInfo = new FileInfo(strFile);
String strCopyTargetFile = String.Format("{0}{1}", targetDirectory, fileInfo.Name);
if (CopyProgress != null)
{
// 触发事件,通知界面
CopyProgress(strFile, strCopyTargetFile);
}
// 拷贝文件
File.Copy(strFile, strCopyTargetFile);
}
catch
{
}
}
}
}
while (FindNextFile(findHandle, out findData));
FindClose(findHandle);
}
}
}
}



using System;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;

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

private void CopyProgress(String sourceFile, String targetFile)
{
if (progressBar1.Value >= progressBar1.Maximum)
{
progressBar1.Value = 0;
}

progressBar1.Value++;
label1.Text = String.Format("从 {0} 拷贝到 {1}", sourceFile, targetFile);
Application.DoEvents();
}

private void Form1_Load(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Maximized;
progressBar1.Dock = DockStyle.Top;
label1.Dock = DockStyle.Top;

String targetDirectory = @"D:\TestCopy\";
if (!System.IO.Directory.Exists(targetDirectory))
{
System.IO.Directory.CreateDirectory(targetDirectory);
}

// 创建一个拷贝文件夹,并且搞好事件
CopyDirectory copyDirectory = new CopyDirectory();
copyDirectory.CopyProgress += CopyProgress;
copyDirectory.Copy(@"C:\Program Files\", targetDirectory);
}
}
}
...全文
215 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
风吹腚腚凉 2012-06-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 的回复:]
其实只要有比较丰富的读代码的经验,一眼就看出了,对方自己写了多少代码,有多少是抄来的。
[/Quote]
有点道理,不过培训机构的下线是老师,老师的下线是学生,重点还是老师的水平和责任心上,虽然牛B的一般都不当老师,但是偶尔也会有那么一两个误入歧途的高手啊
  • 打赏
  • 举报
回复
其实只要有比较丰富的读代码的经验,一眼就看出了,对方自己写了多少代码,有多少是抄来的。
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

这么多青鸟的! 娘的 为什么没有我们伟大的 印度 NIIT
[/Quote]

你以为这类培训真的是以教会学员为己任吗?不就是供人抄袭代码用的嘛!

脱离了学员的基础,没有给他们打基础,这就是这种培训的严重问题。结果其学员你只要使用不到一个月、让其随便做三四个设计就完全明白了、都是驴粪蛋外面光。
threenewbee 2012-06-24
  • 打赏
  • 举报
回复
唉,只是不理解为什么拷贝文件要调用api。
风吹腚腚凉 2012-06-15
  • 打赏
  • 举报
回复
教材升级了都有WINDOWS API了
name506 2012-06-15
  • 打赏
  • 举报
回复
还是挺不错的。
SocketUp 2012-06-15
  • 打赏
  • 举报
回复
NIIT 学费贵啊
  • 打赏
  • 举报
回复
这么多青鸟的! 娘的 为什么没有我们伟大的 印度 NIIT

111,093

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧