Link to home
Start Free TrialLog in
Avatar of mjrogan
mjrogan

asked on

Disconnect All Users From Share With VB.NET?

Hello,

I need a process that will automatically disconnect all users connected to a share on a server when it is executed.  Ideally, I would like to do this with .NET.

I need this process to have the same functionality as the following manual process:

-On Windows Server 2003, I go to Computer Management --> Shared Folders --> Sessions, right click "sessions" and then choose "Disconnect all sessions".

Alternatively, this can be done at a command prompt with:

NET SESSION /DELETE /y

I need to automate this to run automatically at a specified time.

Thanks!
Avatar of wguerram
wguerram

I tied using this API: NetSessionDel

But i never could it get to work:

You can do this:

        Dim s As New System.IO.StreamWriter("C:\delshared.bat")
        s.Write("NET SESSION /DELETE /y")
        s.Close()
        Shell("C:\del.bat", AppWinStyle.Hide)
        Kill("C:\delshared.bat")
Avatar of mjrogan

ASKER

Thanks.  The filename in the fourth line should be delshared.bat as well, right?

If so, I am getting the following when I run it:

"The batch file cannot be found"

What could be wrong here?

Thanks again!
Avatar of mjrogan

ASKER

Also, if I comment out the last line, it runs without error, but the command window reamins open.  Once I uncomment the last line, however, the batch file not found error returns.
Sorry, it should be this way

        Dim s As New System.IO.StreamWriter("C:\delshared.bat")
        s.Write("NET SESSION /DELETE /y")
        s.Close()
        Shell("C:\delshared.bat", AppWinStyle.Hide) 'Execute the bat file
        Kill("C:\delshared.bat")   'Delete the file from the disk


I tried and it works fine.
Avatar of mjrogan

ASKER

The first time it is executed, it does run successfully.  However, any subsequent time that it is executed, it returns the following:

The batch file cannot be found.
Avatar of mjrogan

ASKER

Or it returns this:

'C:\delshared.bat' is not recognized as an internal or external command, operable program or batch file.
ASKER CERTIFIED SOLUTION
Avatar of wguerram
wguerram

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mjrogan

ASKER

That worked, thanks!