服务器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. binwalk windows安装和使用方法

    binwalk是一个文件的分析工具,旨在协助研究人员对文件进行分析,提取及逆向工程.简单易用,完全自动化脚本,并通过自定义签名,提取规则和插件模块,还重要一点的是可以轻松地扩展. 以上是binwalk ...

  2. select,poll,epoll之api笔记

    参考:http://www.cnblogs.com/Anker/p/3265058.html select /* According to POSIX.1-2001 */ #include <s ...

  3. Esxi主机虚拟机迁移注意事项

    1. Esxi主机上的虚拟机迁移只能是低----->高,或版本一样的才能进行迁移 [如Esxi5.1---->Esxi5.5]ok, 而Esxi5.5----->Esxi5.1 no ...

  4. 17.java.lang.CloneNotSupportedException

    java.lang.CloneNotSupportedException不支持克隆异常 当没有实现Cloneable接口或者不支持克隆方法时,调用其clone()方法则抛出该异常.

  5. CSS转载备忘

    原文地址:http://www.cnblogs.com/coffeedeveloper/p/3145790.html#html 转载内容: 对CSS中的Position.Float属性的一些深入探讨 ...

  6. 深入解析SSD中MLC与SLC的性能差异

    固态硬盘(Solid State Disk或Solid State Drive),也称作电子硬盘或者固态电子盘,是由控制单元和固态存储单元(DRAM或FLASH芯片)组成的硬盘. 固态硬盘的接口规范和 ...

  7. C语言入门(7)——自定义函数

    C源程序是由函数组成的.虽然在C语言入门系列前面几篇的程序中大都只有一个主函数main(),但实用程序往往由多个函数组成.函数是C源程序的基本模块,通过对函数模块的调用实现特定的功能.C语言中的函数相 ...

  8. 调用父类Controller错误

    在写一个控制器的时候,要特别注意本类继承的父类.不要继承错了.如图: ,这样就会一直是显示父类的控制器,而不是显示本类的控制器视图. 应该改为: 这些都是平时遇到的一些小问题,留着提醒自己.

  9. ASP.NET MVC4 ASP.NET Web API路由规则

    using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Web.Ht ...

  10. ubuntu10.04 安装NVIDIA GT 420M驱动

    安装ubuntu已经好几天了,由于显卡驱动没装,屏幕在600X800下的效果很难看,于是就想办法,查阅资料终于安装成功了,下面将我的安装方法记录下来以供大家参考. 借鉴:ubuntu12.04下安装N ...