Are there any difference that I should choose one over the other?
They achieve the same result but belong to different APIs, so they return their results and report errors in different ways.
If you're already using other routines from either API, pick that one. If not, SetCurrentDirectory() is more "Windowsy", while _chdir() is more similar to the POSIX API. If you have a mind to port the code to, say, a Linux platform, use _chdir(); if you know you will only ever run the code on Windows platforms, SetCurrentDirectory().
_chdir
actually uses SetCurrentDirectory
internally, so on most cases they are effectively interchangeable. However, _chdir
does one more thing: it updates the current directory of the current drive, stored in an environment variable. This is needed, as the remark in _tchdir
states, because "other functions (fullpath, spawn, etc) need them to be set".
I'm not sure how much this is needed these days, but I would say - if you're using those POSIX-style functions for file operations, path manipulation, process creation etc., use _chdir
accordingly. If you're using Win32 API functions directly, use SetCurrentDirectory
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With