C# - Windows File System:
C# allows you to work with directories and files using various directory and file related classes like , the DirectoryInfo class and the FileInfo class.
The DirectoryInfo Class:
The DirectoryInfo class is derived from the FileSystemInfo class. it have various method for creating , moving , and browsing , through directories and subdirectories. this class can not be inherited.
some commonly used properties of the DirectoryInfo class:
Attributes >> Gets the attributes for the current file.
CreationTime >> Gets the creation time of current file.
Exists >> gets a Boolean value indicating whether the file exists.
Extension >> Gets the string representing the file extension.
FullName >> Gets the full path of the file.
LastAccessTime >> Gets the time the current file was last accessed.
Name >> Gets the name of this file instance.
Directory >> Gets an instance of an directory which the file belongs.
LastWriteTime >> gets the time of the last written activity of the file.
length >> Gets the size , in bytes , of the current files.
Name >> Gets the name of the file.
C# allows you to work with directories and files using various directory and file related classes like , the DirectoryInfo class and the FileInfo class.
The DirectoryInfo Class:
The DirectoryInfo class is derived from the FileSystemInfo class. it have various method for creating , moving , and browsing , through directories and subdirectories. this class can not be inherited.
some commonly used properties of the DirectoryInfo class:
- Attributes >> Gets the attributes for the current file or directory.
- CreationTime >> Gets the creation time of current file or directory.
- Exists >> gets a Boolean value indicating whether the directory exists.
- Extension >> Gets the string representing the file extension.
- FullName >> Gets the full path of the directory or file.
- LastAccessTime >> Gets the time the current file or directory was last accessed.
- Name >> Gets the name of this DirectoryInfo instance.
Some commonly used methods of the DirectoryInfo class:
- Create() >> Creates a directory.
- CreateSubDirectory(string) >> Create a subdirectory and subdirectories on the specified path can be relative to this instance of the directory class.
- Delete() >> Delete directory info if its empty.
- GetDirectories() >> return the subdirectories of the current directories.
- GetFiles() >> return a file list from the current directory.
The FIleInfo Class:
The FileInfo Class is derived from the FileSystemInfo class. it has properties and instance method for creating , copying , moving , and opening of files , and helps in the creation of FileStream object. this class cannot be inherit.
some commonly used properties of the FileInfo class:
some commonly used method of FileInfo class:
- AppendText() >> Create a StreamWriter that appends text to the file represented by this instance of the FileInfo.
- Create() >> create a file.
- Delete() >> deletes a file permanently.
- MoveTo(string destFilename) >> moves a specific file to the new location , providing the option to specify a new file name.
- Open(FileMode mode, FileAccess access, FileShare share) >> opens a file in specified mode with read , write or read/write access and the specified sharing option.
- OpenRead() >> creates a read only FileStream.
- OpenWrite() >> creates a write only FileStream.
the following example demonstrate the use of DirectoryInfo class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace directoryinfo_class.cs
{
class Program
{
static void Main(string[] args)
{
DirectoryInfo mydirinfo = new DirectoryInfo("C:");
Console.WriteLine("full name of the directory is :"+ mydirinfo.FullName);
Console.WriteLine("the directory was last accessed on :"+ mydirinfo.LastAccessTime.ToString());
Console.ReadKey();
}
}
}
output:
the following example demonstrate the use of FileInfo class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace fileinfo_classes.cs
{
class Program
{
static void Main(string[] args)
{
// creating an instance of directoryinfo class
DirectoryInfo mydirinfo = new DirectoryInfo("C:");
// set all the files in the directory and print their name and size
FileInfo[] fileindir = mydirinfo.GetFiles();
foreach (FileInfo file in fileindir)
{
Console.WriteLine("file name : " + file.Name + " extension :" + file.Extension + " size : " + file.Length + "bytes");
}
Console.ReadKey();
}
}
}
output :
reference link :
No comments:
Post a Comment