/// <summary> /// /// </summary> /// <param name="filePath"></param> public static void SaveToSample(string filePath) { var config = new FileMoveConfig(); config.infomations = new FileMoveInfomation[2]; config.infomations[0] = new FileMoveInfomation(); config.infomations[0].directoryPath = "sample\\directory path"; config.infomations[0].keywords = new string[] { "keyword1", "keyword2" }; config.infomations[1] = new FileMoveInfomation(); config.infomations[1].directoryPath = "sample\\ディレクトリ"; config.infomations[1].keywords = new string[] { "キーワード1", "キーワード2" }; config.SaveToJson(filePath); }
/// <summary> /// /// </summary> public static FileMoveConfig LoadFromJson(string filePath) { var result = new FileMoveConfig(); using (var stremReader = new StreamReader(filePath)) { var json = stremReader.ReadToEnd(); using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json))) { var serializer = new DataContractJsonSerializer(typeof(FileMoveConfig)); return((FileMoveConfig)serializer.ReadObject(stream)); } } }
/// <summary> /// 実行 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void RunToolStripMenuItem_Click(object sender, EventArgs e) { try { // 設定読み込み var config = FileMoveConfig.LoadFromJson("./FileMoveManager.json"); // ディレクトリ指定 var directory = Directory.GetCurrentDirectory(); // ファイル列挙 string[] files = Directory.GetFiles(directory, "*"); foreach (string file in files) { // ファイル名 var name = Path.GetFileName(file); foreach (var info in config.infomations) { foreach (var keyword in info.keywords) { // ファイル検索 if (name.Contains(keyword)) { // ディレクトリ作成 SafeCreateDirectory(info.directoryPath); // ファイル移動 var dest = info.directoryPath + "\\" + name; File.Move(file, dest); break; } } } Debug.WriteLine(name); } } catch (Exception exception) { MessageBox.Show(exception.ToString(), "Exception"); } MessageBox.Show("Finish"); }
/// <summary> /// サンプルJSON生成 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void SampleToolStripMenuItem_Click(object sender, EventArgs e) { FileMoveConfig.SaveToSample("sample.json"); }