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!
mjroganAsked:
Who is Participating?
 
wguerramConnect With a Mentor Commented:
I tried this and it works:

Shell("NET SESSION /DELETE /y")
0
 
wguerramCommented:
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")
0
 
mjroganAuthor Commented:
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!
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
mjroganAuthor Commented:
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.
0
 
wguerramCommented:
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.
0
 
mjroganAuthor Commented:
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.
0
 
mjroganAuthor Commented:
Or it returns this:

'C:\delshared.bat' is not recognized as an internal or external command, operable program or batch file.
0
 
mjroganAuthor Commented:
That worked, thanks!
0
All Courses

From novice to tech pro — start learning today.