2011年3月8日火曜日

C# ファイルのパスを調べる関係

if (File.Exists(filePath)) {
// filePathのファイルは存在する
} else {
// filePathのファイルは存在しない
}

if (Directory.Exists(dirPath)) {
// dirPathのディレクトリは存在する
} else {
// dirPathのディレクトリは存在しない
}

//ファイルのサイズを取得
System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\test.txt");
long filesize = fi.Length;



// パスを定数で定義する
const string FILE_PATH = @"C:\Hoge.txt";

// 作成日時を取得する
DateTime dtCreate = System.IO.File.GetCreationTime(FILE_PATH);

// 更新日時を取得する
DateTime dtUpdate = System.IO.File.GetLastWriteTime(FILE_PATH);

// アクセス日時を取得する
DateTime dtAccess = System.IO.File.GetLastAccessTime(FILE_PATH);

// 取得したタイムスタンプを表示する
MessageBox.Show("作成日時 : " + dtCreate.ToString());
MessageBox.Show("更新日時 : " + dtUpdate.ToString());
MessageBox.Show("アクセス日時 : " + dtAccess.ToString());

0 件のコメント: