Advertisement
Methods | Description |
---|---|
DirectoryInfo CreateDirectory(String path) | This method creates all directories and sub-directories in the specified path, only if they don't already exist. |
void Move(String sourceDirectoryName, String destDirectoryName) | This method moves a specified directory to a new location, providing the option to specify a new directory name at the destination. |
void Delete(String path) | This method deletes the directory specified by the path. |
String[] GetFiles(String path) | This method returns the full names of the files in the directory specified by path. |
String[] GetFiles(String path, String pattern) | This method returns the full names of the files with the specified specific pattern in the directory specified by path. |
String[] GetDirectories(String path) | This method returns the names of sub-directories in the specified directory.. |
String GetCurrentDirectory(String path) | This method returns the name of the current working directory. |
bool Exists(String path) | This method determines whether the specified directory exists or not. |
DateTimeGetCreationTime(String path) | This method returns the creation time of the specified directory or directory. |
void SetCreationTime(String path, DateTime time) | This method sets the creation time the specified directory or directory. |
DateTime GetLastAccessTime(String path) | This method returns the last access time of the specified directory or directory. |
void SetLastAccessTime(String path, DateTime time) | This method sets the last access time of the specified directory or directory was written to. |
DateTime GetLastWriteTime(String path) | This method returns the last access time of the specified directory or directory was written to. |
void SetLastWriteTime(String path, DateTime time) | This method sets the last time the specified directory or directory was written to. |
//C# Using the Directory class to create a directory
using System;
using System.IO;
using System.Text;
class A
{
public static void Main(String[] ar)
{
try
{
//Calling the CreateDirectory(String path) method of Directory
//Which creates a new directory at the specified path
//and returns a DirectoryInfo object to peform directory related operations.
DirectoryInfo fs = Directory.CreateDirectory("D:\\My new folder");
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Advertisement
//C# Calling the GetFiles() method of Directory class
//Which returns a String array of the full names for the files in the directory specified by path
using System;
using System.IO;
using System.Collections.Generic;
class A
{
public static void Main(String[] ar)
{
try
{
//Creating a String
String directory = "D:\\";
Console.WriteLine("List of files in the specified " + directory);
//Calling the GetFiles() method of Directory class
String[] files = Directory.GetFiles(directory);
foreach(String fileName in files)
Console.WriteLine(fileName);
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
List of files in the specified D:\
D:\Book1.pdf
D:\Book2.pdf
D:\Mypage.htm
D:\c250.cs
D:\File2.txt
D:\File3.txt
D:\MyFile1.txt
D:\MyFile10.txt
D:\MyFile11.txt
Wildcards characters | Description |
---|---|
* | Zero or more characters in that position. |
? | Zero or one character in that position. |
//C# Calling a different version of GetFiles() method of Directory class
//To get all file names ending in the specified directory with a specific String pattern
using System;
using System.IO;
using System.Collections.Generic;
class A
{
public static void Main(String[] ar)
{
try
{
//Creating a String
String directory = "D:\\";
Console.WriteLine("List of files ending with .txt pattern in " + directory);
//Calling the GetFiles() method of Directory class
//To get all file names ending with ".txt" pattern
//And using wildcard character *, for any number of characters before ".txt"
String[] files = Directory.GetFiles(directory, "*.txt");
//Using foreach loop to enumerate the matching file names
foreach(String fileName in files)
Console.WriteLine(fileName);
//Calling the GetFiles() method of Directory class
//To get all file names starting with "M" pattern
//And using wildcard character *, for any number of characters before "M*"
files = Directory.GetFiles(directory, "M*");
Console.WriteLine("List of files starting with M pattern in " + directory);
//Using foreach loop to enumerate the matching file names
foreach(String fileName in files)
Console.WriteLine(fileName);
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
List of files ending with .txt pattern in D:\
D:\File2.txt
D:\File3.txt
D:\MyFile1.txt
D:\MyFile10.txt
D:\MyFile11.txt
List of files starting with M pattern in D:\
D:\MyFile1.txt
D:\MyFile10.txt
D:\MyFile11.txt
Haha.txt
heheheh.txt
//C# Using the Directory to move a directory to another location(in the same volume)
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//Creating two String objects
String sourceDirectory = "D:\\My new folder";
String destinationDirectory = "D:\\My new folder2";
Console.WriteLine("Files in the " + sourceDirectory);
//Calling the GetFiles() method of Directory class
//To get files in the source directory(before calling the Move() method)
String[] files = Directory.GetFiles(sourceDirectory);
foreach(String fileName in files)
Console.WriteLine(fileName);
Console.WriteLine("Moving the " + sourceDirectory + " to " + destinationDirectory);
//Calling the Move() method of Directory
//which moves a directory present in D: Drive to E: Drive and with a new name
Directory.Move(sourceDirectory, destinationDirectory);
Console.WriteLine("List of files in the newly created " + destinationDirectory);
//Calling the GetFiles() method of Directory class
//To get files in the newly created directory(which was created by calling the Move() method)
files = Directory.GetFiles(destinationDirectory);
foreach(String fileName in files)
Console.WriteLine(fileName);
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Files in the D:\My new folder
D:\My new folder\Haha.txt
D:\My new folder\heheheh.txt
Moving the D:\My new folder to D:\My new folder2
List of files in the newly created D:\My new folder2
D:\My new folder2\Haha.txt
D:\My new folder2\heheheh.txt
Advertisement
//C# Using the Directory to find get and set the directory information
//such as its last access time, creating time and last write time.
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//Creating a String
String directory = "D:\\My new folder2";
Console.WriteLine("Getting the directory specific information about directory " + directory);
//Getting the creating time of the specified directory
DateTime d = Directory.GetCreationTime(directory);
Console.WriteLine("Creation time of the directory: " + d.ToString());
//Getting the last write time of the specified directory
d = Directory.GetLastWriteTime(directory);
Console.WriteLine("Last write time of the directory: " + d.ToString());
//Getting the last access time of the specified directory
d = Directory.GetLastAccessTime(directory);
Console.WriteLine("Last access time of directory: " + d.ToString());
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Getting the directory specific information about directory D:\My new folder2
Creation time of the directory: 17-01-2020 15:47:19
Last write time of the directory: 17-01-2020 16:55:22
Last access time of directory: 17-01-2020 17:08:41
//C# Calling the Delete() method of Directory class to delete an existing directory
using System;
using System.IO;
class A
{
public static void Main(String[] ar)
{
try
{
//Creating a String
String directory = "D:\\My new folder";
//Calling the GetFiles() method of Directory class
String[] files = Directory.GetFiles(directory);
Console.WriteLine("Contents of the directory " + directory);
foreach(String fileName in files)
Console.WriteLine(fileName);
Console.WriteLine("Deleting the Contents of the directory to empty it " + directory);
foreach(String fileName in files)
File.Delete(fileName);
Console.WriteLine("Deleting the directory " + directory);
//Calling the Delete() method of Directory class to delete an existing directory
Directory.Delete(directory);
}
catch(IOException e)
{
Console.WriteLine(e);
}
}
}
Contents of the directory D:\My new folder
D:\My new folder\Haha.txt
D:\My new folder\Heheheh.txt
Deleting the Contents of the directory to empty it D:\My new folder
Deleting the directory D:\My new folder
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement