Skip to content

Instantly share code, notes, and snippets.

@FoggyFinder
Created November 12, 2019 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FoggyFinder/4747e1435b68174f541ed8495ed8f3ef to your computer and use it in GitHub Desktop.
Save FoggyFinder/4747e1435b68174f541ed8495ed8f3ef to your computer and use it in GitHub Desktop.
<Window
x:Class="WpfApp3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:WpfApp3.ViewModels"
Title="File comparer"
Width="800"
Height="450">
<Window.DataContext>
<vm:ApplicationViewModel />
</Window.DataContext>
<Grid>
<Button
x:Name="button"
Width="57"
Margin="10,87,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding BrowseFolder}"
CommandParameter="0"
Content="Folder 1" />
<Button
x:Name="button_Copy"
Width="66"
Height="19"
Margin="683,87,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Command="{Binding BrowseFolder}"
CommandParameter="1"
Content="Folder 2" />
<TextBox
x:Name="TextBox"
Width="192"
Height="18"
Margin="72,87,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
SelectionOpacity="1"
SpellCheck.IsEnabled="True"
Text="{Binding TextBox1}"
TextWrapping="Wrap" />
<TextBox
x:Name="textBox_Copy"
Width="168"
Height="17"
Margin="498,88,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="{Binding TextBox2}"
TextWrapping="Wrap" />
<TextBlock
x:Name="textBlock_Copy1"
Width="254"
Height="248"
Margin="10,142,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="Beige"
Text="{Binding TxtBlock}"
TextWrapping="Wrap" />
<Button
x:Name="button1"
Width="75"
Margin="436,35,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="Compare" />
<TextBlock
x:Name="textBlock_Copy"
Width="251"
Height="236"
Margin="498,124,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Background="Beige"
Text="{Binding TxtBlock}"
TextWrapping="Wrap" />
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Input;
using WpfApp3.Model;
using Winforms = System.Windows.Forms;
namespace WpfApp3.ViewModels
{
public class ApplicationViewModel : INotifyPropertyChanged
{
private UserFile _selectedFile;
public UserFile SelectedFile
{
get => _selectedFile;
set
{
_selectedFile = value;
OnPropertyChanged("SelectedFolder");
}
}
private string textBox1;
public string TextBox1
{
get => textBox1;
set
{
textBox1 = value;
OnPropertyChanged(nameof(TextBox1));
}
}
private string textBox2;
public string TextBox2
{
get => textBox2;
set
{
textBox2 = value;
OnPropertyChanged(nameof(TextBox2));
}
}
private string txtBlock;
public string TxtBlock
{
get => txtBlock;
set
{
txtBlock = value;
OnPropertyChanged(nameof(TxtBlock));
}
}
public ICommand Compare => new RelayCommand((obj) =>
{
try
{
IEnumerable<string> oldFiles = Directory.EnumerateFiles("TextBox1").Select(Path.GetFileName);
IEnumerable<string> newFiles = Directory.EnumerateFiles("TextBox2").Select(Path.GetFileName);
newFiles = newFiles.Except(oldFiles);
}
catch (IOException e)
{
MessageBox.Show(e.ToString());
}
});
public ApplicationViewModel()
{
BrowseFolder = new RelayCommand(Browse);
}
public void Browse(object obj)
{
if (obj is string ival)
{
Winforms.FolderBrowserDialog fbd = new Winforms.FolderBrowserDialog
{
RootFolder = Environment.SpecialFolder.DesktopDirectory,
Description = "Select Folder",
ShowNewFolderButton = false
};
if (fbd.ShowDialog() == Winforms.DialogResult.OK)
{
if (ival == "0")
{
TextBox1 = fbd.SelectedPath;
}
else if (ival == "1")
{
TextBox2 = fbd.SelectedPath;
}
var files =
Directory.GetFiles(fbd.SelectedPath)
.Select(file => Path.GetFileName(file));
TxtBlock = string.Join("\n", files);
}
}
}
public ICommand BrowseFolder { get; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string prop = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment