NetShareDel() Program Example
This function deletes a share name from a server's list of shared resources, disconnecting all connections to the shared resource. The extended function NetShareDelEx() allows the caller to specify a SHARE_INFO_0, SHARE_INFO_1, SHARE_INFO_2, SHARE_INFO_502, or SHARE_INFO_503 structure. This function applies only to Server Message Block (SMB) shares. For other types of shares, such as Distributed File System (DFS) or WebDAV shares, use Windows Networking (WNet) functions, which support all types of shares. Only members of the Administrators, Server Operators, or Power Users local group, or those with Server Operator group membership, can successfully delete file shares with a call to the NetShareDel() function. The Print Operator can delete printer shares. If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management share functions.
The following code sample demonstrates how to delete a share using a call to the NetShareDel() function.
Create a new empty Win32 console application project. Give the project name and change the project location is needed.
Then, add the source file and give it a suitable name.
Then, add the following source code.
#pragma comment(lib, "Netapi32.lib")
void wmain(int argc, WCHAR *argv[ ])
if(argc < 3)
wprintf(L"Usage: NetShareDel servername sharename\n");
wprintf(L"Example: NetShareDel Mikeserver Myfolder\n");
// Call the NetShareDel() function to delete the share.
res=NetShareDel(argv, argv, 0);
// Display the result of the call.
wprintf(L"NetShareDel() is OK!\n");
wprintf(L"%s share was removed successfully!\n", argv);
wprintf(L"NetShareDel() failed! Error %u\n", res);
We would like to delete the previous share. Take note that the share name will be used in the program.
So the share name, Testsharefolder will be used as the second parameter.
Build and run the project. The following screenshot is a sample output.
The share should be removed as shown in the following screenshot.