DirectoryWalker converting C# to C++/CLR

  • Thread starter Thread starter Festus Hagen
  • Start date Start date
F

Festus Hagen

Guest
Hi All,

New to Visual Studio, get around a bit in C/C++ ... not a guru though!
Have my own DirectoryRecursion class that uses FindFirst/NextFile for straight C++ on Windows and fills a list<string>, Trying to achieve the same results using GetDirectories() and GetFiles().

VS Community 2015

Found this MS article How to recursively search folders by using Visual C++ and it fails to retrieve all files ...


void DirSearch(String^ sDir)
{
try
{
array<String^>^ d = Directory::GetDirectories(sDir);
int numDirs = d->Length;
for(int i = 0; i < numDirs; i++) {
array<String^>^ f = Directory::GetFiles(d, tbxFilter->Text);
int numFiles = f->Length;

for(int j = 0; j < numFiles; j++) {
listBox1->Items->Add(f[j]); // for testing, will convert to a enumerable collection of some sorts
}
DirSearch(d);
}
}
catch(System::Exception^ e)
{
System::Console::WriteLine(e->Message);
}
}


Didnt dig much into the above as I found this: Scanning a drive with drilldowns using C#? and have spent my time trying to convert it to C++/CLR without success, so in the flux of things here is where I am at with it. errors are marked // ERROR:


namespace DirectoryWalker
{
using namespace System;
using namespace System::IO;
using namespace System::Collections::Generic;

public ref class DirectoryWalker : IEnumerable<String^>
{
private:
String^ _seedPath;
Func<String^, bool>^ _directoryFilter;
Func<String^, bool>^ _fileFilter;

public: DirectoryWalker(String^ seedPath) : this(seedPath, "", "") // ERROR: expected an identifier
{ // Syntax error: this
} // Not sure what to replace null with.

public: DirectoryWalker(String^ seedPath, Func<String^, bool>^ directoryFilter, Func<String^, bool>^ fileFilter)
{
if (seedPath->Equals(String::Empty))
throw gcnew ArgumentNullException(seedPath);
_seedPath = seedPath;
_directoryFilter = directoryFilter;
_fileFilter = fileFilter;
}

virtual IEnumerator<String^>^ GetEnumerator()
{
Queue<String^>^ directories = gcnew Queue<String^>();
directories->Enqueue(_seedPath);
Queue<String^>^ files = gcnew Queue<String^>();
while (files->Count > 0 || directories->Count > 0)
{
if (files->Count > 0)
{
return files->Dequeue; // ERROR: non standard syntax; use & to create a pointer to member
} // return: cannot convert from System::String ^(__clrcall System::Collections::Generic::Queue<System::String ^>::* )(void) to System::Collections::Generic::IEnumerator<System::String ^> ^
// At a loss here, understand the error, dont know how to fix it ...

if (directories->Count > 0)
{
String^ dir = directories->Dequeue();
array<String^>^ newDirectories = Directory::GetDirectories(dir);
array<String^>^ newFiles = Directory::GetFiles(dir);
for each(String^ path in newDirectories)
{
if (_directoryFilter->Equals(String::Empty) || _directoryFilter(path))
directories->Enqueue(path);
}
for each(String^ path in newFiles)
{
if (_fileFilter->Equals(String::Empty) || _fileFilter(path))
files->Enqueue(path);
}
}
}
}
virtual System::Collections::IEnumerator^ GetEnumerator2() = System::Collections::IEnumerable::GetEnumerator
{
return GetEnumerator();
}
}
}; // ERROR: expected a ;, syntax error: missing ; before }





Not sure the override is correctly done.
Unsure of the use of Func(), never used it before.

The end goal, to have an enumerable collection of path/filenames.

Thank you
-Enjoy
fh : )_~

Continue reading...
 
Back
Top