Friday, April 3, 2009

Files and Directories

To get information about a directory, first you need to create a DirectoryInfo object:
string strPath = @"C:\Temp\Test1"
DirectoryInfo dInfo = new DirectoryInfo(strPath);

dInfo.Name gets the directory name (Test1)
dInfo.Parent gets the parent directory name (Temp)
dInfo.FullName gets the full path (C:\Temp\Test1)


Create a directory if it does not exist:

if (!dInfo.Exists)
dInfo.Create();


To get an array of subdirectories use:
DirectoryInfo[] subDirInfo = dInfo.GetDirectories();

Get File Size:

FileInfo fileInfo = new FileInfo(strPath);
Console.WriteLine(fileInfo.Length);


Get a directory name from a file name:

FileInfo fileInfo = new FileInfo("C:\\MyFolder\\MyFile.exe");
Console.WriteLine(fileInfo.DirectoryName);

To change file timestamp after FileStream write use the following methods

File.SetCreationTime(filepath, DateTime);
File.SetLastWriteTime(filepath, DateTime);


No comments:

Post a Comment