服务器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. css中em与px

    Px是绝对定位   em是相对定位 1. em的值并不是固定的: 2. em会继承父级元素的字体大小. em应用: 1. body选择器中声明Font-size=62.5%:(注:在ie中把body选 ...

  2. MongoDB 安装(一)

    windows 8安装 mongodb 1.下载安装包 http://www.veryhuo.com/down/html/mongodb.html 2.解压到D:/MongoDB目录下. 3.把D:/ ...

  3. android 环境搭建 windows, linux

    android环境也搭建了很多次了,linux下window下.在这里记录下,以后再搭建设置变量啥的就直接看自己的博客就好了.电子挡笔记有时候也不方便 1.下载材料 概述:用的是比较简单的方式搭建环境 ...

  4. Oracle EBS-SQL (SYS-12):查询各Profile的设置情况.sql

    SELECT pro.profile_option_name, pro.user_profile_option_name, lev.level_type TYPE, --lev.level_code, ...

  5. 正版Win7永不崩溃的秘密 解密系统备份!

    备份正版Win7系统 打开Win7系统的控制面板,进入“备份和还原”选项,即可要使用正版Win7系统的备份还原功能. 进入Win7“备份和还原”功能 点击“设置备份”,即可启动Windows 备份程序 ...

  6. Android手势源码浅析-----手势绘制(GestureOverlayView)

    Android手势源码浅析-----手势绘制(GestureOverlayView)

  7. 每天学点Linux:七

    Shell编程 shell变量类型: 只有字符串型,变量可以被赋值,被修改,被引用. shell变量命名方法:第一个字符为字母,其余字符可以是字母,数字或下划线. 变量引用:在变量名前加$号,代表变量 ...

  8. poj2521---lose money

    #include <stdio.h> #include <stdlib.h> int rever(int n) { return -n; } int main() { int ...

  9. Windows Server 2012 R2超级虚拟化之六 Hyper-v Replica 2.0和Live migrations

    Windows Server 2012 R2超级虚拟化之六 Hyper-v Replica 2.0和Live migrations 分钟复制选项也是非常有用的.Hyper-V Replica 2.0在 ...

  10. 网易云课堂_C语言程序设计进阶_第6周:程序结构

    6.1 全局变量 6.2 编译预处理 6.3 大程序结构 6.1 全局变量 全局变量 定义在函数外面的变量是全局变量 全局变量具有全局的生存期和作用域 它们与任何函数都无关 在任何函数内部都可以使用它 ...