Tải bản đầy đủ (.pdf) (15 trang)

Tài liệu Bài 8: File and Registry ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (217.67 KB, 15 trang )

File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
1
1
C# and .NET Framework
C# and .NET Framework
B
B
à
à
i 8:
i 8:
File and Registry
File and Registry
Đo
Đo
à


à
n Quang Minh
n Quang Minh


http
http
://www.VTPortal.net
://www.VTPortal.net
Last update:
Last update:
30. December 2006
30. December 2006
File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
2

2
M
M


c l
c l


c
c
Managing the File System
Managing the File System
Moving, Copying, and Deleting Files
Moving, Copying, and Deleting Files
Reading and Writing to Files
Reading and Writing to Files
The Registry
The Registry


ng
ng
d
d


ng
ng
File and Registry

File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
3
3
Managing the File System
Managing the File System
.NET h
.NET h


tr
tr


c
c
á

á
c thao t
c thao t
á
á
c l
c l
à
à
m vi
m vi


c v
c v


i file
i file


C
C
á
á
c t
c t
á
á
c v

c v


thông thư
thông thư


ng như li
ng như li


t kê file, sao ch
t kê file, sao ch
é
é
p, di chuy
p, di chuy


n,
n,
xo
xo
á
á
.
.


C

C
á
á
c l
c l


p thao t
p thao t
á
á
c v
c v


i file n
i file n


m trong namespace System.IO
m trong namespace System.IO


C
C
á
á
c l
c l



p quan tr
p quan tr


ng: File, FileInfo, Directory, Path,...
ng: File, FileInfo, Directory, Path,...
L
L
à
à
m vi
m vi


c v
c v


i file v
i file v
à
à
folder
folder


C
C
ó

ó
2
2
lo
lo


i
i
đ
đ


i
i




ng
ng
l
l
à
à
m
m
vi
vi



c
c
v
v


i
i
file
file
v
v
à
à
folder
folder
Directory
Directory
v
v
à
à
File:
File:
ch
ch


ch

ch


a
a
c
c
á
á
c
c
phương
phương
th
th


c
c
t
t
ĩ
ĩ
nh
nh
,
,
không
không
th

th


kh
kh


i
i
t
t


o
o
.
.
Thư
Thư


ng
ng
d
d
ù
ù
ng
ng
khi

khi
ch
ch


th
th


c
c
hi
hi


n
n
1
1
thao
thao
t
t
á
á
c
c
v
v



i
i
1 file
1 file
ho
ho


c
c
folder.
folder.
Khi
Khi
thao
thao
t
t
á
á
c
c
,
,
ch
ch


c

c


n
n
cung
cung
c
c


p
p
đư
đư


ng
ng
d
d


n
n
đ
đ
ế
ế
n

n
file hay folder
file hay folder
c
c


n
n
l
l
à
à
m
m
vi
vi


c
c
DirectoryInfo
DirectoryInfo
v
v
à
à
FileInfo
FileInfo
:

:
cung
cung
c
c


p
p
c
c
á
á
c
c
phương
phương
th
th


c
c
như
như
2
2
đ
đ



i
i




ng
ng
trên
trên
,
,
nhưng
nhưng
yêu
yêu
c
c


u
u
ph
ph


i
i
t

t


o
o
instance.
instance.
Thư
Thư


ng
ng
d
d
ù
ù
ng
ng
khi
khi
th
th


c
c
hi
hi



n
n
nhi
nhi


u
u
thao
thao
t
t
á
á
c
c
v
v


i
i
1 file
1 file
ho
ho


c

c
folder.
folder.
File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
4
4
Managing the File System
Managing the File System
Tên
Ý
Ý
ngh
ngh
ĩ
ĩ

a
a
CreationTime Thờigiantạo file hoặc folder
DirectoryName (FileInfo),
Parent (DirectoryInfo)
Đường dẫn đầy đủ củafolder chứa file
hoặc folder hiệnthời
Exists File hay folder có tồntại hay không?
Extension Phầnmở rộng
FullName Tên đầy đủ, cảđường dẫn
LastAccessTime Thờigianlầntruycậpcuối
LastWriteTime Thờigianlầnsửa đổicuối
Name Tên file hay folder
Root Folder gốc(chỉ với DirectoryInfo)
Length Dung lượng (bytes), chỉ với FileInfo
File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh

Minh
5
5
Managing the File System
Managing the File System
//
//
kh
kh


i
i
t
t


o
o
bi
bi
ế
ế
n
n
myFile
myFile
tr
tr



đ
đ
ế
ế
n
n
m
m


t
t
t
t


p
p
tin
tin
FileInfo
FileInfo
myFile
myFile
= new
= new
FileInfo(@"C
FileInfo(@"C
:

:
\
\
How to C
How to C
Sharp.txt
Sharp.txt
");
");
//
//
sao
sao
ch
ch
é
é
p
p
sang
sang


đ
đ
ĩ
ĩ
a
a
D

D
myFile.CopyTo(@"D
myFile.CopyTo(@"D
:
:
\
\
");
");
//
//
ki
ki


m
m
tra
tra
s
s


t
t


n
n
t

t


i
i
Console.WriteLine(myFile.Exists.ToString
Console.WriteLine(myFile.Exists.ToString
());
());
//
//
ghi
ghi
thông
thông
tin
tin
th
th


i
i
đi
đi


m
m
t

t


o
o
file
file
Console.WriteLine(myFile.CreationTime.ToString
Console.WriteLine(myFile.CreationTime.ToString
());
());
//
//
c
c


p
p
nh
nh


t
t
th
th


i

i
đi
đi


m
m
t
t


o
o
file
file
myFile.CreationTime
myFile.CreationTime
= new DateTime(2001, 1, 1, 7, 30, 0);
= new DateTime(2001, 1, 1, 7, 30, 0);
File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à

à
n Quang
n Quang
Minh
Minh
6
6
Managing the File System
Managing the File System
DirectoryInfo
DirectoryInfo
theFolder
theFolder
= new
= new
DirectoryInfo(folderFullName
DirectoryInfo(folderFullName
);
);
if (!
if (!
theFolder.Exists
theFolder.Exists
)
)
throw new
throw new
DirectoryNotFoundException("Folder
DirectoryNotFoundException("Folder
not found: " +

not found: " +
folderFullName
folderFullName
);
);
string
string
currentPath
currentPath
=
=
theFolder.FullName
theFolder.FullName
;
;
//
//
L
L


y
y
tên
tên
c
c
á
á
c

c
thư
thư
m
m


c
c
con
con
c
c


a
a
thư
thư
m
m


c
c
hi
hi


n

n
th
th


i
i
ArrayList
ArrayList
folders = new
folders = new
ArrayList
ArrayList
();
();
foreach(DirectoryInfo
foreach(DirectoryInfo
folder in
folder in
theFolder.GetDirectories
theFolder.GetDirectories
())
())
folders.Add(folder.Name
folders.Add(folder.Name
);
);
//
//
L

L


y
y
tên
tên
c
c
á
á
c
c
file
file
trong
trong
thư
thư
m
m


c
c
hi
hi


n

n
th
th


i
i
ArrayList
ArrayList
files = new
files = new
ArrayList
ArrayList
();
();
foreach(FileInfo
foreach(FileInfo
file in
file in
theFolder.GetFiles
theFolder.GetFiles
())
())
files.Add(file.Name
files.Add(file.Name
);
);

×