次の方法で共有


現在のディレクトリの変更

アクティブ パスの末尾にあるディレクトリは、現在のディレクトリと呼ばれます。これは、明示的に変更されていない限り、アクティブなアプリケーションが開始されたディレクトリです。 アプリケーションは、GetCurrentDirectory 関数を呼び出すことによって、現在のディレクトリを特定できます。 アプリケーションで必要な場合は、GetFullPathName 関数を使用して、ドライブ文字を含める必要がある場合があります。

手記

各プロセスは現在のディレクトリを 1 つだけ持つことができますが、SetCurrentDirectory 関数を使用してアプリケーションがボリュームを切り替えた場合、システムは各ボリュームの最後の現在のパス (ドライブ文字) を記憶します。 この動作は、現在のディレクトリ参照ポイントを別のボリュームに変更するときに、完全修飾パスなしでドライブ文字を指定した場合にのみ現れます。 これは、Get 操作または Set 操作に適用されます。

 

アプリケーションは、SetCurrentDirectory 関数を呼び出すことによって、現在のディレクトリを変更できます。

次の例では、GetCurrentDirectory と SetCurrentDirectoryの使用を示します。

#include <windows.h> 
#include <stdio.h>
#include <tchar.h>

#define BUFSIZE MAX_PATH
 
void _tmain(int argc, TCHAR **argv) 
{ 
   TCHAR Buffer[BUFSIZE];
   DWORD dwRet;

   if(argc != 2)
   {
      _tprintf(TEXT("Usage: %s <dir>\n"), argv[0]);
      return;
   }

   dwRet = GetCurrentDirectory(BUFSIZE, Buffer);

   if( dwRet == 0 )
   {
      printf("GetCurrentDirectory failed (%d)\n", GetLastError());
      return;
   }
   if(dwRet > BUFSIZE)
   {
      printf("Buffer too small; need %d characters\n", dwRet);
      return;
   }

   if( !SetCurrentDirectory(argv[1]))
   {
      printf("SetCurrentDirectory failed (%d)\n", GetLastError());
      return;
   }
   _tprintf(TEXT("Set current directory to %s\n"), argv[1]);

   if( !SetCurrentDirectory(Buffer) )
   {
      printf("SetCurrentDirectory failed (%d)\n", GetLastError());
      return;
   }
   _tprintf(TEXT("Restored previous directory (%s)\n"), Buffer);
}