This article was previously published under Q307398
For a Microsoft Visual C++ .NET 2005/2008 ECMA C++/CLI version of this article, see 950617
(http://support.microsoft.com/kb/950617/
)
.
For a Microsoft Visual C# .NET version of this
article, see
304430
(http://support.microsoft.com/kb/304430/EN-US/
)
. For a Microsoft Visual
Basic .NET version of this article, see
304427
(http://support.microsoft.com/kb/304427/EN-US/
)
. This article refers to
the following Microsoft .NET Framework Class Library namespaces:
This step-by-step article describes how to do six basic
file input/output (I/O) operations in Microsoft Visual C++ 2005 or in Microsoft Visual C++ .NET. If you are new to the .NET Framework,
you will find that the object model for file operations in the .NET Framework is similar to
the FileSystemObject (FSO) that is popular with many Microsoft Visual Studio 6.0 developers. To
make the transition easier, the functionality that is demonstrated in this
article 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 FileSystemObject in the .NET Framework. Because the FileSystemObject is a Component Object Model (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 it. However, the File class, the FileInfo class, the Directory, DirectoryInfo classes, and other related classes in the .NET Framework, offer
functionality that is not available with 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 alert 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 reading it.
Note In
Visual C++ 2005, you must add the common language runtime support compiler option (/clr:oldSyntax) to successfully compile the previous code sample as Managed C++.
To add the common language runtime support compiler option, follow these steps:
Click Project, and then click
ProjectName Properties.
Note ProjectName is a placeholder for the
name of the project.
Expand Configuration Properties, and then click
General.
In the right pane, click to select Common Language Runtime Support, Old Syntax
(/clr:oldSyntax) in the
Common Language Runtime support project settings.
Click
Apply, and then
click OK.
For more information about common language runtime support compiler options, visit the following Microsoft
Developer Network (MSDN) Web site:
This sample code uses a StreamWriter class to create and write to a file. If you have an existing
file, you can open it in the same way.
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);
This sample code uses the Directory and Drive classes to list the logical drives on a system. For this sample,
the results appear in a ListBox control.
listBox1->Items->Clear();
String* drives[] = Directory::GetLogicalDrives();
int numDrives = drives->get_Length();
for (int i=0; i<numDrives; i++)
{
listBox1->Items->Add(drives[i]);
}
This sample code uses the GetFiles method of the Directory class to obtain a listing of files.
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]);
}
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 of folders that they are trying to access. Consider
these possibilities when you write code to handle the exceptions that may
be generated.
To perform file Input output operations,add the System::IO
namespace.
Press SHIFT+F7 to open Form1 in
Design view. Double-click the Read Text File button, and then paste the following code:
Note In Visual C++ 2005, click Designer in the View menu.
// 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);
}
In the Form1 Design view, double-click the Write Text File button, and then paste the following code:
// 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);
In the Form1 Design view, 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, (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()) );
In the Form1 Design view, double-click the List Drives button, and then paste the following code:
// 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]);
}
In the Form1 Design view, 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();
String* dirs[] = Directory::GetDirectories(windir);
int numDirs = dirs->get_Length();
for (int i=0; i<numDirs; i++)
{
listBox1->Items->Add(dirs[i]);
}
In the Form1 Design view, 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();
String* files[]= Directory::GetFiles(this->windir);
int numFiles = files->get_Length();
for (int i=0; i<numFiles; i++)
{
listBox1->Items->Add(files[i]);
}