This article refers to the following Microsoft .NET Framework Class Library namespaces:
- System::ComponentModel
- System::Windows::Forms
- System::Drawing
Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.
listBox1->Items->Clear();
try {
String^ textFile = String::Concat(windir,"\\mytest.txt");
StreamReader^ reader = gcnew 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);
}
StreamWriter^ pwriter = gcnew StreamWriter("C:\\KBTest.txt");
pwriter->WriteLine("File created using StreamWriter class.");
pwriter->Close();
listBox1->Items->Clear();
String ^filew = gcnew String("File Written to C:\\KBTest.txt");
listBox1->Items->Add(filew);
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()));
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]);
}
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]);
}
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]);
}
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 |
private: String^ windir;
windir = System::Environment::GetEnvironmentVariable("windir");
// 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);
}
// 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);
// 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()));
// 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]);
}
// 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]);
}
// 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]);
}
// Form1.h
#pragma once
namespace KB950617
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::IO;
public ref class Form1 : public System::Windows::Forms::Form
{
private:
String^ windir;
public:
Form1(void)
{
InitializeComponent();
windir = System::Environment::GetEnvironmentVariable("windir");
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::ListBox^ listBox1;
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::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->listBox1 = (gcnew System::Windows::Forms::ListBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
// listBox1
this->listBox1->FormattingEnabled = true;
this->listBox1->Location = System::Drawing::Point(24, 24);
this->listBox1->Name = L"listBox1";
this->listBox1->Size = System::Drawing::Size(450, 199);
this->listBox1->TabIndex = 0;
// button1
this->button1->Location = System::Drawing::Point(500, 32);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(112, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"Read Text File";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
// button2
this->button2->Location = System::Drawing::Point(500, 64);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(112, 23);
this->button2->TabIndex = 2;
this->button2->Text = L"Write Text File";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
// button3
this->button3->Location = System::Drawing::Point(500, 96);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(112, 23);
this->button3->TabIndex = 3;
this->button3->Text = L"View File Information";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
// button4
this->button4->Location = System::Drawing::Point(500, 128);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(112, 23);
this->button4->TabIndex = 4;
this->button4->Text = L"List Drives";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
// button5
this->button5->Location = System::Drawing::Point(500, 160);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(112, 23);
this->button5->TabIndex = 5;
this->button5->Text = L"List Subfolders";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
// button6
this->button6->Location = System::Drawing::Point(500, 192);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(112, 23);
this->button6->TabIndex = 6;
this->button6->Text = L"List Files";
this->button6->UseVisualStyleBackColor = true;
this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click);
// Form1
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(692, 286);
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->Controls->Add(this->listBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
// How to read a text file:
// Use try...catch to deal with a 0-byte file or a non-existent 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);
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
// This demonstrates 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);
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
// 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()));
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
// This demonstrates 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]);
}
}
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();
cli::array<String^>^ dirs = Directory::GetDirectories(windir);
int numDirs = dirs->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();
cli::array<String^>^ files = Directory::GetFiles(windir);
int numFiles = files->Length;
for(int i=0; i<numFiles; i++)
{
listBox1->Items->Add(files[i]);
}
}
};
}
// KB950617.cpp
#include "stdafx.h"
#include "Form1.h"
using namespace KB950617;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enable Windows XP visual effects before any controls are created.
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run the program.
Application::Run(gcnew Form1());
return 0;
}
Keywords: kbwindowsforms, kbio, kbforms, kbnewsgrouplink, kbentirenet, kbdsupport, kbfileio, kbinfo, kb