Bài t?ng bư?c này mô t? cách làm sáu cơ b?n
ho?t đ?ng t?p tin đ?u vào/đ?u ra (I/O) t? Microsoft Visual C++ 2005 ho?c trong Microsoft Visual C++.NET. N?u b?n chưa quen v?i vi?c.NET Framework,
b?n s? th?y r?ng mô h?nh đ?i tư?ng cho các ho?t đ?ng t?p tin trong các.NET Framework là tương t? như
các FileSystemObject (FSO) mà là ph? bi?n v?i nhi?u nhà phát tri?n Microsoft Visual Studio 6.0. Đ?
làm cho s? chuy?n đ?i d? dàng hơn, các ch?c năng đư?c th? hi?n trong này
bài vi?t d?a trên bài vi?t cơ s? ki?n th?c Microsoft sau:
Làm th? nào đ? s? d?ng FileSystemObject v?i
Visual Basic
B?n v?n có th? s? d?ng các FileSystemObject trong các.NET Framework. B?i v? các FileSystemObject là m?t thành ph?n mô h?nh đ?i tư?ng thành ph?n (COM), các.NET Framework đ?i h?i mà
truy c?p vào các đ?i tư?ng thông qua các l?p Interop. Các.NET Framework t?o ra m?t wrapper cho
các thành ph?n cho b?n n?u b?n mu?n s? d?ng nó. Tuy nhiên, các Tệp l?p h?c, các FileInfo l?p h?c, các Thư mục, DirectoryInfo các l?p h?c, và khác liên quan đ?n các l?p h?c trong các.NET Framework, cung c?p
ch?c năng không có s?n v?i FSO, mà không có chi phí c?a các
Interop l?p.
Yêu c?u
Danh sách sau v?ch ra đư?c đ? ngh? ph?n c?ng, ph?n m?m,
h? t?ng m?ng, và gói d?ch v? đư?c yêu c?u:
Visual C++ 2005 ho?c Visual c + +.NET
Ch?ng t? File i/O ho?t đ?ng
Các ví d? trong bài vi?t này mô t? cơ b?n t?p I/O ho?t đ?ng kinh doanh.
Ph?n "T?ng bư?c ví d?" mô t? làm th? nào đ? t?o ra m?t chương tr?nh m?u
đó ch?ng t? sáu t?p tin I/O thao taùc sau:
Đ?c m?t t?p tin văn b?n
Vi?t m?t t?p tin văn b?n
Xem chi tieát taäp tin
Danh sách ? đ?a
Danh sách thư m?c
Danh sách t?p tin
Đ?c m?t t?p tin văn b?n
Sau m?u m? s? d?ng m?t StreamReader l?p h?c đ? đ?c m?t t?p tin văn b?n. N?i dung c?a t?p tin là
thêm vào m?t ListBox ki?m soát. Các H?y th?... b?t kh?i đư?c s? d?ng đ? c?nh báo các chương tr?nh n?u t?p tin là có s?n ph?m nào. Có
r?t nhi?u cách đ? xác đ?nh th?i đi?m cu?i cùng c?a t?p tin đư?c đ?t t?i; m?u này
s? d?ng các PEEK phương pháp đ? ki?m tra d?ng ti?p theo trư?c khi đ?c nó.
Chú ý Visual C++ 2005, b?n ph?i thêm ph? bi?n ngôn ng? runtime h? tr? biên d?ch tùy ch?n)/CLR:oldSyntax) đ? thành công biên d?ch m?u m? trư?c đây là qu?n l? C++.
Đ? thêm ph? bi?n h? tr? th?i gian ch?y ngôn ng? biên d?ch tùy ch?n, h?y làm theo các bư?c sau:
Nh?p vào Dự án, sau đó b?mProjectName Thu?c tính.
Chú ý ProjectName là m?t gi? ch? cho tên c?a d? án.
M? r?ng C?u h?nh thu?c tính, sau đó b?mT?ng quát.
Trong ngăn bên ph?i, b?m vào đ? ch?n H? tr? th?i gian ch?y ngôn ng? ph? bi?n, cú pháp c? (/ clr:oldSyntax) trong cácPh? bi?n th?i gian ch?y ngôn ng? h? tr? các thi?t đ?t c?a d? án.
Nh?p vàoÁp d?ng, sau đó b?m Ok.
Đ? bi?t thêm thông tin v? th?i gian ch?y ngôn ng? ph? bi?n h? tr? tùy ch?n biên d?ch, ghé thăm Web site sau c?a Microsoft Developer Network (MSDN):
M? m?u này s? d?ng m?t StreamWriter l?p đ? t?o ra và vi?t ra t?p. N?u b?n có m?t hi?n có
t?p tin, b?n có th? m? nó trong cùng m?t cách.
StreamWriter* pwriter = new StreamWriter(S"c:\\KBTest.txt");
pwriter->WriteLine(S"File created using StreamWriter class.");
pwriter->Close();
listBox1->Items->Clear();
String *filew = new String(S"File Written to C:\\KBTest.txt");
listBox1->Items->Add(filew);
Xem chi tieát taäp tin
M? m?u này s? d?ng m?t FileInfo l?p h?c đ? truy c?p vào m?t t?p tin thu?c tính. Notepad.exe đư?c s? d?ng ? đây
m?u. Các thu?c tính xu?t hi?n trong m?t ListBox ki?m soát.
M? m?u này s? d?ng các Thư mục và Ổ đĩa các l?p h?c vào danh sách các ? đ?a logic trên m?t h? th?ng. Đ?i v?i m?u này,
các k?t qu? xu?t hi?n trong m?t ListBox ki?m soát.
listBox1->Items->Clear();
String* drives[] = Directory::GetLogicalDrives();
int numDrives = drives->get_Length();
for (int i=0; i<numDrives; i++)
{
listBox1->Items->Add(drives[i]);
}
Danh sách c?p con
M? m?u này s? d?ng các GetDirectories phương pháp c?a các Thư mục l?p h?c đ? có đư?c m?t danh sách các thư m?c.
listBox1->Items->Clear();
String* dirs[] = Directory::GetDirectories(windir);
int numDirs = dirs->get_Length();
for (int i=0; i<numDirs; i++)
{
listBox1->Items->Add(dirs[i]);
}
Danh sách t?p tin
M? m?u này s? d?ng các GetFiles phương pháp c?a các Thư mục l?p h?c đ? có đư?c m?t danh sách các t?p tin.
listBox1->Items->Clear();
String* files[]= Directory::GetFiles(this->windir);
int numFiles = files->get_Length();
for (int i=0; i<numFiles; i++)
{
listBox1->Items->Add(files[i]);
}
Nhi?u đi?u có th? đi sai khi m?t ngư?i s? d?ng thu truy c?p vào các t?p tin. Các t?p tin
không t?n t?i, các t?p tin s? d?ng, ho?c ngư?i dùng có th? không có quy?n các
t?p tin thư m?c mà h? đang truy c?p. Xem xét
nh?ng kh? năng khi b?n vi?t m? đ? x? l? các trư?ng h?p ngo?i l? có th?
đư?c t?o ra.
Ví d?-by-step
B?t đ?u Microsoft Visual Studio.NET, Microsoft Visual Studio 2005 ho?c Microsoft Visual C++ 2005 Express Edition.
Trên các Tệp tr?nh đơn, đi?m đ?nM?i, sau đó b?m Dự án.
Dư?i Các lo?i d? án, b?mVisual C++ d? án. Dư?i Khuôn mẫu o dan adran, b?m vào?ng d?ng Windows Forms (.NET).
Chú ý Trong Visual Studio 2005, nh?p vàoVisual c ++ dư?i Các lo?i d? án. Dư?i Khuôn mẫu, b?m?ng d?ng Windows Forms.
Lo?i KB307398 trong cácTên h?p, lo?i C: \ trong cácV? trí h?p, và sau đó nh?p vàoOk.
M? d?ng Form1 trong thi?t k?
xem và sau đó nh?n F4 đ? m? các
Propertries c?a s?.
Trong c?a s? thu?c tính, m? r?ng các Kích thướcthư m?c. Trong các Chi?u r?ng h?p, lo?i 700.
Trong các Chi?u cao h?p, lo?i320.
Đ? th?c hi?n t?p tin đ?u vào đ?u ra ho?t đ?ng, thêm các System::Iokhông gian tên.
B?m phím SHIFT + F7 đ? m? Form1 trong
Thi?t k? c?nh. B?m đúp vào các Đ?c File văn b?n nút, và sau đó dán m? sau đây:
Chú ý Visual C++ 2005, nh?p vào Trình thiết kế trong các Xem tr?nh đơn.
// How to read a text file:
// Use try...catch to deal with a 0 byte file or a non-existant file.
listBox1->Items->Clear();
try
{
String* textFile = String::Concat(windir, (S"\\mytest.txt"));
StreamReader *reader=new StreamReader(textFile);
do
{
listBox1->Items->Add(reader->ReadLine());
}
while(reader->Peek() != -1);
}
catch(FileNotFoundException *ex)
{
listBox1->Items->Add(ex);
}
catch (System::Exception *e)
{
listBox1->Items->Add(e);
}
Trong giao di?n thi?t k? Form1, b?m đúp vào các Vi?t t?p tin văn b?n nút, và sau đó dán m? sau đây:
// This demonstrates how to create and to write to a text file.
StreamWriter* pwriter = new StreamWriter(S"c:\\KBTest.txt");
pwriter->WriteLine(S"The file was created by using the StreamWriter class.");
pwriter->Close();
listBox1->Items->Clear();
String *filew = new String(S"File written to C:\\KBTest.txt");
listBox1->Items->Add(filew);
Trong giao di?n thi?t k? Form1, b?m đúp vào các Xem chi tieát taäp tin nút, và sau đó dán m? sau trong phương pháp:
// This code retrieves file properties. The example uses Notepad.exe.
listBox1->Items->Clear();
String* testfile = String::Concat(windir, (S"\\notepad.exe"));
FileInfo *pFileProps =new FileInfo(testfile);
listBox1->Items->Add(String::Concat(S"File Name = ", (pFileProps->get_FullName() )) );
listBox1->Items->Add(String::Concat(S"Creation Time = ", (pFileProps->get_CreationTime() ).ToString()) );
listBox1->Items->Add(String::Concat(S"Last Access Time = " ,(pFileProps->get_LastAccessTime() ).ToString()) );
listBox1->Items->Add(String::Concat(S"Last Write Time = ", (pFileProps->get_LastWriteTime() ).ToString()) );
listBox1->Items->Add(String::Concat(S"Size = ", (pFileProps->get_Length() ).ToString()) );
Trong giao di?n thi?t k? Form1, b?m đúp vào các Danh sách ? đ?a nút, và sau đó dán m? sau đây:
// This demonstrates how to obtain a list of disk drives.
listBox1->Items->Clear();
String* drives[] = Directory::GetLogicalDrives();
int numDrives = drives->get_Length();
for (int i=0; i<numDrives; i++)
{
listBox1->Items->Add(drives[i]);
}
Trong giao di?n thi?t k? Form1, b?m đúp vào các Danh sách c?p con nút, và sau đó dán m? sau đây:
// This code obtains a list of folders. This example uses the Windows folder.
listBox1->Items->Clear();
String* dirs[] = Directory::GetDirectories(windir);
int numDirs = dirs->get_Length();
for (int i=0; i<numDirs; i++)
{
listBox1->Items->Add(dirs[i]);
}
Trong giao di?n thi?t k? Form1, b?m đúp vào các Danh sách t?p tin nút, và sau đó dán m? sau đây:
// This code obtains a list of files. This example uses the Windows folder.
listBox1->Items->Clear();
String* files[]= Directory::GetFiles(this->windir);
int numFiles = files->get_Length();
for (int i=0; i<numFiles; i++)
{
listBox1->Items->Add(files[i]);
}
Xây d?ng và sau đó ch?y chương tr?nh, b?m CTRL + F5.
Hoàn thành m?u m?
Chú ý B?n ph?i thay đ?i m? sau khi ch?y trong Visual C++ 2005.
//Form1.h
#pragma once
namespace KB307398
{
using namespace System;
using namespace System::IO;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
private:
String *windir;
public:
Form1(void)
{
windir = System::Environment::GetEnvironmentVariable("windir");
InitializeComponent();
}
protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * button1;
private: System::Windows::Forms::Button * button2;
private: System::Windows::Forms::Button * button3;
private: System::Windows::Forms::Button * button4;
private: System::Windows::Forms::Button * button5;
private: System::Windows::Forms::Button * button6;
private: System::Windows::Forms::ListBox * listBox1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->button2 = new System::Windows::Forms::Button();
this->button3 = new System::Windows::Forms::Button();
this->button4 = new System::Windows::Forms::Button();
this->button5 = new System::Windows::Forms::Button();
this->button6 = new System::Windows::Forms::Button();
this->listBox1 = new System::Windows::Forms::ListBox();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(500, 32);
this->button1->Name = S"button1";
this->button1->Size = System::Drawing::Size(112, 23);
this->button1->TabIndex = 1;
this->button1->Text = S"Read Text File";
this->button1->Click += new System::EventHandler(this, button1_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(500, 64);
this->button2->Name = S"button2";
this->button2->Size = System::Drawing::Size(112, 23);
this->button2->TabIndex = 2;
this->button2->Text = S"Write Text File";
this->button2->Click += new System::EventHandler(this, button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(500, 96);
this->button3->Name = S"button3";
this->button3->Size = System::Drawing::Size(112, 23);
this->button3->TabIndex = 3;
this->button3->Text = S"View File Information";
this->button3->Click += new System::EventHandler(this, button3_Click);
//
// button4
//
this->button4->Location = System::Drawing::Point(500, 128);
this->button4->Name = S"button4";
this->button4->Size = System::Drawing::Size(112, 23);
this->button4->TabIndex = 4;
this->button4->Text = S"List Drives";
this->button4->Click += new System::EventHandler(this, button4_Click);
//
// button5
//
this->button5->Location = System::Drawing::Point(500, 160);
this->button5->Name = S"button5";
this->button5->Size = System::Drawing::Size(112, 23);
this->button5->TabIndex = 5;
this->button5->Text = S"List Subfolders";
this->button5->Click += new System::EventHandler(this, button5_Click);
//
// button6
//
this->button6->Location = System::Drawing::Point(500, 188);
this->button6->Name = S"button6";
this->button6->Size = System::Drawing::Size(112, 23);
this->button6->TabIndex = 6;
this->button6->Text = S"List Files";
this->button6->Click += new System::EventHandler(this, button6_Click);
//
// listBox1
//
this->listBox1->Location = System::Drawing::Point(24, 24);
this->listBox1->Name = S"listBox1";
this->listBox1->Size = System::Drawing::Size(450, 199);
this->listBox1->TabIndex = 0;
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(692, 293);
this->Controls->Add(this->listBox1);
this->Controls->Add(this->button6);
this->Controls->Add(this->button5);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);
}
private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)
{// This code shows how to read a text file.
// The try...catch code is to deal with a 0 byte file or a non-existant file.
listBox1->Items->Clear();
try
{
String* textFile = String::Concat(windir, (S"\\mytest.txt"));
StreamReader *reader=new StreamReader(textFile);
do
{
listBox1->Items->Add(reader->ReadLine());
}
while(reader->Peek() != -1);
}
catch(FileNotFoundException *ex)
{
listBox1->Items->Add(ex);
}
catch (System::Exception *e)
{
listBox1->Items->Add(e);
}
}
private: System::Void button2_Click(System::Object * sender, System::EventArgs * e)
{// This code demonstrates how to create and to write to a text file.
StreamWriter* pwriter = new StreamWriter(S"c:\\KBTest.txt");
pwriter->WriteLine(S"The file was created by using the StreamWriter class.");
pwriter->Close();
listBox1->Items->Clear();
String *filew = new String(S"The file was written to C:\\KBTest.txt");
listBox1->Items->Add(filew);
}
private: System::Void button3_Click(System::Object * sender, System::EventArgs * e)
{// This code retrieves file properties. This example uses Notepad.exe.
listBox1->Items->Clear();
String* testfile = String::Concat(windir, (S"\\notepad.exe"));
FileInfo *pFileProps =new FileInfo(testfile);
listBox1->Items->Add(String::Concat(S"File Name = ", (pFileProps->get_FullName() )) );
listBox1->Items->Add(String::Concat(S"Creation Time = ", (pFileProps->get_CreationTime() ).ToString()) );
listBox1->Items->Add(String::Concat(S"Last Access Time = " ,(pFileProps->get_LastAccessTime() ).ToString()) );
listBox1->Items->Add(String::Concat(S"Last Write Time = ", (pFileProps->get_LastWriteTime() ).ToString()) );
listBox1->Items->Add(String::Concat(S"Size = ", (pFileProps->get_Length() ).ToString()) );
}
private: System::Void button4_Click(System::Object * sender, System::EventArgs * e)
{// The code demonstrates how to obtain a list of disk drives.
listBox1->Items->Clear();
String* drives[] = Directory::GetLogicalDrives();
int numDrives = drives->get_Length();
for (int i=0; i<numDrives; i++)
{
listBox1->Items->Add(drives[i]);
}
}
private: System::Void button5_Click(System::Object * sender, System::EventArgs * e)
{// This code obtains a list of folders. This example uses the Windows folder.
listBox1->Items->Clear();
String* dirs[] = Directory::GetDirectories(windir);
int numDirs = dirs->get_Length();
for (int i=0; i<numDirs; i++)
{
listBox1->Items->Add(dirs[i]);
}
}
private: System::Void button6_Click(System::Object * sender, System::EventArgs * e)
{// This code obtains a list of files. This example uses the Windows folder.
listBox1->Items->Clear();
String* files[]= Directory::GetFiles(this->windir);
int numFiles = files->get_Length();
for (int i=0; i<numFiles; i++)
{
listBox1->Items->Add(files[i]);
}
}
};
}
//Form1.cpp
#include "stdafx.h"
#include "Form1.h"
#include <windows.h>
using namespace KB307398;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}
Cho bi?t thêm thông tin v? làm th? nào đ? t?o ra các h?nh th?c Windows trong qu?n l?
Ph?n m? r?ng c?a c + +, xem m?u "ManagedCWinFormWiz" Visual Studio.NET
Tr? giúp.
QUAN TRỌNG: Bài vi?t này đư?c d?ch b?ng ph?n m?m d?ch máy c?a Microsoft ch? không ph?i do con ngư?i d?ch. Microsoft cung c?p các bài vi?t do con ngư?i d?ch và c? các bài vi?t do máy d?ch đ? b?n có th? truy c?p vào t?t c? các bài vi?t trong Cơ s? Ki?n th?c c?a chúng tôi b?ng ngôn ng? c?a b?n. Tuy nhiên, bài vi?t do máy d?ch không ph?i lúc nào c?ng hoàn h?o. Lo?i bài vi?t này có th? ch?a các sai sót v? t? v?ng, cú pháp ho?c ng? pháp, gi?ng như m?t ngư?i nư?c ngoài có th? m?c sai sót khi nói ngôn ng? c?a b?n. Microsoft không ch?u trách nhi?m v? b?t k? s? thi?u chính xác, sai sót ho?c thi?t h?i nào do vi?c d?ch sai n?i dung ho?c do ho?t đ?ng s? d?ng c?a khách hàng gây ra. Microsoft c?ng thư?ng xuyên c?p nh?t ph?n m?m d?ch máy này.
Nh?p chu?t vào đây đ? xem b?n ti?ng Anh c?a bài vi?t này:307398