【C#】クリップボードの内容でテキストファイルとフォルダ作るプログラム
ブログを書くときに一つのテキストで内容を書いてから完成した後にフォルダとファイル作ってコピペしています。
どうにも面倒なので表題のプログラムを作りました。
コピーした内容の1行目をタイトルにしたファイルとフォルダをデスクトップに作ります。
DLはこちら*1
動作例
以下の文字列をコピーします。
実行するとデスクトップにフォルダとファイルが生成されます。
以下コードです。
参照にPresentationCoreを追加してください。
MainメソッドにSTAThread属性を追加してください。
using System; using System.IO; using System.Windows; using System.Linq; class TxtFileAndFolderMaker { [STAThread] static void Main(string[] args) { if(!Clipboard.ContainsText()) { Console.WriteLine("テキストがありません\r\n何かキーを押してください"); Console.ReadLine(); return; } var str = Clipboard.GetText(); //1行目を取得 var title = str.Replace('\r', '_').Replace('\n', '_').Split('_').First(); //タイトル無効文字列を_に置換 char[] invalidChars = Path.GetInvalidFileNameChars(); foreach (var c in invalidChars) title = title.Replace(c, '_'); //タイトル文字数を100字以内にする if (title.Length > 100) title = title.Substring(0, 100); var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); Directory.CreateDirectory(desktopPath + "\\" + title); string fileName = desktopPath +"\\"+ title + "\\" + title + ".txt"; using (var sw = new StreamWriter(fileName, false)) sw.Write(str); } }