|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThis article is focused on understanding the Directory class of Let take a small example to get all the available drive information (this includes both physical & logical drives). Using C routines#include <stdio.h> #include <conio.h> #include <direct.h> #include <stdlib.h> #include <ctype.h> void main( void ){ int drive, curdrive; static char path[_MAX_PATH]; /* Save current drive. */ curdrive = _getdrive(); printf( "Using C API, Available drives are: \n"); /* If we can switch to the drive, it exists. */ for( drive = 1; drive <= 26; drive++ ) if( !_chdrive( drive ) ) printf( "%c: \n", drive + 'A' - 1 ); } Output:
Using C API, Available drives are:
A:\
C:\
D:\
E:\
Z:\
Using Platform SDK File I/O routines#include <Windows.h> #include <stdio.h> void main( void ) { char *lpBuffer=new char[1000]; long retSize; retSize=GetLogicalDriveStrings(1000,lpBuffer); printf("Using SDK API, Available drives are:"); for(int i=0;i<=retSize;i++) { if(lpBuffer[i]!='0') printf("%c",lpBuffer[i]); else printf(""); } } Output:
Using SDK API, Available drives are:
A:\
C:\
D:\
E:\
Z:\
The same program we will do in C# getDr.csusing System;
using System.IO;
class getDr
{
public static void Main()
{
try
{
string[] str=Directory.GetLogicalDrives();
Console.WriteLine( "Using C# Directory Class,Available drives are:");
for(int i=0;i< str.Length;i++)
Console.WriteLine(str[i]);
}
catch(IOException e) { Console.WriteLine(e.ToString()); }
} // end of Main
}//end of Class
So lets understand how this works Now that we know about Directory class so we will examine the IL code of C# sample (getDr.exe) and also // IL code of sample code getDr.exe)
IL_0000:call string[] ['mscorlib']'System.IO'.'Directory'::'GetLogicalDrives'()
//IL code of System.IO library)
IL_000b: call int32 Microsoft.Win32.Win32Native::GetLogicalDrives()
//IL code of System.IO library)
.method assembly hidebysig static pinvokeimpl("kernel32.dll" lasterr winapi)
int32 GetLogicalDrives() cil managed preservesig
{ }
If you see the above IL code of Similarly we will see a sample code to create and change directory. For this sample code we will be using chgDr.csusing System;
using System.IO;
class chgDr
{
public static void Main()
{
try
{
Console.WriteLine("-->Your Current Directory");
String curDir=Directory.GetCurrentDirectory();
Console.WriteLine(curDir);
String newDir="TestDir";
Directory.CreateDirectory(newDir);
Console.WriteLine("--> '" + newDir + "' Directory created");
Directory.SetCurrentDirectory(newDir);
Console.WriteLine("--> Changing to '" + newDir +
"' Directory ");
Console.WriteLine(Directory.GetCurrentDirectory());
Console.WriteLine("--> Changing to '" +
curDir + "' Directory ");
Directory.SetCurrentDirectory(curDir);
Console.WriteLine(Directory.GetCurrentDirectory());
Console.WriteLine("--> Deleting '" + newDir + "' Directory ");
if(Directory.Exists(newDir))
Directory.Delete(newDir);
Console.WriteLine("--> Checking '" + newDir +
"' Directory Exists or not");
if(!Directory.Exists(newDir))
Console.WriteLine("'" + newDir + "' Does not exists ");
}
catch(IOException e){ Console.WriteLine(e.ToString()); }
}
}Output:
D:\samples\vstudio>chgDr
-->Your Current Directory
D:\samples\vstudio
--> 'TestDir' Directory created
--> Changing to 'TestDir' Directory
D:\samples\vstudio\TestDir
--> Changing to 'D:\samples\vstudio' Directory
D:\samples\vstudio
--> Deleting 'TestDir' Directory
--> Checking 'TestDir' Directory Exists or not
'TestDir' Does not exists
Some points to remember:
Further reading
|
|||||||||||||||||||||||||||||||||||||||||||||||||||