5

I have a similar problem to this: Current working directory is visual studio directory

Except that I am working with a C++ project in Visual Studio. Any suggestions?

For example if I try the solution in the following post: GetCurrentDirectory for startup App. c++

I get this:

"C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE"

But I want it to be the Debug folder under my project/solution folders.

Community
  • 1
  • 1
dtmland
  • 2,136
  • 4
  • 22
  • 45
  • As for C++-CLI there should be equivalents for all the suggested [tag:C#] classes and operations – πάντα ῥεῖ Oct 30 '13 at 18:35
  • 2
    It is project setting, Project + Properties, Debugging, Working Directory. – Hans Passant Oct 30 '13 at 18:53
  • @HansPassant That is set to $(ProjectDir). How might I obtain that in my code? – dtmland Oct 30 '13 at 19:54
  • 1
    IIRC, you can set the Working Directory to be `$(OutDir)` instead of project dir. Then the current working directory path will be the actual location of the .exe (i.e. "C:\Project Path\Debug" or something). – Tim Oct 30 '13 at 22:41

1 Answers1

10

Using the _fullpath command allowed me to extract the current directory. For example you can modify the example code on the linked page :

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <direct.h>

void PrintFullPath( char * partialPath )
{
   char full[_MAX_PATH];
   if( _fullpath( full, partialPath, _MAX_PATH ) != NULL )
      printf( "Full path is: %s\n", full );
   else
      printf( "Invalid path\n" );
}

int main( void )
{
   // Get current directory
   PrintFullPath( ".\\" );
}
dtmland
  • 2,136
  • 4
  • 22
  • 45