服务器D盘上传的文件过多,空间剩下很少了,于是想把里面部分文件,大概几万个文件转移到E盘,做了这个小工具。先查询出要转移的文件清单,保存在一个记事本中,如下所示:

接着读取文件名,一个个移动到指定目录中去,winform窗体布局及效果如下:

完整代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading; namespace FileMoveTools
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} #region 目录
private void btnBrowseSrcDir_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
txtSrcDir.Text = dialog.SelectedPath;
}
} private void btnBrowseFile_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Filter = "Text (*.txt)|*.txt;";
openFileDialog.AddExtension = true;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFile.Text = openFileDialog.FileName;
}
}
} private void btnSetDestDir_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
txtDestDir.Text = dialog.SelectedPath;
}
}
#endregion private void btnOK_Click(object sender, EventArgs e)
{
Thread mythread = new Thread(MoveFile);
mythread.IsBackground = true;
mythread.Start();
} private void MoveFile()
{
string srcDir = txtSrcDir.Text.Trim();
string destDir = txtDestDir.Text.Trim();
string files = txtFile.Text.Trim(); #region 验证路径是否存在
if (!Directory.Exists(srcDir))
{
statusMsg.Text = "要迁移的目录不存在";
return;
}
if (!Directory.Exists(destDir))
{
statusMsg.Text = "迁移后的目录不存在";
return;
}
if (!File.Exists(files))
{
statusMsg.Text = "文件清单不存!";
return;
}
#endregion statusMsg.Text = "文件开始迁移..";
int count = 0;
using (StreamReader sr = new StreamReader(files, Encoding.UTF8))
{
string strline = null;
while ((strline = sr.ReadLine()) != null)
{
long diskFreeSpace = GetHardDiskFreeSpace("D");
if (diskFreeSpace <= 5)
{
statusMsg.Text = "硬盘空间剩下5GB,停止迁移文件.";
return;
} string sourceFileName = srcDir + "/" + strline;
string destFileName = destDir + "/" + strline;
if (File.Exists(sourceFileName))
{
count++;
statusMsg.Text = "当前迁移第 " + count + " 个文件"; File.Move(sourceFileName, destFileName);
}
}
}
this.Invoke(new Action(() =>
{
statusMsg.Text = "迁移了 " + count + " 个文件,完成";
}));
} /// <summary>
/// 获取指定驱动器的剩余空间总大小(单位为GB)
/// </summary>
/// <param name="str_HardDiskName">只需输入代表驱动器的字母即可 </param>
/// <returns> </returns>
private static long GetHardDiskFreeSpace(string str_HardDiskName)
{
long freeSpace = new long();
str_HardDiskName = str_HardDiskName + ":\\";
System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives();
foreach (System.IO.DriveInfo drive in drives)
{
if (drive.Name == str_HardDiskName)
{
freeSpace = drive.TotalFreeSpace / (1024 * 1024 * 1024);
}
}
return freeSpace;
}
}
}

winform文件迁移工具的更多相关文章

  1. (WinForm)文件夹状态监控,最小化到托盘,开机自启动

    原文 (WinForm)文件夹状态监控,最小化到托盘,开机自启动 . 文件夾監控(監測文件夾中的文件動態): //MSDN上的例子 public class Watcher { public stat ...

  2. C# winform文件批量转编码 选择文件夹

    C# winform文件批量转编码 选择文件夹 打开指定目录 private void btnFile_Click(object sender, EventArgs e) { OpenFileDial ...

  3. C#窗体WinForm 文件操作

    文件及文件夹操作 C/S:WinForm可以操作客户端文件 Client ServerB/S:浏览器服务 Brower Server 命名空间:using system .IO; 1. File类:文 ...

  4. C#窗体 WinForm 文件操作

    文件及文件夹操作 C/S:WinForm可以操作客户端文件 Client ServerB/S:浏览器服务 Brower Server 命名空间:using system .IO; 1. File类:文 ...

  5. WinForm 文件操作

    文件及文件夹操作 C/S:WinForm可以操作客户端文件 Client ServerB/S:浏览器服务 Brower Server 命名空间:using system .IO; 1. File类:文 ...

  6. Winform文件上传

    近期在做了一个winform的项目的附件上传的需求 最初项目选型的时候有三种 1.使用webservice.webapi上传文件 2,直接保存在数据库中 3.使用共享目录+dos命令 第一种有文件大小 ...

  7. C# Winform 文件编码批量转换工具

    在发布产品程序包时,往往需要对程序文件进行编码检查,写了一个可以批量修改文件编码格式的辅助工具,希望对有同样需求的童鞋有帮助. 1.程序界面: 2.核心代码: /// <summary> ...

  8. [原]C# Winform 文件编码批量转换工具

    在发布产品程序包时,往往需要对程序文件进行编码检查,写了一个可以批量修改文件编码格式的辅助工具,希望对有同样需求的童鞋有帮助. 1.程序界面: 2.核心代码: /// <summary> ...

  9. Winform 文件控件 - 转

    1. OpenFileDialog private void openFileDialogBTN_Click(object sender, System.EventArgs e) { OpenFile ...

随机推荐

  1. html中上标、下标、删除字、小号字等

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 要不要用STL的问题——真理是越辩越明的~

    QtWidgets的维护者 Marc Mutz 有一篇博客比较详尽的介绍了 Qt自己的容器.介绍了何时用什么比较好https://marcmutz.wordpress.com/effective-qt ...

  3. poj1617---columnar encryption

    题意:给出keyword,如BATBOY,A的ascii值最小,所以第二列最先输出,B有两个,左边的先输出,也就是说,接下来输出第一列和第4列, 所以每一个字母都带有一个ascii值和一个序号,用结构 ...

  4. php各类hash算法长度及性能

    Hash结果如下 <?php $data = "hello world"; foreach (hash_algos() as $v) { $r = hash($v, $dat ...

  5. JSP简单练习-获取表单数据

    在JSP中,server端程序与client交互最经常使用的方法就是採用表单提交数据.表单提交的方法主要有两种,一种是get方法.还有一种是post方法.两者最大的差别:使用get方法提交的数据会显示 ...

  6. SQL学习之HAVING过滤分组

    1.SQL除了能用Group By分组数据之外,SQL还允许过滤分组,规定包括那些分组,排除那些分组.例如,你可能想要列出至少有两个订单的所有顾客.为此,必须基于完整的分组而不是个别的行进行过滤. 基 ...

  7. Web定时执行某个方法-网页获取

    实现该功能用到的是System.Timers.Timer 将定时的方法添加到Global.ascx.cs中 public class Global : System.Web.HttpApplicati ...

  8. 有关android源码编译的几个问题

    项目用到编译环境,与源码有些差异不能照搬,关键是连源码都没编译过,下面基本上是行网上照的各种自学成才的分享,病急乱投医了,都记在下面作为参照吧. 1.验证是否编译正确,在终端执行 emulator & ...

  9. SQL语言整理归纳

  10. 第一个输出程序 Console.WriteLine

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...