using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; 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; using Microsoft.Win32; using MSAPI = Microsoft.WindowsAPICodePack; namespace GetADoc { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); //デフォルトに前回の設定を表示 FolderPathBox.Text = Properties.Settings.Default.box1Setting; ScreenCheckbox.IsChecked = Properties.Settings.Default.checkbox1Setting; IconCheckbox.IsChecked = Properties.Settings.Default.checkbox2Setting; SepaDocCheckbox.IsChecked = Properties.Settings.Default.checkbox3Setting; //ドラッグ&ドロップイベントの追加 allBox.AddHandler(TextBox.DragOverEvent, new DragEventHandler(textBox_PreviewDragOver), true); allBox.AddHandler(TextBox.DropEvent, new DragEventHandler(textBox_Drop), true); FolderPathBox.AddHandler(TextBox.DragOverEvent, new DragEventHandler(textBox_PreviewDragOver), true); FolderPathBox.AddHandler(TextBox.DropEvent, new DragEventHandler(textBox_Drop), true); } //フォルダ指定のためにファイルをドラッグ private void textBox_PreviewDragOver(object sender, System.Windows.DragEventArgs e) { if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop, true)) { e.Effects = System.Windows.DragDropEffects.Copy; } else { e.Effects = System.Windows.DragDropEffects.None; } e.Handled = true; } //エクスプローラーからテキストボックスにドロップして指定フォルダの場所をペースト private void textBox_Drop(object sender, System.Windows.DragEventArgs e) { var dropFiles = e.Data.GetData(System.Windows.DataFormats.FileDrop) as string[]; if (dropFiles == null) return; //ドロップしたものがファイルの場合、1つ上の階層のフォルダを指す if (File.Exists(dropFiles[0])) { dropFiles[0] = dropFiles[0].Substring(0, dropFiles[0].LastIndexOf(@"\")); } FolderPathBox.Text = dropFiles[0]; FolderPathBox.Focus(); FolderPathBox.Select(this.FolderPathBox.Text.Length, 0); } //参照ボタンClickでフォルダ選択のダイアログを表示 private void button1_Click(object sender, RoutedEventArgs e) { var selectFile = new MSAPI::Dialogs.CommonOpenFileDialog(); selectFile.IsFolderPicker = true; selectFile.Title = "フォルダを選択してください"; selectFile.InitialDirectory = @"C:"; if (selectFile.ShowDialog() != MSAPI::Dialogs.CommonFileDialogResult.Ok) { return; } FolderPathBox.Text = selectFile.FileName; } //作成ボタンClickでチェックボックスで要求された仕様のADocファイル一式を指定先フォルダに生成 private void button2_Click(object sender, RoutedEventArgs e) { bool IconFlag = false; bool SCREENFlag = false; bool SepaFlag = false; if (IconCheckbox.IsChecked == true)//Iconsチェックボックスがオンのとき { IconFlag = true; } if (ScreenCheckbox.IsChecked == true)//SCREEN Logoチェックボックスがオンのとき { SCREENFlag = true; } if (SepaDocCheckbox.IsChecked == true)//分割ドキュチェックボックスがオンのとき { SepaFlag = true; } //ファイルパスが指定されていない or 指定したフォルダが存在しない場合 if (FolderPathBox.Text == "" || !Directory.Exists(FolderPathBox.Text)) { CustomMsgBox.Show(mainWindow, "保存先が存在しません", "Save destination does not exist", MessageBoxButton.OK, MessageBoxImage.Warning); Properties.Settings.Default.box1Setting = ""; //記憶していたパスを初期化 Properties.Settings.Default.Save(); return; } // テキストボックス2からfile名を取得 var fileName = ADocNameBox.Text; if (ADocNameBox.Text == "") { fileName = "t"; } //テキストボック1から保存場所を取得 var filePath = FolderPathBox.Text + @"\"; // ファイル名に使用不可の文字が含まれている場合の処理 char[] NgChars = System.IO.Path.GetInvalidFileNameChars(); char[] cutComma = { '.' }; if (fileName.IndexOfAny(NgChars) >= 0 || fileName.IndexOfAny(cutComma) >= 0) { fileName = RunBatch.NameCalibration(fileName); CustomMsgBox.Show(mainWindow, "ファイル名に使用できない文字が存在したため\n一部ファイル名を修正しました。" + "\nもう一度作成ボタンをクリックしてください。", "Renamed this file", MessageBoxButton.OK, MessageBoxImage.Warning); ADocNameBox.Text = fileName; return; } //指定したフォルダに同名の.adocや既存の付属ファイル(config.adocなど)がある場合の処理 if (File.Exists(System.IO.Path.Combine(filePath, fileName + ".adoc")) || File.Exists(System.IO.Path.Combine(filePath, "config.adoc")) || Directory.Exists(System.IO.Path.Combine(filePath, "Images")) || Directory.Exists(System.IO.Path.Combine(filePath, "icons")) || Directory.Exists(System.IO.Path.Combine(filePath, "Sub_First")) || Directory.Exists(System.IO.Path.Combine(filePath, "Sub_Second"))) { MessageBoxResult OverwriteADoc = CustomMsgBox.Show(mainWindow, "これから生成するファイルと同名のファイルが既に存在します" + "\n既存のファイルを上書きしますか?", "指定したフォルダに既存のファイルが存在します", MessageBoxButton.YesNo, MessageBoxImage.None); if (OverwriteADoc == MessageBoxResult.Yes) { } else if (OverwriteADoc == MessageBoxResult.No) { CustomMsgBox.Show(mainWindow, "別のフォルダを指定してください", "Please select another directory", MessageBoxButton.OK, MessageBoxImage.None); return; } } //バッチファイルの実行 RunBatch.runBatch(IconFlag, SCREENFlag, SepaFlag, fileName, filePath); //処理の完了を知らせるラベルを表示 CompletedLabel.Visibility = Visibility.Visible; //指定されたファイルパスとチェックボックスを保存 Properties.Settings.Default.box1Setting = FolderPathBox.Text; Properties.Settings.Default.checkbox1Setting = ((bool)ScreenCheckbox.IsChecked); Properties.Settings.Default.checkbox2Setting = ((bool)IconCheckbox.IsChecked); Properties.Settings.Default.checkbox3Setting = ((bool)SepaDocCheckbox.IsChecked); Properties.Settings.Default.Save(); } //コントロール(テキストボックスなど)がフォーカスされたら処理完了のラベルを非表示にする private void DeleteCompLabel(object sender, RoutedEventArgs e) { CompletedLabel.Visibility = Visibility.Hidden; } } }