【原創】文件系統-目錄文件快速複製轉移工具
最近因為公司圖文服務器空間緊張,需要將備份目錄下的所有文件按照目錄結構全部轉移,但是原地址的目錄結構保持不變,在網上搜索無果的情況下,決定自己動手製作一個;
UI畫面:
主要用到的命名空間:
using System.IO;
步驟詳解:
1、循環來源目錄的所有目錄及文件,重點取得需要轉移的文件數,以便下一步初始化列舉數;
2、循環來源目錄的所有目錄,同時取得目錄數和文件大小,并產生需要轉移的文件列表string[列舉數];
3、依照文件列表開線程建立目錄并轉移文件(判斷是否覆蓋【5】),計算轉移百分比;
4、這兩個是解決難點,方案兩種,一種同步刪除,但必須有順序,二、在轉移完成后再統一循環處理;我采取的是第二種,特別是刪除目錄時,必須保證目錄里不存在任何文件才能直接刪除;
關鍵代碼:
1、File.Copy(sourceFileName, destFileName, checkBox3.Checked);
2、
Code
DownThread = new Thread(new ThreadStart(StartUpdate));
DownThread.Start();
private void StartUpdate()
{
try
{
toolStripStatusLabel1.Text = "正在初始化文件列表";
CountFiles();
b_IsFinished = false;
try
{
BeginInvoke(new System.EventHandler(UpdateUI), lblFileNum);
BeginInvoke(new System.EventHandler(UpdateUI), lblDownLoad);
toolStripStatusLabel1.Text = "正在分配列舉緩存";
m_Files = NewFiles();//需要更新的文件
if (m_Files != null)
{
b_Started = true;
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
m_intFilesCount = m_Files.Length;
bool b_f = false;
for (int i = 0; i <= m_intFilesCount - 1; i++)
{
if (b_Pause)
{
b_f = false;
break;
}
if (m_Files[i] != null)
{
m_downingFileName = m_Files[i];
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
string root = p_PathFrom;
string sourceFileName = p_PathFrom + m_Files[i];
string destFileName = p_PathTo + m_Files[i];
string NodeFilePath = m_Files[i].Substring(0, m_Files[i].LastIndexOf("\\"));
string deleteDirectory = p_PathFrom + NodeFilePath;
FileInfo fi = new FileInfo(p_PathFrom + "\\" + m_Files[i]);
m_downingFileSize = (int)fi.Length;
subPrgBar();
try
{
if (!Directory.Exists(p_PathTo + NodeFilePath))
{
Directory.CreateDirectory(p_PathTo + NodeFilePath);
}
if (File.Exists(destFileName))
{
}
File.Copy(sourceFileName, destFileName, checkBox3.Checked);
}
catch { }
try
{
if (checkBox1.Checked)
{
if (File.Exists(sourceFileName))
{
File.Delete(sourceFileName);
}
}
if (checkBox2.Checked)
{
if (Directory.Exists(deleteDirectory))
{
Directory.Delete(deleteDirectory);
}
}
}
catch
{ }
Debug.WriteLine(m_Files[i]);
}
b_f = true;
}
if (b_f)//批量刪除目錄
{
for (int i = m_intFilesCount - 1; i >= 0; i--)
{
if (b_Pause)
{
break;
}
if (m_Files[i] != null)
{
if (m_Files[i] != null)
{
m_downingFileName ="正在刪除來源目錄:"+m_Files[i];
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
string root = p_PathFrom;
string sourceFileName = p_PathFrom + m_Files[i];
string destFileName = p_PathTo + m_Files[i];
string NodeFilePath = m_Files[i].Substring(0, m_Files[i].LastIndexOf("\\"));
string deleteDirectory = p_PathFrom + NodeFilePath;
try
{
if (checkBox1.Checked)
{
if (File.Exists(sourceFileName))
{
File.Delete(sourceFileName);
}
}
if (checkBox2.Checked)
{
if (Directory.Exists(deleteDirectory))
{
Directory.Delete(deleteDirectory);
}
}
}
catch
{ }
}
}
}
b_f = false;
}
}
}
catch (Exception ex)
{
BeginInvoke(new EventHandler(OnShowMessage), ex.Message);
return;
}
//UI線程設置label屬性
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
BeginInvoke(new EventHandler(OnShowMessage), "複製成功!");
}
catch(Exception ex)
{
BeginInvoke(new EventHandler(OnShowMessage), "錯誤!" + ex.Message);
}
b_GlobalFinished = true;
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
}
DownThread = new Thread(new ThreadStart(StartUpdate));
DownThread.Start();
private void StartUpdate()
{
try
{
toolStripStatusLabel1.Text = "正在初始化文件列表";
CountFiles();
b_IsFinished = false;
try
{
BeginInvoke(new System.EventHandler(UpdateUI), lblFileNum);
BeginInvoke(new System.EventHandler(UpdateUI), lblDownLoad);
toolStripStatusLabel1.Text = "正在分配列舉緩存";
m_Files = NewFiles();//需要更新的文件
if (m_Files != null)
{
b_Started = true;
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
m_intFilesCount = m_Files.Length;
bool b_f = false;
for (int i = 0; i <= m_intFilesCount - 1; i++)
{
if (b_Pause)
{
b_f = false;
break;
}
if (m_Files[i] != null)
{
m_downingFileName = m_Files[i];
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
string root = p_PathFrom;
string sourceFileName = p_PathFrom + m_Files[i];
string destFileName = p_PathTo + m_Files[i];
string NodeFilePath = m_Files[i].Substring(0, m_Files[i].LastIndexOf("\\"));
string deleteDirectory = p_PathFrom + NodeFilePath;
FileInfo fi = new FileInfo(p_PathFrom + "\\" + m_Files[i]);
m_downingFileSize = (int)fi.Length;
subPrgBar();
try
{
if (!Directory.Exists(p_PathTo + NodeFilePath))
{
Directory.CreateDirectory(p_PathTo + NodeFilePath);
}
if (File.Exists(destFileName))
{
}
File.Copy(sourceFileName, destFileName, checkBox3.Checked);
}
catch { }
try
{
if (checkBox1.Checked)
{
if (File.Exists(sourceFileName))
{
File.Delete(sourceFileName);
}
}
if (checkBox2.Checked)
{
if (Directory.Exists(deleteDirectory))
{
Directory.Delete(deleteDirectory);
}
}
}
catch
{ }
Debug.WriteLine(m_Files[i]);
}
b_f = true;
}
if (b_f)//批量刪除目錄
{
for (int i = m_intFilesCount - 1; i >= 0; i--)
{
if (b_Pause)
{
break;
}
if (m_Files[i] != null)
{
if (m_Files[i] != null)
{
m_downingFileName ="正在刪除來源目錄:"+m_Files[i];
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
string root = p_PathFrom;
string sourceFileName = p_PathFrom + m_Files[i];
string destFileName = p_PathTo + m_Files[i];
string NodeFilePath = m_Files[i].Substring(0, m_Files[i].LastIndexOf("\\"));
string deleteDirectory = p_PathFrom + NodeFilePath;
try
{
if (checkBox1.Checked)
{
if (File.Exists(sourceFileName))
{
File.Delete(sourceFileName);
}
}
if (checkBox2.Checked)
{
if (Directory.Exists(deleteDirectory))
{
Directory.Delete(deleteDirectory);
}
}
}
catch
{ }
}
}
}
b_f = false;
}
}
}
catch (Exception ex)
{
BeginInvoke(new EventHandler(OnShowMessage), ex.Message);
return;
}
//UI線程設置label屬性
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
BeginInvoke(new EventHandler(OnShowMessage), "複製成功!");
}
catch(Exception ex)
{
BeginInvoke(new EventHandler(OnShowMessage), "錯誤!" + ex.Message);
}
b_GlobalFinished = true;
BeginInvoke(new System.EventHandler(UpdateUI), lblStartExe);
}
Code
private void UpdateUI(object sender, EventArgs e)
{
Label lbl = (Label)sender;
lbl.ForeColor = SystemColors.Desktop;
if (b_Started)
{
toolStripStatusLabel1.Text = m_downingFileName.ToString();
}
lblPathNum.Text = m_intPathsCount.ToString() + " 個目錄";
lblFileNum.Text = m_intFilesCount.ToString() + " 個文件";
lblDownLoad.Text = "總計 " + GetFileSize(m_AllFileSize) + "";
lblStartExe.Text = "已經複製 " + GetFileSize(m_downedFileSize) + "";
if (b_GlobalFinished)
{
button1.Enabled = true;
button5.Enabled = false;
}
}
private void UpdateUI(object sender, EventArgs e)
{
Label lbl = (Label)sender;
lbl.ForeColor = SystemColors.Desktop;
if (b_Started)
{
toolStripStatusLabel1.Text = m_downingFileName.ToString();
}
lblPathNum.Text = m_intPathsCount.ToString() + " 個目錄";
lblFileNum.Text = m_intFilesCount.ToString() + " 個文件";
lblDownLoad.Text = "總計 " + GetFileSize(m_AllFileSize) + "";
lblStartExe.Text = "已經複製 " + GetFileSize(m_downedFileSize) + "";
if (b_GlobalFinished)
{
button1.Enabled = true;
button5.Enabled = false;
}
}
計算目錄文件大小合計
public void AllFileSize()
{
int filesize = 0;
DirectoryInfo di = new DirectoryInfo(p_PathFrom);
FileInfo[] fi = di.GetFiles();
int intFiles = fi.Length;
foreach (FileInfo fiTemp in fi)
{
if (b_Pause)
{
break;
}
filesize += (int)fiTemp.Length ;
}
m_AllFileSize = filesize;
DirectoryInfo[] ddi = di.GetDirectories();
foreach (DirectoryInfo diTemp in ddi)
{
if (b_Pause)
{
break;
}
filesize = FilesAllFileSize(filesize, diTemp.FullName);
}
m_AllFileSize = filesize;
}
public int FilesAllFileSize(int filesize, string NodeUpdateServerPath)
{
DirectoryInfo di = new DirectoryInfo(NodeUpdateServerPath);
FileInfo[] fi = di.GetFiles();
int intFiles = fi.Length;
foreach (FileInfo fiTemp in fi)
{
if (b_Pause)
{
break;
}
filesize += (int)fiTemp.Length ;
}
m_AllFileSize = filesize;
DirectoryInfo[] ddi = di.GetDirectories();
foreach (DirectoryInfo diTemp in ddi)
{
if (b_Pause)
{
break;
}
filesize = FilesAllFileSize(filesize, diTemp.FullName);
}
m_AllFileSize = filesize;
return filesize;
}
public void AllFileSize()
{
int filesize = 0;
DirectoryInfo di = new DirectoryInfo(p_PathFrom);
FileInfo[] fi = di.GetFiles();
int intFiles = fi.Length;
foreach (FileInfo fiTemp in fi)
{
if (b_Pause)
{
break;
}
filesize += (int)fiTemp.Length ;
}
m_AllFileSize = filesize;
DirectoryInfo[] ddi = di.GetDirectories();
foreach (DirectoryInfo diTemp in ddi)
{
if (b_Pause)
{
break;
}
filesize = FilesAllFileSize(filesize, diTemp.FullName);
}
m_AllFileSize = filesize;
}
public int FilesAllFileSize(int filesize, string NodeUpdateServerPath)
{
DirectoryInfo di = new DirectoryInfo(NodeUpdateServerPath);
FileInfo[] fi = di.GetFiles();
int intFiles = fi.Length;
foreach (FileInfo fiTemp in fi)
{
if (b_Pause)
{
break;
}
filesize += (int)fiTemp.Length ;
}
m_AllFileSize = filesize;
DirectoryInfo[] ddi = di.GetDirectories();
foreach (DirectoryInfo diTemp in ddi)
{
if (b_Pause)
{
break;
}
filesize = FilesAllFileSize(filesize, diTemp.FullName);
}
m_AllFileSize = filesize;
return filesize;
}