[VB.NET] ファイル操作について

.NET Framework 2.0以降は、System.IO だけでなく、My.Computer からもファイル操作ができるらしい。

これ、どっちを使った方がいい、というものはなく、どちらも全く同じ動作をするんだとか。特に違いがなくどちらでもいいなら、My.Computer.FileSystem の方が、全てまとまっていて使いやすいかな~。

備忘録としてメモメモ。


■ファイル存在チェック
System.IO.File.Exists(FilePath)
My.Computer.FileSystem.FileExists(FilePath)

■ディレクトリ(フォルダ)存在チェック
System.IO.Directory.Exists(DirPath)
My.Computer.FileSystem.DirectoryExists(DirPath)

■ディレクトリ(フォルダ)作成
System.IO.Directory.CreateDirectory(DirPath)
My.Computer.FileSystem.CreateDirectory(DirPath)

■ファイル削除
System.IO.File.Delete(FilePath)
My.Computer.FileSystem.DeleteFile(FilePath)

■ディレクトリ(フォルダ)削除
System.IO.Directory.Delete(DirPath)
My.Computer.FileSystem.DeleteDirectory(DirPath, FileIO.DeleteDirectoryOption)

■ディレクトリ(フォルダ)内の全ファイル取得
System.IO.Directory.GetFiles(DirPath)
My.Computer.FileSystem.GetFiles(DirPath)

■ディレクトリ(フォルダ)内の全サブディレクトリ(サブフォルダ)取得
System.IO.Directory.GetDirectories(DirPath)
My.Computer.FileSystem.GetDirectories(DirPath)


おまけ

■ファイルの作成日
System.IO.File.GetCreationTime(FilePath)
Dim Info As FileInfo = My.Computer.FileSystem.GetFileInfo(FilePath) : Dim date As Date = Info.CreationTime

■ファイルの更新日
System.IO.File.GetLastWriteTime(FilePath)
Dim Info As FileInfo = My.Computer.FileSystem.GetFileInfo(FilePath) : Dim date As Date = Info.LastWriteTime

ファイル操作を単発で行うなら System.IO の方が短くて済むけど、そうでなければ My.Computer の方が便利かも。

コメント

タイトルとURLをコピーしました