TIPS/Programming

Visual C++ 2008逕ィ 謖螳壹ヵ繧ゥ繝ォ繝莉・荳九ョ繝輔ぃ繧、繝ォ蜀榊クー逧讀懃エ「 繧オ繝ウ繝励Ν繝励Ο繧ー繝ゥ繝

讎りヲ

縺薙ョ繝励Ο繧ー繝ゥ繝縺ッ縲∵欠螳壹ヵ繧ゥ繝ォ繝莉・荳九ョ繝輔ぃ繧、繝ォ繧貞榊クー逧縺ォ讀懃エ「縺吶k繧オ繝ウ繝励Ν繝励Ο繧ー繝ゥ繝縺ァ縺吶

荳玖ィ倥ョ迺ー蠅縺ァ縺ョ髢狗匱縺ョ蜿り縺ォ縺吶k縺薙→縺悟コ譚・縺セ縺吶

繝繧ヲ繝ウ繝ュ繝シ繝

荳玖ィ倥h繧翫し繝ウ繝励Ν繝励Ο繧ク繧ァ繧ッ繝医r繝繧ヲ繝ウ繝ュ繝シ繝牙庄閭ス縺ァ縺吶

FileSerchSample100.zip(16KB)

迺ー蠅

  • OS: WindowsXP SP3
  • PC: WindowsXP SP3縺悟虚菴懊☆繧輝C
  • 髢狗匱迺ー蠅: Microsoft Visual C++ Express Edition(.NET)

蝓コ譛ャ蜍穂ス

  • 謖螳壹&繧後◆繝輔か繝ォ繝莉・荳九ョ繝輔ぃ繧、繝ォ繧貞榊クー逧縺ォ讀懃エ「縺励∪縺吶
  • 繝輔ぃ繧、繝ォ蜷阪↓縺ッ縲√Ρ繧、繝ォ繝峨き繝シ繝峨↑縺ゥ縺ァ繝輔ぅ繝ォ繧ソ繧呈欠螳壹☆繧九%縺ィ縺悟コ譚・縺セ縺吶
  • 讀懃エ「縺輔l縺溘ヵ繧。繧、繝ォ繧偵√Μ繧ケ繝医懊ャ繧ッ繧ケ縺ォ陦ィ遉コ縺励∪縺吶
fileop20090608.jpg

謚陦

  • 繝。繧、繝ウ繝輔か繝シ繝繧医j縲√ヵ繧。繧、繝ォ繧貞榊クー逧縺ォ讀懃エ「縺吶k蜈繝輔か繝ォ繝繧呈欠螳壹〒縺阪∪縺吶
  • 讀懃エ「繝励Ο繧ー繝ゥ繝縺ッ縲∝挨繧ッ繝ゥ繧ケ縺ィ縺励※縺ゅj縺セ縺吶
  • 繧ッ繝ゥ繧ケ縺ョ繧、繝ウ繧ケ繧ソ繝ウ繧ケ繧堤函謌舌@縲髢「謨ー繧貞ョ溯。後☆繧九%縺ィ縺ァ縲ヾtring蝙九ョ繧ウ繝ャ繧ッ繧キ繝ァ繝ウ縺ォ讀懃エ「縺輔l縺溘ヵ繧。繧、繝ォ蜷阪′譬シ邏阪&繧後∪縺吶
  • 縺薙l繧峨ョ謫堺ス懊↓縺ッ縲ヾystem::IO::Directory 繧ッ繝ゥ繧ケ繧剃スソ逕ィ縺励∪縺吶

闡嶺ス懈ィゥ縺ェ縺ゥ縺ォ縺、縺縺ヲ

縺薙ョ繧オ繝ウ繝励Ν繝励Ο繧ー繝ゥ繝縺ョ菴ソ逕ィ縺ッ繝輔Μ繝シ縺ィ縺励∪縺吶′縲∝渕譛ャ逧縺ォ菴懆縺御ソ晄怏縺励※縺縺セ縺吶

蜀埼榊ク繝サ霆「霈峨↓縺、縺縺ヲ

蜀埼榊ク繝サ霆「霈峨↓縺、縺縺ヲ縺ッ莠句燕縺ォ縺皮嶌隲荳九&縺縲
縺ェ縺翫√%縺ョ險倅コ九ョ蜀螳ケ縺ォ髢「縺励※繝医Λ繝悶Ν縺檎匱逕溘@縺ヲ繧りイャ莉サ繧定イ縺縺九ュ縺セ縺吶

霆「霈峨∝埼榊ク縺ョ髫帙↓縺ッ縲∝ス難シキシ・シ「繧オ繧、繝医∈縺ョ繝ェ繝ウ繧ッ繧剃ス募ヲ縺九↓謗イ霈峨@縺ヲ縺縺溘□縺代k縺ィ縲∽ス懆縺悟万縺ウ縺セ縺吶

繧ス繝シ繧ケ繧ウ繝シ繝

ListFiles.h

	#pragma once
	//
	//蠢隕√↑蜷榊燕遨コ髢
		using namespace System;
		using namespace System::Windows::Forms;
		using namespace System::Data;
		using namespace System::Collections::Generic;
	//
	ref class ListFiles
	{
	private:	List<String^>^	DirListItems;	//繝輔か繝ォ繝縺ョ繝ェ繧ケ繝医r譬シ邏阪☆繧九
	public:		List<String^>^	FileListItems;	//繝輔ぃ繧、繝ォ縺ョ繝ェ繧ケ繝医r譬シ邏阪☆繧九
												//繝輔ぃ繧、繝ォ縺ョ繝ェ繧ケ繝医ッ蜈ャ髢九@縺ヲ縲∝叙繧雁コ縺帙k繧医≧縺ォ縺吶k縲
	private:	int				_NumberOfFolders;	//繝輔か繝ォ繝縺ョ謨ー
	private:	int				_NumberOfFiles;		//繝輔ぃ繧、繝ォ縺ョ謨ー
	//
	public:		property	int	NumberOfFolders{	//隱ュ縺ソ蜃コ縺怜ーら畑縺ァ蜈ャ髢
					int	get(){
						return(_NumberOfFolders);
					}
				}
	public:		property	int	NumberOfFiles{
					int	get(){
						return(_NumberOfFiles);
					}
				}
	public:
		ListFiles(void);
	//謖螳壹&繧後◆繝輔か繝ォ繝莉・荳九ョ繝輔ぃ繧、繝ォ繧貞ィ縺ヲ讀懃エ「縲
	public:	System::Void	SerchFilesUnderSpecifiedFolder(String^	TopFolder,	String^	Filter);
	//謖螳壹&繧後◆繝輔か繝ォ繝莉・荳九ョ繝輔ぃ繧、繝ォ繧呈爾縺吶
	private:	System::Void	ListFileNameInSpecifiedFolder(String^	TopFolder,	String^ Filter);
	//謖螳壹&繧後◆繝輔か繝ォ繝莉・荳九ョ繝輔か繝ォ繝繧呈爾縺吶
	private:	System::Void	SerchFolderRecursively(String^	TopFolder);
	};

ListFile.cpp

	#include "StdAfx.h"
	#include "ListFiles.h"
	//
	ListFiles::ListFiles(void){
		//蠢隕√↑螟画焚繧貞晄悄蛹
		this->DirListItems = gcnew List<String^>;
		this->FileListItems = gcnew List<String^>;
		_NumberOfFolders = 0;
		_NumberOfFiles = 0;
	}
	//
	System::Void	ListFiles::SerchFilesUnderSpecifiedFolder(String^	TopFolder,	String^	Filter){
		_NumberOfFolders = 0;	//繝輔か繝ォ繝謨ー縺ィ繝輔ぃ繧、繝ォ謨ー繧貞晄悄蛹
		_NumberOfFiles = 0;
	//
		SerchFolderRecursively(TopFolder);	//縺セ縺壹ッ繝輔か繝ォ繝縺縺台ク隕ァ縺吶k縲
	//
		//縺薙%縺ァ縲.irListItems縺ォ縺ッ縲∵欠螳壹ヵ繧ゥ繝ォ繝莉・荳九ョ繝輔か繝ォ繝縺悟ィ驛ィ荳隕ァ縺輔l縺ヲ縺繧九
		//蜷繝輔か繝ォ繝縺ョ荳ュ縺ォ縺ゅk繝輔ぃ繧、繝ォ繧剃ク隕ァ縺吶k縲
		//繧ウ繝ャ繧ッ繧キ繝ァ繝ウ縺九i荳縺、縺壹▽繝輔か繝ォ繝蜷阪r蜿悶j蜃コ縺励√◎縺ョ繝輔か繝ォ繝莉・荳九ョ繝輔ぃ繧、繝ォ蜷阪r蛻玲嫌縺吶k縲
		for each (String^	DirItem in this->DirListItems){
			//繝輔ぃ繧、繝ォ蜷阪r霑ス蜉
			ListFileNameInSpecifiedFolder(DirItem, Filter);
			Application::DoEvents();	//繧「繝励Μ縺悟崋縺セ繧九ョ繧帝亟豁「縺吶k縲
		}
	}
	//謖螳壹&繧後◆繝輔か繝ォ繝莉・荳九ョ繝輔ぃ繧、繝ォ繧偵Μ繧ケ繝医☆繧九
	System::Void	ListFiles::ListFileNameInSpecifiedFolder(String^	TopFolder,	String^ Filter){
		//謖螳壹ヵ繧ゥ繝ォ繝莉・荳九ョ縲√ヵ繧。繧、繝ォ繧貞叙蠕励☆繧九
		array<String^>^ Files = System::IO::Directory::GetFiles(TopFolder);
		//
		System::Collections::IEnumerator^	listFiles	=	Files->GetEnumerator();
		while( listFiles->MoveNext()){
			String^ fileName = safe_cast<String^>(listFiles->Current);
			FileListItems->Add(fileName);
			_NumberOfFiles ++;
		}
	}
	//謖螳壹ヵ繧ゥ繝ォ繝莉・荳九ョ繝輔か繝ォ繝繧貞榊クー逧縺ォ讀懃エ「縺励∪縺吶
	System::Void	ListFiles::SerchFolderRecursively(String^	TopFolder){
		bool	IsExceptionOccured = false;
		array<String^>^ Dirs;
		//
		Application::DoEvents();	//繧「繝励Μ縺悟崋縺セ繧九ョ繧帝亟豁「縺吶k縲
		//
			 try{
				Dirs = System::IO::Directory::GetDirectories(TopFolder);
			 }catch(Exception^ e){
				 //萓句、悶′縺翫%縺」縺溘i縲√い繧ッ繧サ繧ケ縺ァ縺阪↑縺縺ィ險縺縺薙→縲ゅ%繧御サ・髯阪ョ繝輔か繝ォ繝縺ッ讀懃エ「縺励↑縺縲
				 IsExceptionOccured = true;
			 }
			//
			 if(IsExceptionOccured == false){
				_NumberOfFolders++;
				DirListItems->Add(TopFolder);	//繝輔か繝ォ繝繧定ソス蜉
				System::Collections::IEnumerator^	listDirs = Dirs->GetEnumerator();
				while( listDirs->MoveNext()){
					String^	dirName =	safe_cast<String^>(listDirs->Current);
					//繝輔か繝ォ繝繧貞榊クー逧縺ォ讀懃エ「縺吶k縲
					SerchFolderRecursively(dirName);
				}
			}
	}

繝医ャ繝   蟾ョ蛻 繝舌ャ繧ッ繧「繝繝 繝ェ繝ュ繝シ繝   荳隕ァ 蜊倩ェ樊、懃エ「 譛邨よ峩譁ー   繝倥Ν繝   譛邨よ峩譁ー縺ョRSS
Last-modified: 2009-06-08 (譛) 20:45:04 (3089d)