Delphi IDE (239)
Windows with Delphi (38)
Map or unmap a network disk at run-time
This article has not been rated yet. After reading, feel free to leave comments and rate it.
Question:I want to attach to a file server and map a network disk from within my Delphi application. How can I do this?
Answer:You need to use the Windows API function WNetAddConnection2 to map a remote share as a disk.
This function replaces the old WNetAddConnection call which did not allow to specify a username.
If you need to specify a user name or password you can do this in arguments 2 (password) and 3 (user name). Note that example below sends nil. If you send nil as user name and password, then WNetAddConnection2 will use the user context for the running process and the password associated with this account.
Note that supplying a user name/ password other than the process currently running will only work on Windows NT, Windows 2000 and Windows XP.
Finally use WNetCancelConnection2() to remove a network mapping.