Question

I'm trying to disconnect network connections which are not mapped to a local drive (e.g. net use \server\share).

I know the command net use U: /D to delete a network connection which is mapped to U:.

Now I want to disconnect that connection using it's UNC Path (e.g. net use \server\ /D).

I want to implement this in a C# Application using the Windows Networking API (WNetCancelConnection-Function) but this function can't handle the UNC Path, too.

I receive all connected UNC Paths with the following code:

var searcher = new ManagementObjectSearcher(
        "SELECT * FROM Win32_NetworkConnection");

        foreach (ManagementObject queryObj in searcher.Get())
        {
            Console.WriteLine(queryObj["RemoteName"]);
            Console.WriteLine(queryObj["RemotePath"]);
        }
        Console.ReadKey();

Hope so can help me solving that problem.

Edit:

i = WNetCancelConnection2A(ls_ShareName, iFlags, 1);

ls_ShareName = "\\server\share" (escaped) iFlags = 0

results in: This network connection does not exist.

I'm 100% sure that my "shareName" is correct... also net use \server\share /D does not work.

Was it helpful?

Solution

Not sure where can be your problem without seeing your code. But after

net use \\192.168.1.45\ipc$

the following code

#include "windows.h"

void main(void){
    WNetCancelConnection2("\\\\192.168.1.45\\ipc$",0,TRUE);
}

disconnects the resource without problems.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top