Sign in to follow this  
errorist

[.net] System::IO::Directory::GetFiles(...) returns 0-length array

Recommended Posts

hi all... i ported this code from c# to managed cpp... c# version
		/// <summary>
		/// Searchs the spedified path for the specified file and returns a full file path if found
		/// </summary>
		public static string FindFile( string fileName, string searchPath )
		{
			if( searchPath.Length == 0 )
			{
				searchPath = Environment.CurrentDirectory;
			}

			foreach( string file in Directory.GetFiles( searchPath ) )
			{
				if( Path.GetFileName( file ) == fileName )
				{
					return file;
				}
			}

			foreach( string directory in Directory.GetDirectories( searchPath ) )
			{
				string file = FindFile( fileName, directory );

				if( Path.GetFileName( file ) == fileName )
				{
					return file;
				}
			}

			return string.Empty;
		}

managed cpp version

		// Searchs the spedified path for the specified file and returns a full file path if found
	public: static String* FindFile( String* fileName, String* searchPath )
		{
			if( searchPath->Length == 0 )
			{
				searchPath = Application::StartupPath;
			}

			IEnumerator* files = Directory::GetFiles( searchPath, Path::GetExtension( fileName ) )->GetEnumerator();

			while( files->MoveNext() )
			{
				String* file = __try_cast< String* >( files->Current );

				if( Path::GetFileName( file ) == fileName )
				{
					return file;
				}
			}

			IEnumerator* directories = Directory::GetDirectories( searchPath )->GetEnumerator();

			while( directories->MoveNext() )
			{
				String* directory = __try_cast< String* >( directories->Current );
				String* file = FindFile( fileName, directory );

				if( Path::GetFileName( file ) == fileName )
				{
					return file;
				}
			}

			return String::Empty;
		}

it worked and still works fine in c#...but it doesnt work fine now in managed cpp... this is annoying coz there should be no problem...every thing is ok...i cant tell anything else about the error...i just get empty array (0 length)... the file path which is passed in the parameter is correct...the path exist and it contains ~170 files... Directoy::GetDirectories(...) works fine...but Directory::GetFiles(...) doesnt... thanx in advance...

Share this post


Link to post
Share on other sites
Does the forgot the wildcard part mean you aren't having problems anymore?

If not, one thing I noticed is the comparison:



if(Path::GetFileName(file) == fileName)




If you change that to:



if(Path::GetFileName(file)->Equals(fileName))




it will work just fine.

Magius

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this