For a Managed Extensions for C++ version of this article, see 307398
(http://support.microsoft.com/kb/307398/
)
.
For a Microsoft Visual C# .NET version of this article, see 304430
(http://support.microsoft.com/kb/304430/
)
.
For a Microsoft Visual Basic .NET version of this article, see 304427
(http://support.microsoft.com/kb/304427/
)
.
This article refers to the following Microsoft .NET Framework Class Library namespaces:
System::ComponentModel
System::Windows::Forms
System::Drawing
INTRODUCTION
This step-by-step article describes how to do six basic file I/O operations in Microsoft Visual C++ 2005 or in Microsoft Visual C++ 2008. This article uses the Ecma C++/CLI syntax instead of the Managed Extensions for C++ syntax that Microsoft Knowledge Base article 307398 describes.
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
307398
(http://support.microsoft.com/kb/307398/
)
How to do basic file I/O in Visual C++ 2005 or in Visual C++ .NET
The object model for file operations in the Microsoft .NET Framework resembles the FileSystemObject (FSO) that is popular with many Microsoft Visual Studio 6.0 developers. The functionality that this article describes is based on the following Microsoft Knowledge Base article:
186118
(http://support.microsoft.com/kb/186118/
)
How to use FileSystemObject with
Visual Basic
You can still use the FSO in the .NET Framework. Because the FSO is a COM component, the .NET Framework requires that access to the object be through the Interop layer. The .NET Framework generates a wrapper for the component for you if you want to use the component. However, the File class, the FileInfo class, the Directory class, the DirectoryInfo class, and other related classes in the .NET Framework offer functionality that is not available through the FSO without the overhead of the Interop layer.
The examples in this article describe basic file I/O operations.
The "Step-by-step example" section describes how to create a sample program
that demonstrates the following six file I/O operations:
The following sample code uses a StreamReader class to read a text file. The contents of the file are added to a ListBox control. The try...catch block is used to warn the program if the file is empty. There are many ways to determine when the end of the file is reached. This sample uses the Peek method to examine the next line before it reads the next line.
This sample code uses the Directory class and the Drive class to list the logical drives on a system. For this sample, the results appear in a ListBox control.
Many things can go wrong when a user gains access to files. The files may not exist, the files may be in use, or users may not have rights on the files in the folders that they are trying to access. Consider these possibilities when you write code to handle the exceptions that may be generated.
Start Microsoft Visual Studio 2005 or Microsoft Visual C++ 2008.
On the File menu, point to
New, and then click Project.
Under Project Types, click
Visual C++ Projects. Under Templates, click
Windows Forms Application.
Note If your default programming language is not Visual C++, you may see Visual C++ Projects under Other Languages.
Type KB950617 in the Name box, type C:\ in the Location box, and then click OK.
Open the Form1 form in Design view, and then press F4 to open the Properties window.
In the Properties window, expand the Size property folder. In the Width box, type 700. In the Height box, type 320.
Add one ListBox control and six
Button controls to Form1.
Note To view the toolbox, click Toolbox on the View menu.
In the Properties window, change the
Location property, the Name property, the Size property, the
TabIndex property, and the Text property of these
controls as follows:
Collapse this tableExpand this table
Control ID
Location
Name
Size
TabIndex
Text
button1
500, 32
button1
112,
23
1
Read Text File
button2
500, 64
button2
112,
23
2
Write Text File
button3
500, 96
button3
112,
23
3
View File Information
button4
500, 128
button4
112,
23
4
List Drives
button5
500, 160
button5
112,
23
5
List Subfolders
button6
500, 192
button6
112,
23
6
List Files
listBox1
24, 24
listBox1
450,
200
0
Open the Form1.h file. In the Form1 class declaration,
declare one private String variable by using the following code:
private: String^ windir;
In the Form1 class constructor, add the following code:
To perform file I/O operations, add the System::IO
namespace.
In Solution Explorer, select Form1.h, and then click Designer in the View menu to open Form1 in Design view. Double-click the Read Text File button, and then paste the following code in the button click handler:
// How to read a text file:
// Use try...catch to deal with a 0-byte file or a nonexistent file.
listBox1->Items->Clear();
try {
StreamReader^ reader = gcnew StreamReader("c:\\mytest.txt");
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);
}
In Design view in Form1, double-click the Write Text File button, and then paste the following code in the button click handler:
// This shows you how to create and to write to a text file.
StreamWriter^ pwriter = gcnew StreamWriter("c:\\KBTest.txt");
pwriter->WriteLine("The file was created by using the StreamWriter class.");
pwriter->Close();
listBox1->Items->Clear();
String^ filew = gcnew String("File written to c:\\KBTest.txt");
listBox1->Items->Add(filew);
In Design view in Form1, double-click the View File Information button, and then paste the following code in the method:
// This code retrieves file properties. The example uses Notepad.exe.
listBox1->Items->Clear();
String^ testfile = String::Concat(windir,"\\notepad.exe");
FileInfo^ pFileProps = gcnew FileInfo(testfile);
listBox1->Items->Add(String::Concat("File Name = ", pFileProps->FullName));
listBox1->Items->Add(String::Concat("Creation Time = ", pFileProps->CreationTime.ToString()));
listBox1->Items->Add(String::Concat("Last Access Time = ", pFileProps->LastAccessTime.ToString()));
listBox1->Items->Add(String::Concat("Last Write Time = ", pFileProps->LastWriteTime.ToString()));
listBox1->Items->Add(String::Concat("Size = ", pFileProps->Length.ToString()));
In Design view in Form1, double-click the List Drives button, and then paste the following code:
// This shows you how to obtain a list of disk drivers.
listBox1->Items->Clear();
cli::array<String^>^ drives = Directory::GetLogicalDrives();
int numDrives = drives->Length;
for(int i=0; i<numDrives; i++)
{
listBox1->Items->Add(drives[i]);
}
In Design view in Form1, double-click the List Subfolders button, and then paste the following code:
// This code obtains a list of folders. This example uses the Windows folder.
listBox1->Items->Clear();
cli::array<String^>^ dirs = Directory::GetDirectories(windir);
int numDirs = dirs->Length;
for(int i=0; i<numDirs; i++)
{
listBox1->Items->Add(dirs[i]);
}
In Design view in Form1, double-click the List Files button, and then paste the following code:
// This code obtains a list of files. This example uses the Windows folder.
listBox1->Items->Clear();
cli::array<String^>^ files = Directory::GetFiles(windir);
int numFiles = files->Length;
for(int i=0; i<numFiles; i++)
{
listBox1->Items->Add(files[i]);
}
To build and then to run the program, press CTRL+F5.