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

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

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
);
);
File and Registry
File and Registry
-

-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
7
7
Moving, Copying, and Deleting Files
Moving, Copying, and Deleting Files
C
C
ó
ó
th
th


sao
sao
ch
ch
é

é
p
p
,
,
di
di
chuy
chuy


n
n
ho
ho


c
c
xo
xo
á
á
t
t


p
p
tin.

tin.


Phương
Phương
th
th


c
c
Path.Combine(string
Path.Combine(string
, string):
, string):
tr
tr


v
v


tên
tên
đ
đ


y

y
đ
đ


c
c


a
a
file
file
t
t


o
o
t
t


đư
đư


ng
ng
d

d


n
n
v
v
à
à
tên
tên
file.
file.


Phương
Phương
th
th


c
c
File.Delete(string
File.Delete(string
):
):
xo
xo
á

á
t
t


p
p
tin.
tin.


Phương
Phương
th
th


c
c
File.Move(string
File.Move(string
, string):
, string):
di
di
chuy
chuy


n

n
file
file
t
t


v
v


tr
tr
í
í
c
c
ũ
ũ
đ
đ
ế
ế
n
n
v
v


tr

tr
í
í
m
m


i
i
.
.


Phương
Phương
th
th


c
c
File.Copy(string
File.Copy(string
, string):
, string):
sao
sao
ch
ch
é

é
p
p
file sang
file sang
m
m


t
t
thư
thư
m
m


c
c
m
m


i
i
.
.
File and Registry
File and Registry
-

-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
8
8
Reading and Writing to Files
Reading and Writing to Files
Đ
Đ


c
c
v
v
à
à
ghi
ghi
file

file
d
d


a
a
trên
trên
kh
kh
á
á
i
i
ni
ni


m
m
stream (
stream (
lu
lu


ng
ng
d

d


li
li


u
u
)
)


stream
stream
l
l
à
à
đ
đ


i
i




ng

ng
d
d
ù
ù
ng
ng
đ
đ


chuy
chuy


n
n
d
d


li
li


u
u
. Do
. Do
đ

đ
ó
ó
stream
stream
c
c
ó
ó
th
th


l
l
à
à
lu
lu


ng
ng
d
d


a
a
trên

trên
b
b


nh
nh


,
,
trên
trên
t
t


p
p
tin,
tin,
trên
trên
m
m


ng
ng
,

,




FileStream
FileStream
:
:
đ
đ


i
i




ng
ng
d
d
ù
ù
ng
ng
đ
đ



đ
đ


c
c
ghi
ghi
file
file
nh
nh


phân
phân
.
.


StreamReader
StreamReader
v
v
à
à
StreamWriter
StreamWriter
:

:
đ
đ


i
i




ng
ng
d
d
ù
ù
ng
ng
đ
đ


đ
đ


c
c
ghi

ghi
file text.
file text.


Ch
Ch
ú
ú
ý:
ý:
c
c
á
á
c
c
t
t
á
á
c
c
v
v


đ
đ



c
c
ghi
ghi
h
h


u
u
h
h
ế
ế
t
t
đ
đ


u
u
s
s


d
d



ng
ng
buffer.
buffer.
Do
Do
đ
đ
ó
ó
,
,
v
v


i
i
t
t
á
á
c
c
v
v


ghi

ghi
,
,
ph
ph


i
i
đ
đ


y
y
d
d


li
li


u
u
t
t


buffer

buffer
lên
lên
đ
đ
ĩ
ĩ
a
a
trư
trư


c
c
khi
khi
đ
đ
ó
ó
ng
ng
file.
file.
File and Registry
File and Registry
-
-
Editor

Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
9
9
Reading and Writing to Files
Reading and Writing to Files
Đ
Đ


c
c
ghi
ghi
file
file
nh
nh


phân

phân
:
:
d
d
ù
ù
ng
ng
FileStream
FileStream
.
.


H
H
à
à
m
m
t
t


o
o
:
:
c

c


n
n
ch
ch


ra
ra
filename,
filename,
FileMode
FileMode
,
,
FileAccess
FileAccess
,
,
FileShare
FileShare
.
.
FileMode
FileMode
:
:
ki

ki


u
u
m
m


file,
file,
c
c
ó
ó
th
th


l
l
à
à
Append, Create,
Append, Create,
CreateNew
CreateNew
,
,
Open,

Open,
OpenOrCreate
OpenOrCreate
,
,
ho
ho


c
c
Truncate.
Truncate.
FileAccess
FileAccess
:
:
ki
ki


u
u
truy
truy
c
c


p

p
,
,
c
c
ó
ó
th
th


l
l
à
à
Read,
Read,
ReadWrite
ReadWrite
,
,
ho
ho


c
c
Write.
Write.
FileShare

FileShare
:
:
ki
ki


u
u
chia
chia
s
s


gi
gi


a
a
c
c
á
á
c
c
thread,
thread,
c

c
ó
ó
th
th


l
l
à
à
Inheritable, None,
Inheritable, None,
Read,
Read,
ReadWrite
ReadWrite
, or Write
, or Write


Đ
Đ


đ
đ


c

c
v
v
à
à
ghi
ghi
byte,
byte,
d
d
ù
ù
ng
ng
c
c
á
á
c
c
h
h
à
à
m
m
:
:
ReadByte

ReadByte
():
():
đ
đ


c
c
m
m


t
t
byte
byte
t
t


stream
stream
WriteByte(byte
WriteByte(byte
):
):
ghi
ghi
m

m


t
t
byte
byte
v
v
à
à
o
o
stream
stream
Read/
Read/
Write(byte
Write(byte
[],
[],
int
int
off,
off,
int
int
count):
count):
đ

đ


c/ghi
c/ghi
m
m


t
t
m
m


ng
ng
byte
byte
b
b


t
t
đ
đ


u

u
t
t


off,
off,
đ
đ


d
d
à
à
i
i
count
count


Sau
Sau
khi
khi
đ
đ


c/ghi

c/ghi
,
,
d
d
ù
ù
ng
ng
Close()
Close()
đ
đ


đ
đ
ó
ó
ng
ng
file
file
File and Registry
File and Registry
-
-
Editor
Editor
:

:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
10
10
Reading and Writing to Files
Reading and Writing to Files
Đ
Đ


c
c
v
v
à
à
ghi
ghi
file text:
file text:
d
d
ù

ù
ng
ng
StreamReader
StreamReader
v
v
à
à
StreamWriter
StreamWriter


C
C
ó
ó
th
th


kh
kh


i
i
t
t



o
o
StreamReader
StreamReader
d
d


a
a
trên
trên
Tên
Tên
file
file
c
c


n
n
đ
đ


c
c
M

M


t
t
FileStream
FileStream
kh
kh
á
á
c
c
Ho
Ho


c
c
m
m


t
t
FileInfo
FileInfo
v
v



i
i
phương
phương
th
th


c
c
OpenText
OpenText
()
()


C
C
ó
ó
th
th


kh
kh


i

i
t
t


o
o
StreamWriter
StreamWriter
d
d


a
a
trên
trên
Tên
Tên
file
file
c
c


n
n
đ
đ



c
c
,
,


encode
encode
M
M


t
t
FileStream
FileStream
kh
kh
á
á
c
c
Ho
Ho


c
c
m

m


t
t
FileInfo
FileInfo
v
v


i
i
phương
phương
th
th


c
c
CreatText
CreatText
()
()


Đ
Đ



đ
đ


c
c
v
v
à
à
ghi
ghi
,
,
d
d
ù
ù
ng
ng
c
c
á
á
c
c
h
h
à

à
m
m
Read()/Write():
Read()/Write():
đ
đ


c
c
v
v
à
à
ghi
ghi
m
m


t
t


t
t


ReadLine()/WriteLine

ReadLine()/WriteLine
():
():
đ
đ


c
c
v
v
à
à
ghi
ghi
m
m


t
t
dòng
dòng
ReadToEnd
ReadToEnd
():
():
đ
đ



c
c
đ
đ
ế
ế
n
n
h
h
ế
ế
t
t
file
file
File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang

n Quang
Minh
Minh
11
11
Reading and Writing to Files
Reading and Writing to Files
void WriteToTextFile(string FileName, string strMessage)
{
FileStream myFileStream = new FileStream(FileName, FileMode.Append, FileAccess.Write, System.IO.FileShare.None);
System.IO.StreamWriter myWriter = new StreamWriter(myFileStream);
myWriter.WriteLine(System.DateTime.Now.ToString() + " - " + strMessage);
myWriter.Close();
myFileStream.Close();
}
string ReadFileTextContent(string Filename)
{
StreamReader myStreamReader = null;
string FilePath = System.Web.HttpContext.Current.Server.MapPath(Filename);
string result = string.Empty;
try
{
myStreamReader = File.OpenText(FilePath);
result = myStreamReader.ReadToEnd();
}
catch(Exception exc)
{
throw;
}
finally

{
if (myStreamReader != null) myStreamReader.Close();
}
return result;
}
File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
12
12
The Registry
The Registry
Registry
Registry


L

L
à
à
m
m


t
t
c
c


u
u
tr
tr
ú
ú
c
c
d
d


ng
ng
cây
cây
cho

cho
ph
ph
é
é
p
p
c
c
á
á
c
c
trinh
trinh


ng
ng
d
d


ng
ng
c
c
ó
ó
th

th


lưu
lưu
thông
thông
tin.
tin.
Đư
Đư


c
c
qu
qu


n
n


b
b


i
i
Windows

Windows


Đ
Đ


so
so


n
n
th
th


o
o
registry,
registry,
d
d
ù
ù
ng
ng
tr
tr
ì

ì
nh
nh
regedit
regedit
.
.


C
C
á
á
c
c
th
th
à
à
nh
nh
ph
ph


n
n
quan
quan
tr

tr


ng
ng
:
:
HKEY_CLASSES_ROOT (HKCR):
HKEY_CLASSES_ROOT (HKCR):
ch
ch


a
a
c
c
á
á
c
c


t
t


c
c



a
a
c
c
á
á
c
c
th
th
à
à
nh
nh
ph
ph


n
n
COM
COM
trong
trong
Windows.
Windows.
HKEY_CURRENT_USER (HKCU):
HKEY_CURRENT_USER (HKCU):
ch

ch


a
a
c
c
á
á
c
c
thông
thông
tin
tin
tu
tu


bi
bi
ế
ế
n
n
c
c


a

a
user
user
hi
hi


n
n
th
th


i
i
HKEY_LOCAL_MACHINE (HKLM):
HKEY_LOCAL_MACHINE (HKLM):
c
c
á
á
c
c
c
c
á
á
c
c
thông

thông
tin
tin
v
v


hardware
hardware
v
v
à
à
software
software
HKEY_USERS (HKUSR):
HKEY_USERS (HKUSR):
ch
ch


a
a
thông
thông
tin
tin
v
v



c
c
á
á
c
c
user
user


Đ
Đ


đ
đ


c
c
v
v
à
à
ghi
ghi
registry,
registry,
ph

ph


i
i
c
c
ó
ó
quy
quy


n
n
admin (
admin (
m
m


c
c
đ
đ


nh
nh
)

)
File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
13
13
The Registry
The Registry
Truy
Truy
c
c


p
p
registry

registry


namespace: Microsoft.Win32
namespace: Microsoft.Win32
Registry:
Registry:
ch
ch


a
a
c
c
á
á
c
c


t
t


v
v


key

key
trong
trong
registry
registry
RegistryKey
RegistryKey
:
:
cho
cho
ph
ph
é
é
p
p
thao
thao
t
t
á
á
c
c
v
v


i

i
c
c
á
á
c
c
key
key
– Các phương thứccủa RegistryKey
OpenSubKey()
:
:
m
m


key con (
key con (
ti
ti
ế
ế
p
p
t
t


c

c
đi
đi
xu
xu


ng
ng
)
)
CreateSubKey()
/DeleteSubKey
/DeleteSubKey
():
():
t
t


o/xo
o/xo
á
á
key con
key con
GetValue()/SetValue
GetValue()/SetValue
():
():

l
l


y
y
/
/
đ
đ


t
t
gi
gi
á
á
tr
tr


c
c


a
a
key
key

File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
14
14


ng
ng
d
d


ng
ng



ng
ng
d
d


ng
ng
c
c
á
á
c
c
l
l


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



i
i
t
t


p
p
tin
tin
v
v
à
à
registry
registry


T
T


o
o
tr
tr
ì
ì

nh
nh
so
so


n
n
th
th


o
o
văn
văn
b
b


n
n


Đ
Đ


c
c

v
v
à
à
ghi
ghi
c
c


u
u
h
h
ì
ì
nh
nh
trong
trong
registry
registry
File
File
truy
truy
c
c



p
p
l
l


n
n
cu
cu


i
i
C
C
á
á
c
c
thi
thi
ế
ế
t
t
l
l



p
p
như
như
m
m
à
à
u
u
ch
ch


,
,
m
m
à
à
u
u
n
n


n
n
, font
, font

m
m


c
c
đ
đ


nh
nh
C
C
á
á
c
c
thông
thông
tin
tin
lưu
lưu
v
v
ế
ế
t
t

như
như
c
c
á
á
c
c
xâu
xâu
t
t
ì
ì
m
m
ki
ki
ế
ế
m
m
v
v
à
à
thay
thay
th
th

ế
ế


File and Registry
File and Registry
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
15
15
T
T
à
à
i li
i li



u tham kh
u tham kh


o
o
Professional C#, Second Edition
Professional C#, Second Edition
http
http
://www.asp.net
://www.asp.net
/> />

Đ
Đ


a ch
a ch


download t
download t
à
à
i li
i li



u
u
/> />-
-
day/tab.aspx
day/tab.aspx
Di
Di


n đ
n đ
à
à
n C# & .NET
n C# & .NET
http://www.
http://www.
thanglong.edu.vn
thanglong.edu.vn
/forum/cmd/0/categ
/forum/cmd/0/categ
ory/hoc
ory/hoc
-
-
tap
tap
-
-

nghien
nghien
-
-
cuu/dot
cuu/dot
-
-
net/tab.aspx
net/tab.aspx

×