客户这边需要往服务器上传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. FreeMarker的模板文件语法

    FreeMarker的模板文件并不比HTML页面复杂多少,FreeMarker模板文件主要由如下4个部分组成: 1,文本:直接输出的部分 2,注释:<#-- ... -->格式部分,不会输 ...

  2. Oracle学习笔记(2)——过程和函数

    过程和函数统称为PL/SQL子程序,通过输入.输出参数或输入/输出参数与其调用者交换信息.他们是被命名的PL/SQL块,被编译后存储在数据库中,以备执行.因此,可以在数据库中直接按名称使用它们. 1. ...

  3. diffuse linux 文件比对工具

    linux下比较好用的可视化文件比对工具

  4. 四、Mp3文件类型及其判断

    根据前两篇文章的分析,帧分为标签帧和数据帧,MP3文件类型是根据数据帧的类型来分的,文件类型如下表: 位率相等(Constant BitRate) CBR  Mp3文件 位率不等(Variable B ...

  5. Android开发中一些常见的问题解决方案

    分享一下自己开发中遇到的一些常见问题及解决方案,方面以后快速开发少走弯路,也可以供大家一起学习. 1.开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listvie ...

  6. Java编程的23种设计模式

    设计模式(Design Patterns)                                   --可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用. ...

  7. css基础之 图片瀑布流布局:用CSS+DIV等宽格子堆砌瀑布流效果 (一)

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

  8. 从汇编看c++中成员函数指针(一)

    下面先来看c++的源码: #include <cstdio> using namespace std; class X { public: int get1() { ; } virtual ...

  9. jquery 实现简单拖拽

    $.fn.drag = function(obj) { var dragging = false; var oDrag = $(obj); oDrag.mousedown(function(e) { ...

  10. VSFTP服务

    互联网最开始的三大服务:HTTP.mail.FTP 一.文件服务器简介     FTP:在内网和公网使用.服务器:windows,Linux    客户端:windows,Linux     samb ...