Lab 2
Lab 2
| Lab 2
Lab#2: Dealing with Directories & Files
#Directory
• When using a computer, you always need to create a directory so that you can organize your files
and documents. For example, you create folders called movie, music, document, etc. to store the
corresponding data. This is convenient for you to find what you need quickly.
Step 1: Navigate to the place where you want to create a directory, for example, D drive.
Step 2: Right-click the blank space and choose New > Folder. Type a name for the new folder.
Step 1: Also, go to the place where you need to create a folder, for example, File Explorer or
desktop.
Step 2: Press the keys on your keyboard at the same time: Ctrl + Shift + N. Windows will create
a folder named New Folder immediately. You can change the name to what you want.
Step 1: Go to the search box, type cmd, and right-click Command Prompt to choose Run as
administrator.
Step 2: In the CMD window, go to the drive where you want to create a folder by typing the drive
letter followed by a colon and pressing Enter, for example, D:.
Step 3: Type mkdir followed by the name of the folder you want to create and press Enter, for
example, mkdir mynewfolder.
Page | 1
4thLevel | Operating System. | Lab 2
namespace createDirectory
{
class Program
{
static void Main(string[] args)
{
string dir = @"D:\Temp";
string subdir = @"D:\Temp\subTemp";
// If directory does not exist, create it.
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
// Create sub directory
if (!Directory.Exists(subdir))
{
Directory.CreateDirectory(subdir);
}
}
}
}
Select directory, Press the keys on your keyboard at the same time: Ctrl+Delete.
Page | 2
4thLevel | Operating System. | Lab 2
namespace removeDirectory
{
class Program
{
static void Main(string[] args)
{
string dir = @"D:\Temp";
// If directory does not exist, don't even try
if (Directory.Exists(dir))
{
Directory.Delete(dir);
}
}
}
}
Right-Click on directory, Cut → Go to the wanted destination → press Right-Click in any space → Paste.
Page | 3
4thLevel | Operating System. | Lab 2
namespace moveDirectory
{
class Program
{
static void Main(string[] args)
{
string sourceDirName = @"C:\Temp";
string destDirName = @"C:\NewTemp";
try
{
Directory.Move(sourceDirName, destDirName);
}
catch (IOException exp)
{
Console.WriteLine(exp.Message);
}
}
}
}
#Files
C# Code TO COPY FILE:
using System;
using System.IO;
namespace CopyFile
{
class Program
{
static void Main(string[] args)
{
string fileName = "test.txt";
string sourcePath = @"D:\TestFolder";
string targetPath = @"D:\TestFolder\SubDir";
Page | 4
4thLevel | Operating System. | Lab 2
// To copy a file to another location and
// overwrite the destination file if it already exists.
File.Copy(sourceFile, destFile, true);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
// Use static Path methods to extract only the file name from the path.
fileName = Path.GetFileName(s);
destFile = Path.Combine(targetPath, fileName);
File.Copy(s, destFile, true);
}
}
else
{
Console.WriteLine("Source path does not exist!");
}
namespace MoveFile
{
class Program
{
static void Main(string[] args)
{
string sourceFile = @"D:\Test\test.txt";
string destinationFile = @"D:\Test\private\test.txt";
Page | 5
4thLevel | Operating System. | Lab 2
}
namespace MoveFile
{
class Program
{
static void Main(string[] args)
{
String line;
try
{
//Pass the file path and file name to the StreamReader constructor
StreamReader sr = new StreamReader("C:\\Sample.txt");
//Read the first line of text
line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
Console.ReadLine();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
}
}
Page | 6
4thLevel | Operating System. | Lab 2
namespace WriteFile
{
class Program
{
static void Main(string[] args)
{
try
{
//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C:\\Test.txt");
//Write a line of text
sw.WriteLine("Hello World!!");
//Write a second line of text
sw.WriteLine("From the StreamWriter class");
//Close the file
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
}
}
}
#Simple Task:
How To Create, Move, Copy and Rename File Using CMD.
********************
Best Wishes.
Page | 7