CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Confused over GetCurrentDirectory

    Does each process have its current directory reset to the folder where it was started from at the beginning, or is it some sort of a global value that is not changed like that?

    And the second question, what can change that current directory?

  2. #2
    Join Date
    Feb 2005
    Posts
    2,160

    Re: Confused over GetCurrentDirectory

    It's part of the environment inherited by the application from the OS at execution time, so it's a little of both.

    As far as I know, there's no way to change it without directly mucking with the environment table for the application and this would likely screw up many of the standard C/C++ library functions.

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Confused over GetCurrentDirectory


  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Confused over GetCurrentDirectory

    Quote Originally Posted by ahmd View Post
    Does each process have its current directory reset to the folder where it was started from at the beginning [...]
    No. The current directory of a process may be or may be not the folder from where it is launched.
    See for an example the lpCurrentDirectory parameter of CreateProcess function.
    Also it may be changed anytime by a call of SetCurrentDirectory, GetOpenFileName, and so on.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  5. #5
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Confused over GetCurrentDirectory

    Guys, I'm a bit confused. According to MSDN and the link that Viggy posted above:
    the current directory; it is the directory in which the active application started, unless explicitly changed.
    , but ovidiucucu says that:
    No. The current directory of a process may be or may be not the folder from where it is launched. See for an example the lpCurrentDirectory parameter of CreateProcess function.
    The reason I need to know this is because I'm tryin to convert a relative path to an absolute one (that path will be used in my app only) and GetCurrentDirectory seems to be the only way to do it.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Confused over GetCurrentDirectory

    I said the same thing in other words.

    To get the directory in which the executable file is, you can call GetModuleFileName function. See this FAQ: http://www.codeguru.com/forum/showthread.php?t=312471.

    // there are also other Codeguru FAQs which can make you less confused.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Confused over GetCurrentDirectory

    Yeah, I thought about it, but you see it's not that easy. Say, if I call CreateFile with "test.txt", where will test.txt be created?

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Confused over GetCurrentDirectory

    Quote Originally Posted by ahmd View Post
    Say, if I call CreateFile with "test.txt", where will test.txt be created?
    In the current directory of the process.
    Last edited by ovidiucucu; September 26th, 2009 at 06:18 AM.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Confused over GetCurrentDirectory

    OK, I apologize for my tenaciousness, but does this mean that I need to use GetCurrentDirectory to convert that relative path to an absolute one (for my program only)?

  10. #10
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: Confused over GetCurrentDirectory

    But why do you need that? If you know the current directory, or have set current directory, you need not to use absolute path.

    But personally, I always prefer using absolute path (without relying on current directory, since drive change may make matter worse).
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  11. #11
    Join Date
    Feb 2009
    Location
    Portland, OR
    Posts
    1,488

    Re: Confused over GetCurrentDirectory

    I'm sorry, but the question of why I need it is not what is discussed on this thread. But since you asked, I need it to compare two file paths (that can be provided by a user) and that doesn't seem to be an easy task.

  12. #12
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Confused over GetCurrentDirectory

    Quote Originally Posted by ahmd View Post
    Guys, I'm a bit confused. According to MSDN and the link that Viggy posted above:, but ovidiucucu says that:

    The reason I need to know this is because I'm tryin to convert a relative path to an absolute one (that path will be used in my app only) and GetCurrentDirectory seems to be the only way to do it.
    I believe that ovidiucucu was referring to the directory in which the EXE itself resides.

    Viggy

  13. #13
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: Confused over GetCurrentDirectory

    Quote Originally Posted by ahmd View Post
    [...] I need it to compare two file paths (that can be provided by a user) [...]
    That's far, far away of both thread title and OP (original post).
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured