????? C++ ?? ????? ???? recursively ??? ??????? ?? ??? ???? ????

???? ?????? ???? ??????
???? ID: 307009 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

?? ??? ?? ??? ???? ????? ???? ?? ?? ???? ??????? ?? ??? recursively ??? ?????????, ?? ??? ??? ??? ???????, ?? ????? ?? ?? ??? ?????? ???? ?? ????? ?? ?????????? recursion ?? ??? ??? ???? ???? ??? ?? ???? ??????? ?????? ?? ??? ???? ???? ??????? ?? ??? ?? ???? ??? ???? ???? ??? ???????? ????????? ?? ???? ??? Each part of the code in this article is explained, and a working code sample is also provided.

Directory recursion is a common I/O task for developers. TheFileSystemObjectobject makes this task easy for Component Object Model (COM) programs, but this task is easier in Visual C++ .NET 2002. Similar to theFileSystemObjectobject, the classes in theSystem.IOnamespace provide an object-oriented way to access files and folders.

??????????

????? ???? outlines ???????? ?????????, ??????????, ??????? ??????, ?? ???? ???? ???:
  • Microsoft Visual C++ .NET or Microsoft Visual C++ 2005

Enable Directory Recursion

The file and directory manipulation classes reside in theSystem::IO??? ????? ??? Before you work with these classes, add the following code:
using <mscorlib.dll>
using namespace System;
using namespace System::IO;
TheSystem::IOnamespace provides many options for working with files and with folders. TheSystem::IOnamespace not only provides classes that you can create an instance of, but it also provides file and directory utility classes. These classes contain static methods that you can call without having to declare a variable of that type. ?????? ?? ???, ?? ????? ?? ???? ?????????????class to obtain the subfolders of a folder.

The following code uses the staticGetDirectories?? ??? ??????????????class to return an array of strings. This array contains folder paths to the subfolders of the C:\ folder, if any exist:
String* directories[] = Directory::GetDirectories("C:\\");
				
The??????????class also contains theGetFiles?????? ??? ?? ????? ?? ???? ???GetFilesto retrieve a string array of files that match a certain criteria. The following code sample uses theGetFilesmethod to retrieve all files in the C:\ folder with a .dll file name extension:
String* files[] = Directory::GetFiles("C:\\", "*.dll");
				
?? ????? ?? ???? ???GetDirectories???? ??GetFiles?? ??? ??????????????class to recursively search for files that match the search string. Use the following method to perform the recursion:
	 void DirSearch(String* sDir)
	 {
		 try
		 {
			 // Find the subfolders in the folder that is passed in.
			 String* d[] = Directory::GetDirectories(sDir);
			 int numDirs = d->get_Length();
			 
			 for (int i=0; i < numDirs; i++)
			 {
				 // Find all the files in the subfolder.
				 String* f[] = Directory::GetFiles(d[i],textBox1->Text);
				 int numFiles = f->get_Length();

				 for (int j=0; j < numFiles; j++)
				 {
					 listBox1->Items->Add(f[j]);
				 }
				 DirSearch(d[i]);
			 }
		 }
		 catch (System::Exception* e)
		 {
			 MessageBox::Show(e->Message);
		 }
	 }
The previous code passes a string to theDirSearch?????? ?? ??? ???? ???.. ?? ???????? ??? ?? ???? ?? ????? ????? ??? ?? ??????? ?? ???? ?? ??? ?? ????? ?? ???? ???GetDirectories???? ????????? ??? ??? ??? ?? ??????? ?? ??????????? ?? ??????? ???? ?? ??? ?????? ??? ???????GetDirectories?????? ?? ?? ???? ???? ??, ?? ????? ?? ???? ??? ????? ?????? ?? ???????? ???????? iterate ???? ?? ??? ??? ???????? ????????? ?? ??? ????? ????GetFiles?? ??????? ??? ??????? ?? iterate ???? ?? ??? ?????? ??? ??? ????? ?? ???? ??????? ?? ??? ?? ??? ???? ??? ??GetFiles?????? ??? ??? ????? ??? ??? ???????? ?? ?? ?????? ??????? ??GetFiles???? ???? ??? ????? ??? ?????? ?? ??? ???? ??, ?? ?? ???? ???? ????? ??? ????? ??? ??? ???????? ???????? ????? ??, ?? ??? ???DirSearch??? ?? ??? ????, ?? ???? ??? ?? ???????? ???? ???? ?????? ????????? ??? ??????? ?? ??? ?????????? ?? ????? ?? ??? ?? ?? recursive ??? ?? ????? ?? ?????

????? ?????

  1. Microsoft Visual Studio .NET 2003 ?? Microsoft Visual Studio 2005 ??????? ?????
  2. ????? ???????????? ??,????? ????-????? ????, ?? ???? ????????????.
  3. ?? ???????????????? ??????????? ????,????? C++ .NET.

    ???:Microsoft Visual C++ 2005, ???????? C++ .NET???? ??? ??????? C++.
  4. ?? ???????????????????? ????,Windows ??????? ????????? (.NET).
  5. ????????????? ???,RecursiveSearchCPP. ???????????????? ???,C:\?? ????-????? ????, ?? ???? ???OK.
  6. ?????Form1??????? ?????? ??? ?????, ?? ?? ??? ????? ????? ?? ??? F4 ??????
  7. ??? ????? ??? ??????? ???????????????. ???????????????, ??????700. ??????? ??????????, ??????320.
  8. ?? ??????????????????, ????? ?????????????, ??ComboBox????????, ??ListBox???????? ?? ?????Form1 ??????? ????????? ?????
  9. ??? ????? ???, ??? ??????????,????,TabIndex, ???????? follows ?? ??? ???:
    ?? ?????? ?? ??????? ?????? ?????? ?? ??????? ????
    ???????? ID?????????TabIndex???
    label18, 16144, 165??? ??? ??????? ?? ???:
    textBox18, 40120, 204*.dll
    label28, 96120, 253????? ?????:
    Button1608, 24875, 250& ?????
    listBox1152, 8530, 2251
    comboBox18, 128120, 252C:\\
  10. ?????InitializeComponent?????? ????? ?? ??? ?????? ??? ??? ??????? ???????? ?? ?? ?? ???? ???????? ?? ?????? enlist ???? ?? ??? ????? ??? ??????:
    // Enlist all the network drives that are available on the computer.
    String* p_logicalDrives[] = __gc new String*[];
    comboBox1->Items->Clear();
    p_logicalDrives = Directory::GetLogicalDrives();
    int numDrives = p_logicalDrives->get_Length();
    
    for (int i=0; i< numDrives; i++)
    {
    	comboBox1->Items->Add(p_logicalDrives[i]);
    }
  11. ?? ???-????? ??????????, ?? ???? ??? ?????? ??? ????? ??? ???????:
    // Clear the list box.
    listBox1->Items->Clear();
    // You do not have the option to change the values of the files to be searched
    // until the search is completed. Therefore, the following value is false.
    textBox1->Enabled = false;
    comboBox1->Enabled = false;
    
    button1->Text = S"Searching...";
    this->Cursor = Cursors::WaitCursor;
    Application::DoEvents();
    
    // Callthe recursive search method.
    DirSearch(comboBox1->Text);
    button1->Text = S"Search";
    this->Cursor = Cursors::Default;
    
    // After the search is completed, the search criteria is enabled 
    //and you can add other search criteria to search the files recursively.
    textBox1->Enabled = true;
    comboBox1->Enabled = true;
    
  12. ????????? ?????, ????? ?? ??? CTRL + F5 ??????
  13. ????????? ??????

????? ??? ?????

// Form1.h
#pragma once

namespace RecursiveSearchCPP
{
	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;

	/// <summary> 
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you must change the 
	///          Resource File Name property for the managed resource compiler tool 
	///          that is associated with all .resx files that this class depends on.  Otherwise,
	///          the designers will not be able to interact correctly with localized
	///          resources that are associated with this form.
	/// </summary>
	public __gc class Form1 : public System::Windows::Forms::Form
	{	
	public:
		Form1(void)
		{
			InitializeComponent();
		}
  
	protected:
		void Dispose(Boolean disposing)
		{
			// Form::Dispose(Disposing);
			if (disposing && components)
			{
				components->Dispose();
			}
			__super::Dispose(disposing);
		}
	private: System::Windows::Forms::Label *  label1;
	private: System::Windows::Forms::TextBox *  textBox1;
	private: System::Windows::Forms::Label *  label2;
	private: System::Windows::Forms::Button *  button1;
	private: System::Windows::Forms::ListBox *  listBox1;
	private: System::Windows::Forms::ComboBox *  comboBox1;

	private:
		/// <summary>
		/// This is the required designer variable.
		/// </summary>
		System::ComponentModel::Container * components;

		/// <summary>
		/// This is the required method for Designer support. Do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->label1 = new System::Windows::Forms::Label();
			this->textBox1 = new System::Windows::Forms::TextBox();
			this->label2 = new System::Windows::Forms::Label();
			this->button1 = new System::Windows::Forms::Button();
			this->listBox1 = new System::Windows::Forms::ListBox();
			this->comboBox1 = new System::Windows::Forms::ComboBox();
			this->SuspendLayout();
			// 
			// label1
			// 
			this->label1->Location = System::Drawing::Point(8, 16);
			this->label1->Name = S"label1";
			this->label1->Size = System::Drawing::Size(144, 16);
			this->label1->TabIndex = 5;
			this->label1->Text = S"Search for files containing:";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(8, 40);
			this->textBox1->Name = S"textBox1";
			this->textBox1->Size = System::Drawing::Size(120, 20);
			this->textBox1->TabIndex = 4;
			this->textBox1->Text = S"*.dll";
			// 
			// label2
			// 
			this->label2->Location = System::Drawing::Point(8, 96);
			this->label2->Name = S"label2";
			this->label2->Size = System::Drawing::Size(120, 23);
			this->label2->TabIndex = 3;
			this->label2->Text = S"Look in:";
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(608, 248);
			this->button1->Name = S"button1";
			this->button1->TabIndex = 0;
			this->button1->Text = S"&Search";
			this->button1->Click += new System::EventHandler(this, button1_Click);
			// 
			// listBox1
			// 
			this->listBox1->Location = System::Drawing::Point(152, 8);
			this->listBox1->Name = S"listBox1";
			this->listBox1->Size = System::Drawing::Size(530, 225);
			this->listBox1->TabIndex = 1;
			// 
			// comboBox1
			//
			// Enlist all the network drives that are available on the computer.
			//
			String* p_logicalDrives[] = __gc new String*[];
			comboBox1->Items->Clear();
			p_logicalDrives = Directory::GetLogicalDrives();
	        int numDrives = p_logicalDrives->get_Length();
			for (int i=0; i< numDrives; i++)
			{
				comboBox1->Items->Add(p_logicalDrives[i]);
			}
			this->comboBox1->Location = System::Drawing::Point(8, 128);
			this->comboBox1->Name = S"comboBox1";
			this->comboBox1->Size = System::Drawing::Size(120, 25);
			this->comboBox1->TabIndex = 2;
			this->comboBox1->Text = S"C:\\";

			// 
			// Form1
			// 
			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
			this->ClientSize = System::Drawing::Size(688, 286);
			this->Controls->Add(this->comboBox1);
			this->Controls->Add(this->listBox1);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->label1);
			this->Name = S"Form1";
			this->Text = S"Form1";
			this->ResumeLayout(false);

		}

	private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
	 {
  		//Clears the list box		
  		listBox1->Items->Clear();
  		// You do not have option to change the values of the files to be searched
  		// until the search is completed. Therefore, the value of the following is false.
 	 	textBox1->Enabled = false;
  		comboBox1->Enabled = false;

 	 	button1->Text = S"Searching...";
 		 this->Cursor = Cursors::WaitCursor;
  		Application::DoEvents();

  		// Call the recursive search method.
  		DirSearch(comboBox1->Text);
  		button1->Text = S"Search";
  		this->Cursor = Cursors::Default;
  
  		// After the search is completed, the search criteria is enabled 
  		// and you can add other search criteria to search the files recursively.
  		textBox1->Enabled = true;
  		comboBox1->Enabled = true;
	 }

	 void DirSearch(String* sDir)
	 {
		 try
		 {
			 // Find the subfolders in the folder that is passed in.
			 String* d[] = Directory::GetDirectories(sDir);
			 int numDirs = d->get_Length();
			 
			 for (int i=0; i < numDirs; i++)
			 {
				 // Find all the files in the subfolder.
				 String* f[] = Directory::GetFiles(d[i],textBox1->Text);
				 int numFiles = f->get_Length();

				 for (int j=0; j < numFiles; j++)
				 {
					 listBox1->Items->Add(f[j]);
				 }
				 DirSearch(d[i]);
			 }
		 }
		 catch (System::Exception* e)
		 {
			 System::Console::WriteLine(e->Message);
		 }
	 }
};
}

//Form1.cpp
#include "stdafx.h"
#include "Form1.h"
#include <windows.h>

using namespace RecursiveSearchCPP;

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;
}

??????

C++ .NET 2002 ????? ?? ??? Windows ?? ??? ??? ???????? ????????? ????? ?? ??? ???? ???? ?? ???? ??? ???? ??????? ?? ??? Visual Studio .NET ??? ??? "ManagedCWinFormWiz" ????? ?? ??????

???

???? ID: 307009 - ????? ???????: 04 ?????? 2010 - ??????: 2.0
???? ???? ???? ??:
  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET 2003 Standard Edition
??????: 
kbhowtomaster kbio kbmt KB307009 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:307009

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com