客户这边需要往服务器上传PDF文件。然后PDF文件很多,需要挑出来的PDF文件也不少。因此做了个小工具。

功能很简单,选定源文件夹,选定记录着要提取的文件的excel 文件。OK ,界面如下。

XAML代码如下

<Window x:Class="文件迁移工具.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:文件迁移工具"
mc:Ignorable="d"
Title="MainWindow" Height="670.266" Width="911.817"> <Grid Margin="0,0,-8,1">
<Grid.RowDefinitions>
<RowDefinition Height="400*"/>
<RowDefinition Height="239*"/>
</Grid.RowDefinitions>
<Label x:Name="label" Content="请选择文件夹:" HorizontalAlignment="Left" Margin="78,53,0,0" VerticalAlignment="Top" FontSize="21.333"/>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="22" Margin="253,63,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="491"/>
<Button x:Name="button" Content="读 取" HorizontalAlignment="Left" Margin="758,63,0,0" VerticalAlignment="Top" Width="79" Height="22" Click="button_Click"/>
<DataGrid AutoGenerateColumns="False" Name="dataGrid1" VerticalAlignment="Top"
CanUserSortColumns="False" Margin="78,194,75,0" IsReadOnly="True"
CanUserResizeColumns="False" CanUserResizeRows="False" SelectionMode="Single"
CanUserReorderColumns="False" AlternationCount="2" Height="371" RowHeaderWidth="0" CanUserAddRows="False" Grid.RowSpan="2" >
<DataGrid.Columns>
<DataGridTextColumn Header="序号" Width="150" Binding="{Binding id}"/>
<DataGridTextColumn Header="文件夹下所有文件" Width="540" Binding="{Binding Name}"/> </DataGrid.Columns>
</DataGrid>
<Label x:Name="label_Copy" Content="请选择EXCLE:" HorizontalAlignment="Left" Margin="78,103,0,0" VerticalAlignment="Top" FontSize="21.333"/>
<TextBox x:Name="textBox_Copy" HorizontalAlignment="Left" Height="22" Margin="253,116,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="491"/>
<Button x:Name="button_Copy" Content="读 取" HorizontalAlignment="Left" Margin="758,116,0,0" VerticalAlignment="Top" Width="79" Height="22" Click="button2_Click"/>
<Button x:Name="button1" Content="开 始 提 取" HorizontalAlignment="Left" Margin="253,152,0,0" VerticalAlignment="Top" Width="490" Height="27" Click="button1_Click"/>
<Label x:Name="lblmsg" Content="" HorizontalAlignment="Left" Margin="413,191,0,0" Grid.Row="1" VerticalAlignment="Top"/>
<Button x:Name="button2" Content="只显示失败文件" HorizontalAlignment="Left" Margin="741,191,0,0" Grid.Row="1" VerticalAlignment="Top" Width="96" Height="25" Click="button2_Click_1"/> </Grid> </Window>

后台CS如下:

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace 文件迁移工具
{
class FileName
{
public int id { get; set; }
public string Name { get; set; }
}
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
int id = -;
List<FileName> lost = new List<FileName>();//失败集合
public MainWindow()
{
InitializeComponent();
}
List<FileName> list = new List<FileName>();
private void button_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.FolderBrowserDialog fd = new System.Windows.Forms.FolderBrowserDialog();
fd.ShowNewFolderButton = false;
System.Windows.Interop.HwndSource source = PresentationSource.FromVisual(this) as System.Windows.Interop.HwndSource;
System.Windows.Forms.IWin32Window win = new OldWindow(source.Handle); System.Windows.Forms.DialogResult result = fd.ShowDialog(win);
if (result.Equals(System.Windows.Forms.DialogResult.OK))
{
textBox.Text = fd.SelectedPath;
GetAllDirectories(textBox.Text);
}
} private List<FileName> GetAllDirectories(string rootPath) { string[] subPaths = System.IO.Directory.GetDirectories(rootPath);//得到所有子目录
foreach (string path in subPaths)
{
GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List
}
string[] files = System.IO.Directory.GetFiles(rootPath);
foreach (string file in files)
{
id++;
FileName model = new FileName();
model.Name = file.ToUpper();
model.id = id;
list.Add(model);//将当前目录中的所有文件全名存入文件List
}
return list;
} private void button2_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dialog =
new Microsoft.Win32.OpenFileDialog();
dialog.Filter = "Excel File(*.xlsx)|*.xls";
if (dialog.ShowDialog() == true)
{
textBox_Copy.Text = dialog.FileName;
}
}
/// <summary>
/// 开始提取
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, RoutedEventArgs e)
{
ExcleCore core = new ExcleCore(textBox_Copy.Text.Trim());
lblmsg.Content = "开始读取excel里面的数据";
DataTable dt= core.GetTable(, false);
if (dt != null)
{
List<FileName> excel = new List<FileName>(); int i = ;
foreach (DataRow dr in dt.Rows)
{
FileName model = new FileName();
model.id = ++i;
model.Name = dr[].ToString().ToUpper();
excel.Add(model);
}
dataGrid1.ItemsSource = excel;
if (!Window.GetWindow(dataGrid1).IsVisible)
{
Window.GetWindow(dataGrid1).Show();
}
dataGrid1.UpdateLayout(); lblmsg.Content = "开始读取excel成功,开始迁移文件到RESOURCE文件夹";
string Path = AppDomain.CurrentDomain.BaseDirectory + "\\RESOURCE\\";
foreach (FileName m in excel)
{
var query = from s in list
where s.Name.Contains(m.Name)
select s;
if (query.ToList().Count > )
{
string oldpath = query.ToList()[].Name;
string newpath = Path + m.Name;
File.Copy(oldpath, newpath, true);
DataGridRow row = (DataGridRow)this.dataGrid1.ItemContainerGenerator.ContainerFromIndex(m.id - );
row.Background = new SolidColorBrush(Colors.Blue);
lblmsg.Content = "文件【" + m.Name + "】移动成功";
}
else
{ DataGridRow row = (DataGridRow)this.dataGrid1.ItemContainerGenerator.ContainerFromIndex(m.id - );
row.Background = new SolidColorBrush(Colors.Red);
lost.Add(m);
}
}
lblmsg.Content = "任务执行完成,成功的背景色为蓝色,失败的背景色为红色。";
} } private void button2_Click_1(object sender, RoutedEventArgs e)
{
dataGrid1.ItemsSource = lost;
if (!Window.GetWindow(dataGrid1).IsVisible)
{
Window.GetWindow(dataGrid1).Show();
}
dataGrid1.UpdateLayout();
for (int i = ; i < this.dataGrid1.Items.Count; i++)
{
DataGridRow row = (DataGridRow)this.dataGrid1.ItemContainerGenerator.ContainerFromIndex(i);
row.Background = new SolidColorBrush(Colors.Red);
} }
}
}

WPF的绑定和winfrom的还是有些不一样的。

不过大体上照猫画虎也能搞出来。

WPF做的迁移文件小工具的更多相关文章

  1. 分享一个WPF 实现 Windows 软件快捷小工具

    分享一个WPF 实现 Windows 软件快捷小工具 Windows 软件快捷小工具 作者:WPFDevelopersOrg 原文链接:https://github.com/WPFDevelopers ...

  2. WPF根据Oracle数据库的表,生成CS文件小工具

    开发小工具的原因: 1.我们公司的开发是客户端用C#,服务端用Java,前后台在通讯交互的时候,会用到Oracle数据库的字段,因为服务器端有公司总经理开发的一个根据Oracle数据库的表生成的cla ...

  3. 用WPF做了几个小游戏

    最近看书看累了,参考别人的代码(其实差不多就是把代码重新打了一遍o(╯□╰)o),用wpf做了个<2048>小游戏,顺便在<Git教程>学习下git,也顺便把在<写让别人 ...

  4. 【分享】生成Revit扩展的addin文件小工具

    在进行Revit二次开发的时候,加载命令/程序使用的是添加addin文件的方式,每次都需要手动的写,而且参数有好多,很不方便.于是乎我有了写一个小工具的想法.进过研究终于完成了.主要使用RevitAd ...

  5. 经纬坐标(BLH)数据创建.kml文件小工具设计 Java版

    技术背景 KML,是标记语言(Keyhole Markup Language)的缩写,最初由Keyhole公司开发,是一种基于XML 语法与格式的.用于描述和保存地理信息(如点.线.图像.多边形和模型 ...

  6. C# wpf 实现 MD5加密解密 小工具

    源文件: http://pan.baidu.com/share/link?shareid=2038099474&uk=3912660076 MD5 C# 实现代码来源于网络,感谢原系作者! 参 ...

  7. Python小工具:据说这是搜索文件最快的工具!没有之一!一起感受下......

    电脑自带的搜索文件功能相信大家都体验过,那是真的慢,等它找到文件,我都打完一把游戏了! 那必须不能忍,于是我自己做了一个文件搜索工具,犄角旮旯的文件都能一秒钟搜索出来的那种! 保证能把你们男(女)朋友 ...

  8. 日志文件清理工具V1.1

    上次做完日志文件清理工具V1.0 的版本后,确实给自己的工作带来不少的方便.虽然只是一个小工具,代码也比较简单,但有用就是好东西.上次开发比较匆忙,有些细节没来得及完善,今天吃完晚饭,边看亚冠比赛边把 ...

  9. Windows 上的 Jetty 小工具

    做项目经常遇到需要开发Java应用,我喜欢用Jetty进行开发.部署,主要是由于Jetty的轻量级. Jetty 项目主页:http://www.eclipse.org/jetty/, 最新版9.30 ...

随机推荐

  1. USACO Section 2.1 Sorting a Three-Valued Sequence

    /* ID: lucien23 PROG: sort3 LANG: C++ */ #include <iostream> #include <fstream> #include ...

  2. linux C连接mysql

    linux 环境下C语言连接数据库首先要配置环境 1,确定你的linux下安装mysql我们可以做个测试. 打开你的终端,在终端下输入:service mysqld status [root@bogo ...

  3. MHA自动切换流程

    MHA的全名叫做mysql-master-ha,配置后可以在10-30秒内完成master自动切换,切换过程如下: 1. 检测master的状态,方法是一秒一次“ SELECT 1 As Value” ...

  4. jquery初学者易犯的错误

    1 获取类或者对象的时候,忘记写“#”或者“.” 错误案例: $(document).ready(function(){ $("btn1").click(function(){ a ...

  5. QT-利用C++仿制windown自带的记事本程序V1.0

    下班无事, 发现QT还是很好用的, 就仿制windows的记事本做了一个,未彻底DEBUG, 先拿来分享下. windows记事本大概是这样的: 大概分为以下几步: 1. 界面用QT代码写,即可, Q ...

  6. 在配置WCF服务的时候出现的错误总结

    1.由于扩展配置问题而无法提供您请求的页面.如果该页面是脚本,请添加处理程序.如果应下载文件,请添加 MIME 映射. 我是通过安装图中的FramWork3.5.1搞定的. 网上的其他参考: http ...

  7. 关于twitter的GIF变mp4的测试

    这个事是好久之前听说了,今天FQ的时候突然想起来了,就去测试了一下这个gif转MP4到底有多神奇... 这个是我的twitter地址:https://twitter.com/chendatony31 ...

  8. openstack安装记录(二)keystone安装

    先决条件 在你配置 OpenStack 身份认证服务前,你必须创建一个数据库和管理员令牌. 完成下面的步骤以创建数据库: 用数据库连接客户端以 root 用户连接到数据库服务器: $ mysql -u ...

  9. A+B II

    Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...

  10. autofac使用笔记

    在之前的项目中用来解耦的使用的轻型IOC框架是unity,它的使用也是很方便的提供在之前的文章的也提到过它的使用方式,但是使用久了之后发现了它的不足之处就是需要配置xml文件来对应的接口和实现的关系. ...