FileSystem(ファイルシステム)


POINT たとえファイルが存在しても ファイルを読み取るのに十分なアクセス許可を持たない場合も、False Directory.Existsメソッドに指定するフォルダ名は パスの最後に"\"があってもなくても、"C:"のようであっても問題なし
string f = @"C:\test.txt"; if (System.IO.File.Exists(f)) { MessageBox.Show( f + "'は存在します。"); } else { MessageBox.Show( f + "'は存在しません"); } // copy System.IO.File.Copy("C:/src.txt", "C:/dst.txt" ); // 上書き許可 System.IO.File.Copy("C:/src.txt", "C:/dst.txt", true );
using System.IO; // ファイルの有無 if ( File.Exists("c:/foo") ) {} if ( Directory.Exists("c:/") ) {} // 作成、消去 Directory.CreateDirectory("d:/test"); Directory.Delete("d:/test"); File.Delete( "d:/test.txt" ); // 移動 File.Move( "d:/src", "d:/foo/src" ); // リネーム File.Move( "d:/src", "d:/dst" ); // MyDocument Enviroment.GetFolderPath( Enviroment.SpecialFolder.Personal ); // 列挙する。 string[] a = Directory.GetDirectories( "d:/" ); string[] a = Directory.GetFiles( "d:/" ); // パターンにマッチしたファイルだけを返す string[] a = Directory.GetFiles( "d:/", "*.txt" ); // パスの内容を解析 ( dirname ) Path.GetRootPath( d:/foo.txt ); Path.GetDirectoryName( d:/foo.txt ); // foo.txt Path.GetFileName( d:/foo.txt ); // .txt ( .含む ) Path.GetExtension( d:/foo.txt ); // foo Path.GetFileNameWithoutExtension( d:/foo.txt );



dirname


Path.GetDirectoryName( d:/foo.txt );
// ファイルサイズ ( byte 数 ) FileInfo f = new FileInfo( path ); long n = f.Length;



ファイル生成の監視


FileSystemWatcher fs = new FileSystemWatcher(); // 監視するディレクトリ fs.Path = "d:/"; // 監視するファイル string file = "*.txt"; fs.Filter = file; fs.NotifyFilter = NotifyFilters.FileName; string[] data = new string[] { "d:/", file }; // 3秒後にスレッドにファイルを作らせる。 if (ThreadPool.QueueUserWorkItem(new WaitCallback(func), data)) { Console.WriteLine("wait"); // 10 秒間だけ待つ WaitForChangedResult ret = fs.WaitForChanged(WatcherChangeTypes.Created, 10 * 1000); Console.WriteLine( "create file {0}", ret.Name ); } Console.WriteLine("finish"); } static void func( object obj ) { string[] data = (string[])obj; Console.WriteLine("file をつくります"); Thread.Sleep(3000); string path = data[0]; string file = data[1]; path += file; FileStream f = File.Create(path); }