Click to Rate and Give Feedback
MSDN
MSDN Library
System Services
File Services
File Systems
 SetCurrentDirectory Function

  Switch on low bandwidth view
SetCurrentDirectory Function

Changes the current directory for the current process.

Syntax

C++
BOOL WINAPI SetCurrentDirectory(
  __in  LPCTSTR lpPathName
);

Parameters

lpPathName [in]

The path to the new current directory. This parameter may specify a relative path or a full path. In either case, the full path of the specified directory is calculated and stored as the current directory. For more information, see Naming a File.

The string must not exceed MAX_PATH characters, including the terminating null character. The final character before the null character must be a backslash ('\'). If you do not specify the backslash, it will be added for you; therefore, specify MAX_PATH-2 characters for the path unless you include the trailing backslash, in which case, specify MAX_PATH-1 characters for the path.

Return Value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Each process has a single current directory made up of two parts:

  • A disk designator that is either a drive letter followed by a colon, or a server name and share name (\\servername\sharename)
  • A directory on the disk designator

Multi-threaded applications and shared library code should should not use the SetCurrentDirectory function. The current directory state written by the SetCurrentDirectory function is stored as a global variable in each process, therefore multi-threaded applications cannot reliably use this value without possible data corruption from other threads that may also be reading or setting this value. This limitation also applies to the GetCurrentDirectory and GetFullPathName functions.

Examples

For an example, see Changing the Current Directory.

Requirements

Minimum supported clientWindows 2000 Professional
Minimum supported serverWindows 2000 Server
HeaderWinBase.h (include Windows.h)
LibraryKernel32.lib
DLLKernel32.dll
Unicode and ANSI namesSetCurrentDirectoryW (Unicode) and SetCurrentDirectoryA (ANSI)

See Also

Directory Management Functions
GetCurrentDirectory
GetFullPathName

Send comments about this topic to Microsoft

Build date: 6/25/2009

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Issue setting current directory when running app as a service      Binu Mathew   |   Edit   |   Show History
Hi,

My application is run as a windows service. It needs to change the current directory to a mapped path for eg. x:\myapp\data\ or \\192.168.9.132\myapp\data\.. When I run the application as a standard desktop application, it set the path correctlt, but when I run the same application as a service it does not set the path and comes back with probably Path not Found.

If I change the directory to local directory (not a mapped or network directory) it works fine.

Thanks
Tags What's this?: Add a tag
Flag as ContentBug
Re: Issue setting current directory when running app as a service      Joshua25640735   |   Edit   |   Show History
Binu Matthew, I should flag this as bug but I won't. Better to document this clearly.


If you are running as a service, you can't use mapped drives.

Furthermore, if your service isn't a domain account (e.g. if you follow all the

defaults and run as SYSTEM), you cannot use network paths at all.


Tags What's this?: Add a tag
Flag as ContentBug
Re: Issue setting current directory when running app as a service      mxman   |   Edit   |   Show History
Actually, you CAN use a mapped drive when running an app as a service.

Your service must be running under a user name that has credentials that allow drives to be mapped. In the Windows NT days, we used to find we could just log in as that user, map the drive, then when the service started under that login, the drives would be available. (And, the AutoExec resource kit tool allowed you to do this.)

However, it is better for your app to be passed drive mapping information (login name, password, drive) and use the WNetAddConnection2 function. You could pass it in to your service in a configuration file, such as an ini file, or create a registry entry to store the information. When your service starts, it should check for the settings, and attempt to map the drive, and report errors accordingly.
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker