![Phân tích tư tưởng của nhân dân qua đoạn thơ: Những người vợ nhớ chồng… Những cuộc đời đã hóa sông núi ta trong Đất nước của Nguyễn Khoa Điềm](https://timtailieu.net/upload/document/136415/phan-tich-tu-tuong-cua-nhan-dan-qua-doan-tho-039-039-nhung-nguoi-vo-nho-chong-nhung-cuoc-doi-da-hoa-song-nui-ta-039-039-trong-dat-nuoc-cua-nguyen-khoa-136415.jpg)
File and Registry Operations part 2
Số trang: 5
Loại file: pdf
Dung lượng: 124.47 KB
Lượt xem: 10
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Di chuyển, Sao chép, Huỷ File Chúng ta vừa mới đế cập di chuyển và huỷ files hoặc folders bằng phương thức MoveTo() và Delete() của lớp FileInfo và DirectoryInfo. Các phương thức tương đương nhau trên các lớp File và Directory là Move() và Delete().
Nội dung trích xuất từ tài liệu:
File and Registry Operations part 2Di chuyển, Sao chép, Huỷ File Chúng ta vừa mới đế cập di chuyển và huỷ files hoặc folders bằng phương thứcMoveTo() và Delete() của lớp FileInfo và DirectoryInfo. Các phương thức tương đươngnhau trên các lớp File và Directory là Move() và Delete(). Lớp FileInfo và File cũng cócách thức thực hiện tương tự, CopyTo() and Copy(). Không có fương thức nào copy cácfolder, tuy nhiên bạn có thể copy từng file trong folder. Tất cả các phương thức này đều hoàn toàn trực giác, bạn có thể tìm kiếm chi tiếttrong help MSDN. Trong phần này chúng ta sẽ học cách gọi các phương thức tỉnhMove(), Copy(), và Delete() trong lớp File.Để thực hiện chúng ta dùng bài học phầntrước FileProperties làm ví dụ, FilePropertiesAndMovement. Ví dụ này ta sẽ thêm tínhnăng mỗi lần thuộc tính của một file được hiển thị, ứng dụng này sẽ cho ta chọn lựa thêmxoá file hoặc di chuyển hoặc sao chép nó đến vị trí khácVí dụ: FilePropertiesAndMovementVí dụ mới như sau: Từ hình trên chúng ta có thể thấy nó rất giống nhau trong lần xuất hiện ở ví dụFileProperties, Ngoại trừ chúng có thêm một nhóm gồm ba nút button và một textbox tạiphía dưới cửa xổ. Các điều khiển này chỉ hiện khi ví dụ hiển thị thuộc tính của một file-Lần khác chúng bị ẩn, Khi thuộc tính của file được hiển thị,FilePropertiesAndMovement tự động đặt tên đầy đủ đường dẫn của file đó ở cuối của xổtrong textbox. User có thể nhấn bất kỳ buttons để thực hiện phép toán thích hợp. Khichương trình chạy một message box tương ứng được hiển thị xác nhận hành động. Để mã hoá chúng ta cần thêm các điều khiển thích hợp, giống như thêm các sự kiệnđiều khiển cho ví dụ FileProperties . Chúng ta tạo các controls mới với tên buttonDelete,buttonCopyTo, buttonMoveTo, và textBoxNewPath.Chúng ta sẽ thấy sự kiện điều kiện nhận được khi user nhấn vào Delete button;protected void OnDeleteButtonClick(object sender, EventArgs e){ try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = Really delete the file + filePath + ?; if (MessageBox.Show(query, Delete File?, MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Delete(filePath); DisplayFolderList(currentFolderPath); } } catch(Exception ex) { MessageBox.Show(Unable to delete file. The following exception + occurred: + ex.Message, Failed); }} Đoạn code thực hiện phương thức này sẽ có phần nắm bắt lỗi, thông báo sẽ lỗikhông thể xoá được nếu file xoá đang thực hiện trên một tiến trình khác. Chúng ta xâydựng đường dẫn của file của file bị xoá từ trường CurrentParentPath , Nơi nó chứa dườngdẫn thư mục cha, và tên file trong textBoxFileName textbox:Phương thức di chuyển và sao chép file được cấu trúc :protected void OnMoveButtonClick(object sender, EventArgs e){ try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = Really move the file + filePath + to + textBoxNewPath.Text + ?; if (MessageBox.Show(query, Move File?, MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Move(filePath, textBoxNewPath.Text); DisplayFolderList(currentFolderPath); } } catch(Exception ex) { MessageBox.Show(Unable to move file. The following exception + occurred: + ex.Message, Failed); }}protected void OnCopyButtonClick(object sender, EventArgs e){ try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = Really copy the file + filePath + to + textBoxNewPath.Text + ?; if (MessageBox.Show(query, Copy File?, MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Copy(filePath, textBoxNewPath.Text); DisplayFolderList(currentFolderPath); } } catch(Exception ex) { MessageBox.Show(Unable to copy file. The following exception + occurred: + ex.Message, Failed); }} Chúng ta cũng tạo buttons và textbox mới được đánh dấu enabled và disabled ở thờiđiểm thích hợp để enable chúng khi chúng ta hiển thị nội dung của file, chúng ta cầnthêm đoạn code sau:protected void DisplayFileInfo(string fileFullName){ FileInfo theFile = new FileInfo(fileFullName); if (!theFile.Exists) throw new FileNotFoundException(File not found: + fileFullName); textBoxFileName.Text = theFile.Name; textBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString(); textBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString(); textBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString(); textBoxFileSize.Text = theFile.Length.ToString() + bytes; // enable move, copy, delete buttons textBoxNewPath.Text = theFile.FullName; textBoxNewPath.Enabled = true; buttonCopyTo.Enabled = ...
Nội dung trích xuất từ tài liệu:
File and Registry Operations part 2Di chuyển, Sao chép, Huỷ File Chúng ta vừa mới đế cập di chuyển và huỷ files hoặc folders bằng phương thứcMoveTo() và Delete() của lớp FileInfo và DirectoryInfo. Các phương thức tương đươngnhau trên các lớp File và Directory là Move() và Delete(). Lớp FileInfo và File cũng cócách thức thực hiện tương tự, CopyTo() and Copy(). Không có fương thức nào copy cácfolder, tuy nhiên bạn có thể copy từng file trong folder. Tất cả các phương thức này đều hoàn toàn trực giác, bạn có thể tìm kiếm chi tiếttrong help MSDN. Trong phần này chúng ta sẽ học cách gọi các phương thức tỉnhMove(), Copy(), và Delete() trong lớp File.Để thực hiện chúng ta dùng bài học phầntrước FileProperties làm ví dụ, FilePropertiesAndMovement. Ví dụ này ta sẽ thêm tínhnăng mỗi lần thuộc tính của một file được hiển thị, ứng dụng này sẽ cho ta chọn lựa thêmxoá file hoặc di chuyển hoặc sao chép nó đến vị trí khácVí dụ: FilePropertiesAndMovementVí dụ mới như sau: Từ hình trên chúng ta có thể thấy nó rất giống nhau trong lần xuất hiện ở ví dụFileProperties, Ngoại trừ chúng có thêm một nhóm gồm ba nút button và một textbox tạiphía dưới cửa xổ. Các điều khiển này chỉ hiện khi ví dụ hiển thị thuộc tính của một file-Lần khác chúng bị ẩn, Khi thuộc tính của file được hiển thị,FilePropertiesAndMovement tự động đặt tên đầy đủ đường dẫn của file đó ở cuối của xổtrong textbox. User có thể nhấn bất kỳ buttons để thực hiện phép toán thích hợp. Khichương trình chạy một message box tương ứng được hiển thị xác nhận hành động. Để mã hoá chúng ta cần thêm các điều khiển thích hợp, giống như thêm các sự kiệnđiều khiển cho ví dụ FileProperties . Chúng ta tạo các controls mới với tên buttonDelete,buttonCopyTo, buttonMoveTo, và textBoxNewPath.Chúng ta sẽ thấy sự kiện điều kiện nhận được khi user nhấn vào Delete button;protected void OnDeleteButtonClick(object sender, EventArgs e){ try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = Really delete the file + filePath + ?; if (MessageBox.Show(query, Delete File?, MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Delete(filePath); DisplayFolderList(currentFolderPath); } } catch(Exception ex) { MessageBox.Show(Unable to delete file. The following exception + occurred: + ex.Message, Failed); }} Đoạn code thực hiện phương thức này sẽ có phần nắm bắt lỗi, thông báo sẽ lỗikhông thể xoá được nếu file xoá đang thực hiện trên một tiến trình khác. Chúng ta xâydựng đường dẫn của file của file bị xoá từ trường CurrentParentPath , Nơi nó chứa dườngdẫn thư mục cha, và tên file trong textBoxFileName textbox:Phương thức di chuyển và sao chép file được cấu trúc :protected void OnMoveButtonClick(object sender, EventArgs e){ try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = Really move the file + filePath + to + textBoxNewPath.Text + ?; if (MessageBox.Show(query, Move File?, MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Move(filePath, textBoxNewPath.Text); DisplayFolderList(currentFolderPath); } } catch(Exception ex) { MessageBox.Show(Unable to move file. The following exception + occurred: + ex.Message, Failed); }}protected void OnCopyButtonClick(object sender, EventArgs e){ try { string filePath = Path.Combine(currentFolderPath, textBoxFileName.Text); string query = Really copy the file + filePath + to + textBoxNewPath.Text + ?; if (MessageBox.Show(query, Copy File?, MessageBoxButtons.YesNo) == DialogResult.Yes) { File.Copy(filePath, textBoxNewPath.Text); DisplayFolderList(currentFolderPath); } } catch(Exception ex) { MessageBox.Show(Unable to copy file. The following exception + occurred: + ex.Message, Failed); }} Chúng ta cũng tạo buttons và textbox mới được đánh dấu enabled và disabled ở thờiđiểm thích hợp để enable chúng khi chúng ta hiển thị nội dung của file, chúng ta cầnthêm đoạn code sau:protected void DisplayFileInfo(string fileFullName){ FileInfo theFile = new FileInfo(fileFullName); if (!theFile.Exists) throw new FileNotFoundException(File not found: + fileFullName); textBoxFileName.Text = theFile.Name; textBoxCreationTime.Text = theFile.CreationTime.ToLongTimeString(); textBoxLastAccessTime.Text = theFile.LastAccessTime.ToLongDateString(); textBoxLastWriteTime.Text = theFile.LastWriteTime.ToLongDateString(); textBoxFileSize.Text = theFile.Length.ToString() + bytes; // enable move, copy, delete buttons textBoxNewPath.Text = theFile.FullName; textBoxNewPath.Enabled = true; buttonCopyTo.Enabled = ...
Tìm kiếm theo từ khóa liên quan:
máy tính mạng máy tính internet C# ngôn ngữ C# ngôn ngữ ngôn ngữ lập trình lập trình phần mềm file ứng dụngTài liệu liên quan:
-
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 284 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 281 0 0 -
Giáo án Tin học lớp 9 (Trọn bộ cả năm)
149 trang 280 0 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 276 0 0 -
Bài giảng: Lịch sử phát triển hệ thống mạng
118 trang 260 0 0 -
Giáo trình Hệ thống mạng máy tính CCNA (Tập 4): Phần 2
102 trang 259 0 0 -
Ngân hàng câu hỏi trắc nghiệm môn mạng máy tính
99 trang 258 1 0 -
47 trang 242 3 0
-
Đề cương chi tiết học phần Thiết kế và cài đặt mạng
3 trang 241 0 0 -
Giáo trình Lập trình cơ bản với C++: Phần 1
77 trang 235 0 0