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

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

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

C# and NET Framework Bài 8: File and Registry Đoàn Quang Minh minhdqtt@gmail.com http://www.VTPortal.net Last update: 30 December 2006 File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Mục lục Managing the File System Moving, Copying, and Deleting Files Reading and Writing to Files The Registry Ứng dụng File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Managing the File System NET hỗ trợ thao tác làm việc với file – Các tác vụ thông thường liệt kê file, chép, di chuyển, xoá – Các lớp thao tác với file nằm namespace System.IO – Các lớp quan trọng: File, FileInfo, Directory, Path, Làm việc với file folder – Có loại đối tượng làm việc với file folder Directory File: chứa phương thức tĩnh, khởi tạo Thường dùng thực thao tác với file folder Khi thao tác, cần cung cấp đường dẫn đến file hay folder cần làm việc DirectoryInfo FileInfo: cung cấp phương thức đối tượng trên, yêu cầu phải tạo instance Thường dùng thực nhiều thao tác với file folder File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Managing the File System Tên Ý nghĩa CreationTime Thời gian tạo file folder DirectoryName (FileInfo), Parent (DirectoryInfo) Đường dẫn đầy đủ folder chứa file folder thời Exists File hay folder có tồn hay không? Extension Phần mở rộng FullName Tên đầy đủ, đường dẫn LastAccessTime Thời gian lần truy cập cuối LastWriteTime Thời gian lần sửa đổi cuối Name Tên file hay folder Root Folder gốc (chỉ với DirectoryInfo) Length Dung lượng (bytes), với FileInfo File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Managing the File System // khởi tạo biến myFile trỏ đến tập tin FileInfo myFile = new FileInfo(@"C:\How to C Sharp.txt"); // chép sang ổ đĩa D myFile.CopyTo(@"D:\"); // kiểm tra tồn Console.WriteLine(myFile.Exists.ToString()); // ghi thông tin thời điểm tạo file Console.WriteLine(myFile.CreationTime.ToString()); // cập nhật thời điểm tạo file myFile.CreationTime = new DateTime(2001, 1, 1, 7, 30, 0); File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Managing the File System DirectoryInfo theFolder = new DirectoryInfo(folderFullName); if (!theFolder.Exists) throw new DirectoryNotFoundException("Folder not found: " + folderFullName); string currentPath = theFolder.FullName; // Lấy tên thư mục thư mục thời ArrayList folders = new ArrayList(); foreach(DirectoryInfo folder in theFolder.GetDirectories()) folders.Add(folder.Name); // Lấy tên file thư mục thời ArrayList files = new ArrayList(); foreach(FileInfo file in theFolder.GetFiles()) files.Add(file.Name); File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Moving, Copying, and Deleting Files Có thể chép, di chuyển xố tập tin – Phương thức Path.Combine(string, string): trả tên đầy đủ file tạo từ đường dẫn tên file – Phương thức File.Delete(string): xoá tập tin – Phương thức File.Move(string, string): di chuyển file từ vị trí cũ đến vị trí – Phương thức File.Copy(string, string): chép file sang thư mục File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Reading and Writing to Files Đọc ghi file dựa khái niệm stream (luồng liệu) – stream đối tượng dùng để chuyển liệu Do stream luồng dựa nhớ, tập tin, mạng,… – FileStream: đối tượng dùng để đọc ghi file nhị phân – StreamReader StreamWriter: đối tượng dùng để đọc ghi file text – Chú ý: tác vụ đọc ghi hầu hết sử dụng buffer Do đó, với tác vụ ghi, phải đẩy liệu từ buffer lên đĩa trước đóng file File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Reading and Writing to Files Đọc ghi file nhị phân: dùng FileStream – Hàm tạo: cần filename, FileMode, FileAccess, FileShare FileMode: kiểu mở file, Append, Create, CreateNew, Open, OpenOrCreate, Truncate FileAccess: kiểu truy cập, Read, ReadWrite, Write FileShare: kiểu chia sẻ thread, Inheritable, None, Read, ReadWrite, or Write – Để đọc ghi byte, dùng hàm: ReadByte(): đọc byte từ stream WriteByte(byte): ghi byte vào stream Read/Write(byte[], int off, int count): đọc/ghi mảng byte off, độ dài count – Sau đọc/ghi, dùng Close() để đóng file File and Registry - Editor: Đoàn Quang Editor: Đoà Minh Reading and Writing to Files Đọc ghi file text: dùng StreamReader StreamWriter – Có thể khởi tạo StreamReader dựa Tên file cần đọc Một FileStream khác Hoặc FileInfo với phương thức OpenText() – Có thể khởi tạo StreamWriter dựa Tên file cần đọc, mã encode Một FileStream khác Hoặc FileInfo với phương thức CreatText() – Để đọc ghi, dùng hàm Read()/Write(): đọc ghi ký tự ReadLine()/WriteLine(): đọc ghi dòng ReadToEnd(): đọc đến hết file File and Registry - Editor: Đoàn Quang Editor: Đoà Minh 10 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 - Editor: Đoàn Quang Editor: Đoà Minh 11 The Registry Registry – Là cấu trúc dạng cho phép trinh ứng dụng lưu thơng tin Được quản lý Windows – Để soạn thảo registry, dùng trình regedit – Các thành phần quan trọng: HKEY_CLASSES_ROOT (HKCR): chứa mô tả thành phần COM Windows HKEY_CURRENT_USER (HKCU): chứa thông tin tuỳ biến user thời HKEY_LOCAL_MACHINE (HKLM): các thông tin hardware software HKEY_USERS (HKUSR): chứa thông tin user – Để đọc ghi registry, phải có quyền admin (mặc định) File and Registry - Editor: Đoàn Quang Editor: Đoà Minh 12 The Registry Truy cập registry – namespace: Microsoft.Win32 Registry: chứa mô tả key registry RegistryKey: cho phép thao tác với key – Các phương thức RegistryKey OpenSubKey(): mở key (tiếp tục xuống) CreateSubKey()/DeleteSubKey(): tạo/xoá key GetValue()/SetValue(): lấy/đặt giá trị key File and Registry - Editor: Đoàn Quang Editor: Đoà Minh 13 Ứng dụng Ứng dụng lớp thao tác với tập tin registry – Tạo trình soạn thảo văn – Đọc ghi cấu hình registry File truy cập lần cuối Các thiết lập màu chữ, màu nền, font mặc định Các thơng tin lưu vết xâu tìm kiếm thay thế… File and Registry - Editor: Đoàn Quang Editor: Đoà Minh 14 Tài liệu tham khảo Professional C#, Second Edition http://www.asp.net http://www.microsoft.com/net/default.mspx http://www.codeproject.com Địa download tài liệu http://www.thanglong.edu.vn/giang-day/tab.aspx Diễn đàn C# & NET http://www.thanglong.edu.vn/forum/cmd/0/categ ory/hoc-tap-nghien-cuu/dot-net/tab.aspx File and Registry - Editor: Đoàn Quang Editor: Đoà Minh 15 ... hết file File and Registry - Editor: Đoàn Quang Editor: Đoà Minh 10 Reading and Writing to Files void WriteToTextFile(string FileName, string strMessage) { FileStream myFileStream = new FileStream(FileName,... folders.Add(folder.Name); // Lấy tên file thư mục thời ArrayList files = new ArrayList(); foreach(FileInfo file in theFolder.GetFiles()) files.Add (file. Name); File and Registry - Editor: Đoàn Quang... Quang Editor: Đoà Minh Reading and Writing to Files Đọc ghi file nhị phân: dùng FileStream – Hàm tạo: cần filename, FileMode, FileAccess, FileShare FileMode: kiểu mở file, Append, Create, CreateNew,

Ngày đăng: 25/01/2014, 18:20

Từ khóa liên quan

Mục lục

  • C# and .NET Framework Bài 8: File and Registry

  • Mục lục

  • Managing the File System

  • Managing the File System

  • Managing the File System

  • Managing the File System

  • Moving, Copying, and Deleting Files

  • Reading and Writing to Files

  • Reading and Writing to Files

  • Reading and Writing to Files

  • Reading and Writing to Files

  • The Registry

  • The Registry

  • Ứng dụng

  • Tài liệu tham khảo

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan