Docsity

Working with Files and Directories - C Sharp Programming - Lecture Slides, Slides of C Sharp Programming

Partial preview of the text

Download Working with Files and Directories - C Sharp Programming - Lecture Slides and more Slides C Sharp Programming in PDF only on Docsity!

Working with Files and

Directories

Overview

 Understanding the Windows file

system

 The Path class

 Reading and writing files

File System Organization (2)

 Relative references begin with:

 .\ to reference the current directory  ..\ to reference the parent directory

 Example

..\Chapter.05\Complete\Temp.doc

File System (Examples)

 Example to create an absolute

reference to Temp.doc in the folder named C:\Windows\Temp

mkdir C:\Windows\Temp\Temp.doc

The Path Class (Introduction)

 It’s static members get information

about path and drive separators

 It’s used mostly for compatibility

with other operating systems and

other operating system versions

The Path Class (Members Properties)

 The DirectorySeparatorChar

property gets the directory

separator

 Typically a backslash ()

 The VolumeSeparatorChar property

gets the character that separates

the volume from the path name

 Typically a full colon (:) in Windows

 Your book mentions others

The Path Class (File Information 1)

 The following members get information about a file (even if it does not exist)  Each member accepts a file name as its argument

GetDirectoryName gets the directory part of the file  The trailing backslash is removed  GetFileName gets the file name with the file extension  GetExtension gets only the file extension

The Path Class (File Information 2)

 GetPathRoot gets the drive

designator with a trailing slash

 HasExtension returns True if the

file has an extension

 IsPathRooted returns True if the

file reference is an absolute

reference

The Directory Class

(Current Directory)

 SetCurrentDirectory changes the

current working directory

 The method accepts a string containing the directory name  A DirectoryNotFound exception is thrown if the directory does not exist

 GetWorkingDirectory returns a

string containing the current

working directory

The Directory Class

(Managing Directories 1)

CreateDirectory creates a directory or subdirectory  A string argument is passed containing the directory name  Delete deletes a directory or subdirectory  The first argument contains the directory to delete  The optional second argument contains True to force subdirectories and files to be deleted

The Directory Class

(Example)

 Create an absolute and relative

directory

Directory.CreateDirectory("C:\Demo") Directory.CreateDirectory("Demo")

 Delete a directory

Directory.Delete("C:\Demo") Directory.Delete("C:\Demo", True)

 Test whether a directory exists

Dim Exists As Boolean Exists = Directory.Exists("C:\Demo")

The File Class

 Operates similarly to the Directory class  Static methods create, move, and delete files  Copy method copies source file to destination file  Create method creates a new file  Delete method deletes a file  Exists method returns a Boolean value indicating whether the file exists  GetAttributes and SetAttributes get and set file attributes, respectively  GetCreationTime , GetLastAccessTime and GetLastWriteTime get file modification information

Bitwise Operations

The DriveInfo Class

 The DriveInfo class is used to get

information about a logical drive

 Pass the logical drive name ( String ) as an argument to the GetDrives constructor  The method returns an array of DriveInfo types  (one for each drive)