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(); //デフォルトに前回の設定を表示 box1.Text = Properties.Settings.Default.box1Setting; checkbox1.IsChecked = Properties.Settings.Default.checkbox1Setting; checkbox2.IsChecked = Properties.Settings.Default.checkbox2Setting; checkbox3.IsChecked = Properties.Settings.Default.checkbox3Setting; //ドラッグ&ドロップイベントの追加 allBox.AddHandler(TextBox.DragOverEvent, new DragEventHandler(textBox_PreviewDragOver), true); allBox.AddHandler(TextBox.DropEvent, new DragEventHandler(textBox_Drop), true); box1.AddHandler(TextBox.DragOverEvent, new DragEventHandler(textBox_PreviewDragOver), true); box1.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(@"\")); } box1.Text = dropFiles[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; } box1.Text = selectFile.FileName; } //作成ボタンClickでチェックボックスで要求された仕様のADocファイル一式を指定先フォルダに生成 private void button2_Click(object sender, RoutedEventArgs e) { //ファイルパスが指定されていない or 指定したフォルダが存在しない場合 if (box1.Text == "" || !Directory.Exists(box1.Text)) { MessageBox.Show("保存先が存在しません", "Folder not selected", MessageBoxButton.OK, MessageBoxImage.Warning); Properties.Settings.Default.box1Setting = ""; //記憶していたパスを初期化 Properties.Settings.Default.Save(); return; } // テキストボックス2からfile名を取得 var fileName = box2.Text; if (box2.Text == "") { fileName = "t"; } //テキストボック1から保存場所を取得 var filePath = box1.Text + @"\"; // ファイル名に使用不可の文字が含まれている場合の処理 char[] NgChars = System.IO.Path.GetInvalidFileNameChars(); char[] cutComma = { '.' }; if (fileName.IndexOfAny(NgChars) >= 0 || fileName.IndexOfAny(cutComma) >= 0) { string[] charsToRemove = new string[] { @"\", ".", "/", ":", "*", "?", "<", ">", "|" }; foreach (var allNg in charsToRemove) { fileName = fileName.Replace(allNg, string.Empty); } MessageBox.Show("ファイル名に使用できない文字が存在したため\n一部ファイル名を修正しました。\nもう一度作成ボタンをクリックしてください。", "Renamed this file", MessageBoxButton.OK, MessageBoxImage.Warning); box2.Text = fileName; return; } //バッチファイルの実行 ProcessStartInfo processStartInfo = new ProcessStartInfo(); string stCurrentDir = System.IO.Directory.GetCurrentDirectory(); processStartInfo.FileName = System.IO.Path.Combine(stCurrentDir, "getADoc.Data", "getADoc.bat"); processStartInfo.CreateNoWindow = true; // コマンドプロンプトを非表示 processStartInfo.UseShellExecute = false; // シェル機能オフ //チェックボックスの状況に応じて引数を指定 processStartInfo.Arguments = ""; if (checkbox2.IsChecked == true)//Iconsチェックボックスがオンのとき { processStartInfo.Arguments += "/Icons "; } if (checkbox1.IsChecked == true)//SCREEN Logoチェックボックスがオンのとき { processStartInfo.Arguments += "/SCREEN "; } if (checkbox3.IsChecked == true)//分割ドキュチェックボックスがオンのとき { processStartInfo.Arguments += "/SepaDoc "; } processStartInfo.Arguments += fileName; //GetADoc.batを実行 Process process = Process.Start(processStartInfo); process.WaitForExit(); process.Close(); //指定されたフォルダに生成物を移動 File.Move(System.IO.Path.Combine(stCurrentDir, fileName + ".adoc"), filePath + fileName + ".adoc"); File.Move(System.IO.Path.Combine(stCurrentDir, "config.adoc"), filePath + "/config.adoc"); Directory.Move(System.IO.Path.Combine(stCurrentDir, "Images"), System.IO.Path.Combine(filePath, "Images")); if (checkbox2.IsChecked == true) { Directory.Move(System.IO.Path.Combine(stCurrentDir, "icons"), System.IO.Path.Combine(filePath, "icons")); } if (checkbox3.IsChecked == true) { File.Move(System.IO.Path.Combine(stCurrentDir, "IncDoc.adoc"), System.IO.Path.Combine(filePath, "IncDoc.adoc")); Directory.Move(System.IO.Path.Combine(stCurrentDir, "Sub_First"), System.IO.Path.Combine(filePath, "Sub_First")); Directory.Move(System.IO.Path.Combine(stCurrentDir, "Sub_Second"), System.IO.Path.Combine(filePath, "Sub_Second")); } //指定されたファイルパスとチェックボックスを保存 Properties.Settings.Default.box1Setting = box1.Text; Properties.Settings.Default.checkbox1Setting = ((bool)checkbox1.IsChecked); Properties.Settings.Default.checkbox2Setting = ((bool)checkbox2.IsChecked); Properties.Settings.Default.checkbox3Setting = ((bool)checkbox3.IsChecked); Properties.Settings.Default.Save(); } } }