客户这边需要往服务器上传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 = -1; 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(0, false); if (dt != null) { List<FileName> excel = new List<FileName>(); int i = 0; foreach (DataRow dr in dt.Rows) { FileName model = new FileName(); model.id = ++i; model.Name = dr[0].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 > 0) { string oldpath = query.ToList()[0].Name; string newpath = Path + m.Name; File.Copy(oldpath, newpath, true); DataGridRow row = (DataGridRow)this.dataGrid1.ItemContainerGenerator.ContainerFromIndex(m.id - 1); row.Background = new SolidColorBrush(Colors.Blue); lblmsg.Content = "文件【" + m.Name + "】移动成功"; } else { DataGridRow row = (DataGridRow)this.dataGrid1.ItemContainerGenerator.ContainerFromIndex(m.id - 1); 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 = 0; i < this.dataGrid1.Items.Count; i++) { DataGridRow row = (DataGridRow)this.dataGrid1.ItemContainerGenerator.ContainerFromIndex(i); row.Background = new SolidColorBrush(Colors.Red); } } } }
WPF的绑定和winfrom的还是有些不一样的。
不过大体上照猫画虎也能搞出来。