Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between _chdir and SetCurrentDirectory in windows?

Are there any difference that I should choose one over the other?

like image 581
shawn Avatar asked Sep 25 '11 07:09

shawn


2 Answers

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().

like image 94
Uffe Avatar answered Nov 16 '22 22:11

Uffe


_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.

like image 6
eran Avatar answered Nov 16 '22 22:11

eran