winform文件迁移工具
服务器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文件迁移工具的更多相关文章
- (WinForm)文件夹状态监控,最小化到托盘,开机自启动
原文 (WinForm)文件夹状态监控,最小化到托盘,开机自启动 . 文件夾監控(監測文件夾中的文件動態): //MSDN上的例子 public class Watcher { public stat ...
- C# winform文件批量转编码 选择文件夹
C# winform文件批量转编码 选择文件夹 打开指定目录 private void btnFile_Click(object sender, EventArgs e) { OpenFileDial ...
- C#窗体WinForm 文件操作
文件及文件夹操作 C/S:WinForm可以操作客户端文件 Client ServerB/S:浏览器服务 Brower Server 命名空间:using system .IO; 1. File类:文 ...
- C#窗体 WinForm 文件操作
文件及文件夹操作 C/S:WinForm可以操作客户端文件 Client ServerB/S:浏览器服务 Brower Server 命名空间:using system .IO; 1. File类:文 ...
- WinForm 文件操作
文件及文件夹操作 C/S:WinForm可以操作客户端文件 Client ServerB/S:浏览器服务 Brower Server 命名空间:using system .IO; 1. File类:文 ...
- Winform文件上传
近期在做了一个winform的项目的附件上传的需求 最初项目选型的时候有三种 1.使用webservice.webapi上传文件 2,直接保存在数据库中 3.使用共享目录+dos命令 第一种有文件大小 ...
- C# Winform 文件编码批量转换工具
在发布产品程序包时,往往需要对程序文件进行编码检查,写了一个可以批量修改文件编码格式的辅助工具,希望对有同样需求的童鞋有帮助. 1.程序界面: 2.核心代码: /// <summary> ...
- [原]C# Winform 文件编码批量转换工具
在发布产品程序包时,往往需要对程序文件进行编码检查,写了一个可以批量修改文件编码格式的辅助工具,希望对有同样需求的童鞋有帮助. 1.程序界面: 2.核心代码: /// <summary> ...
- Winform 文件控件 - 转
1. OpenFileDialog private void openFileDialogBTN_Click(object sender, System.EventArgs e) { OpenFile ...
随机推荐
- ■[iOS] Interface type cannot be statically allocated の原因と対応
iOSでの開発をしていると.表題のエラーが起こる場合があります. 原因 変数の型が静的割り当てになっていることが原因. 対応 変数の型をポインタ型にすると.エラーがなくなります.(変数の前に*をつける ...
- C#学习日志 day8 -------------- async await 异步方法入门(引用博客)以及序列化和反序列化的XML及json实现
首先是异步方法的介绍,这里引用自http://www.cnblogs.com/LoveJenny/archive/2011/11/01/2230933.html async and await 简单的 ...
- qt windows分发工具使用(windoployqt)
在qt的安装目录下:QTDIR/bin/windeployqt 例如我的默认安装在: C:\Qt\Qt5.3.1\5.3\msvc2013 windoployqt在: C:\Qt\Qt5.3.1\ ...
- Solr学习(2) Solr4.2.0+IK Analyzer 2012
Solr学习(二) Solr4.2.0+IK Analyzer 2012 开场白: 本章简单讲述如何在solr中配置著名的 IK Analyzer 分词器. 本章建立在 Solr学习(一) 基础上进 ...
- sublime text 插件开发
前言:术语和参考资料 sublime text 2的扩展模式相当的丰富.有多种方法可以修改语法高亮模式以及所有的菜单等.它还可以创建一个新的build系统,自动补全,语言定义,代码片段,宏定义,快捷键 ...
- Java面试题之四
十六.运行时异常与一般异常有何异同 Java提供了两类主要的异常:runtime exception和checked exception. 1.checked exception:这种异常也就是我们 ...
- css3幻灯片换位效果
<title>css3幻灯片换位效果</title> <style type="text/css"> .flowGallery {width: ...
- JavaScript 开发经验整理
前言 今年接触了一个B/S的项目,总结了一些JavaScript开发经验,整理些有用的内容与大家分享. 本文会持续更新... 1.实现代码访问的控制 随着项目JavaScript代码库扩大,本应被控制 ...
- C++ 文件操作实例
图1 文件个数及名称 图2 文件内容 背景:如图1所示,现有9个要处理的文件,每个文件的内容格式如图2所示,仅仅只是数值部分不同. 问题:如何提取每个文件中的相同属性的数值到同一个文件中? 输出示例: ...
- .NET平台和C#语言
.NET平台的作用C#语言通过.NET平台来编写 部署 运行.NET应用程序. 为什么学习.NET语言1. C#语言是为.NET平台而生的.2. C#语言是完全面向对象的语言,所以一般情况下我们用C# ...