File.Copy(SourcePath, TargetPath, IsOverWrite);
(1) :要復制的文件路徑 類型:
(2) :復制后文件存放文件夾 類型:
(3) :是否覆蓋寫入 類型:bool
2. ():
File Source = new File(SourcePath);
Source.CopyTo(TargetPath, IsOverWrite);
(1) :要復制的文件路徑 類型:
(2):復制后文件存放文件夾 類型:
(3):是否覆蓋寫入 類型:bool
(4) :File類的對象 類型:File
但是它們都不能顯示進度條.
對啊,這一搜,沒有一個趁我心的,難道這個就無解了嗎?
當然不.
所以我做了這個教程.
實戰引入:我要做一個復制文件的WPF項目,既追求效率、性能還需要進度條顯示.一、布局
先在你要復制的中(我這里是.xaml),創建
用到的所有控件
序號
類型
名稱
作用
1.
顯示選擇的路徑
2.
打開選擇文件
3.
點擊后復制文件
4.
顯示復制信息
5.
顯示復制進度
6.
打開復制后的文件夾
可以自定義名稱,但是在后面的代碼部分要跟著改.
這里還是貼出我的.xaml

二、主程序 1. 定義變量:
public string FilePath = string.Empty;
public string LocalPath = string.Empty;
private string Creator = Environment.CurrentDirectory + @"\res";
private Thread CopyThread;
注:
(1) :要復制的文件路徑
(2) :復制后文件存放文件夾
(3) :當文件夾不存在時,創建該文件夾所用到的字符串
(4) :復制線程
2. 選擇文件
private void Select_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "請選擇要上傳的文件.";
openFileDialog.Filter = "所有類型的文件|*.*";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = false;
openFileDialog.RestoreDirectory = false;
openFileDialog.CheckFileExists= true;
try
{
openFileDialog.ShowDialog();
FilePath = Path.GetFullPath(openFileDialog.FileName);
if (!Directory.Exists(Creator)) Directory.CreateDirectory(Creator);
LocalPath = Environment.CurrentDirectory + @"\res\" + Path.GetFileName(openFileDialog.FileName);
PathShower.Text = LocalPath;
}
catch (ArgumentException)
{
MessageBox.Show("請選擇一個文件.");
}
catch (Exception)
{
MessageBox.Show("請合理輸入文件路徑.");

}
}
注:
這里主要運用了用于選擇文件(當然文本框也可以).
3. 復制 (1) 復制信息類
public class CopyFileInfo
{
public string SourcePath { get; set; }
public string TargetPath { get; set; }
}
注:
原理很簡單,就是定義兩個屬性,不多贅述.
(2) 復制文件方法
private void CopyFile(object obj)
{
CopyFileInfo? c = obj as CopyFileInfo;
CopyFile(c.SourcePath, c.TargetPath);
}
private void CopyFile(string SourcePath, string TargetPath)
{
FileInfo F = new FileInfo(SourcePath);
FileStream FSR = F.OpenRead();
FileStream FSW = File.Create(TargetPath);
long FileLength = F.Length;
byte[] Buffer = new byte[2097152];
int n = 0;
while (true)
{
DisplayCopyInfo.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,
new Action(UpdateCopyProgress), FileLength, FSR.Position);
n=FSR.Read(Buffer, 0, 2097152);
if (n==0)
{
break;
}
FSW.Write(Buffer, 0, n);
FSW.Flush();
}
FSR.Close();
FSW.Close();
}
這里就是一個很基本的復制文件的方法了.
注:
byte[] = new byte[];
這行代碼中的代表每次復制文件的長度,可以改動,但不能太大,會報錯. 這個速度已經很快了拷貝文件沒有進度條,沒有特殊需求默認即可.
(3) 事件觸發
private void CopyBtn_Click(object sender, RoutedEventArgs e)
{
try
{
CopyProgress.Maximum = new FileInfo(FilePath).Length;
//保存文件信息
CopyFileInfo copy = new CopyFileInfo() { SourcePath = FilePath, TargetPath = LocalPath };
//異步調用
CopyThread = new Thread(new ParameterizedThreadStart(CopyFile));
//實例化線程
CopyThread.Start(copy);
//啟動線程
}
catch
{
MessageBox.Show("請選擇或輸入文件路徑及名稱!");
}

}
4. 更新進度條
private void UpdateCopyProgress(long FileLength, long CurrentLength)
{
DisplayCopyInfo.Text = string.Format("總長度:{0}, 已復制:{1}", FileLength, CurrentLength);
CopyProgress.Value = CurrentLength;
}
注:
這里使用了類型的對象的屬性Value.
5. 打開本地文件夾
public static void command(string input)
{
Process CmdProcess = new Process();
CmdProcess.StartInfo.FileName = "cmd.exe";
CmdProcess.StartInfo.CreateNoWindow = true;
CmdProcess.StartInfo.UseShellExecute = false;
CmdProcess.StartInfo.RedirectStandardInput = true;
CmdProcess.StartInfo.RedirectStandardOutput = true;
CmdProcess.StartInfo.RedirectStandardError = true;
CmdProcess.StartInfo.Arguments = "/c " + input;
CmdProcess.Start();
CmdProcess.WaitForExit();
CmdProcess.Close();
}
private void OpenDirectory_Click(object sender, RoutedEventArgs e)
{
if (!Directory.Exists(Creator)) Directory.CreateDirectory(Creator);
command("explorer \"" + Environment.CurrentDirectory + "\\res\"");
}
注:
這里使用了類來達到打開文件夾的效果.
6. 關閉窗口確認
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
MessageBoxResult result = MessageBox.Show("是否退出?", "退出確認", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (result == MessageBoxResult.Cancel)
{
e.Cancel = true;
}
else
{
Environment.Exit(0);
}
}
注:
需要在xaml里添加關閉事件:="".
三、完整代碼
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
namespace Advanced_Copy
{
public partial class MainWindow : Window
{

public string FilePath = string.Empty;
public string LocalPath = string.Empty;
private string Creator = Environment.CurrentDirectory + @"\res";
private Thread CopyThread;
public MainWindow()
{
InitializeComponent();
}
private void CopyBtn_Click(object sender, RoutedEventArgs e)
{
try
{
CopyProgress.Maximum = new FileInfo(FilePath).Length;
CopyFileInfo copy = new CopyFileInfo() { SourcePath = FilePath, TargetPath = LocalPath };
CopyThread = new Thread(new ParameterizedThreadStart(CopyFile));
CopyThread.Start(copy);
}
catch
{
MessageBox.Show("請選擇或輸入文件路徑及名稱!");
}
}
private void Select_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "請選擇要上傳的文件.";
openFileDialog.Filter = "所有類型的文件|*.*";
openFileDialog.FileName = string.Empty;
openFileDialog.FilterIndex = 1;
openFileDialog.Multiselect = false;
openFileDialog.RestoreDirectory = false;
openFileDialog.CheckFileExists= true;
try
{
openFileDialog.ShowDialog();
FilePath = Path.GetFullPath(openFileDialog.FileName);
if (!Directory.Exists(Creator)) Directory.CreateDirectory(Creator);
LocalPath = Environment.CurrentDirectory + @"\res\" + Path.GetFileName(openFileDialog.FileName);
PathShower.Text = LocalPath;
}
catch (ArgumentException)
{
MessageBox.Show("請選擇一個文件.");
}
catch (Exception)
{
MessageBox.Show("請合理輸入文件路徑.");
}
}
private void CopyFile(object obj)
{
CopyFileInfo? c = obj as CopyFileInfo;
CopyFile(c.SourcePath, c.TargetPath);
}
private void CopyFile(string SourcePath, string TargetPath)
{
FileInfo F = new FileInfo(SourcePath);
FileStream FSR = F.OpenRead();

FileStream FSW = File.Create(TargetPath);
long FileLength = F.Length;
byte[] Buffer = new byte[2097152];
int n = 0;
while (true)
{
DisplayCopyInfo.Dispatcher.BeginInvoke(DispatcherPriority.SystemIdle,
new Action(UpdateCopyProgress), FileLength, FSR.Position);
n=FSR.Read(Buffer, 0, 2097152);
if (n==0)
{
break;
}
FSW.Write(Buffer, 0, n);
FSW.Flush();
}
FSR.Close();
FSW.Close();
}
private void UpdateCopyProgress(long FileLength, long CurrentLength)
{
DisplayCopyInfo.Text = string.Format("總長度:{0}, 已復制:{1}", FileLength, CurrentLength);
CopyProgress.Value = CurrentLength;
}
private void OpenDirectory_Click(object sender, RoutedEventArgs e)
{
if (!Directory.Exists(Creator)) Directory.CreateDirectory(Creator);
Command.command("explorer \"" + Environment.CurrentDirectory + "\\res\"");
}
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
MessageBoxResult result = MessageBox.Show("是否退出?", "退出確認", MessageBoxButton.OKCancel, MessageBoxImage.Question);
if (result == MessageBoxResult.Cancel)
{
e.Cancel = true;
}
else
{
Environment.Exit(0);
}
}
}
public class CopyFileInfo
{
public string SourcePath { get; set; }
public string TargetPath { get; set; }
}
}
四、效果圖
教程到此結束,感謝參考!
Made By On 2022.12.15