As I demonstrated how to map a network drive in my last post, it seems like it might be a good idea to explain how to delete an existing mapped drive as well. Again we have to rely on Windows APIs to do this… but its a pretty simple one this time (for once).
Archives For VB.NET – Windows API
Useful Windows APIs that I have used in VB.NET
There is no .NET method for creating a network drive yet (as of .NET 4.0) so here is how you can do it using good old Windows APIs 🙂 As well as the API definitions, I’ve included a managed method that wraps up the functionality of the API so that it is easier to use from .NET code.
Perhaps this is common knowledge for a lot of people but even though I have been working with Windows APIs from VB.NET quite a lot recently, I did not know about this little ‘trick’ until today.
Ever wanted to identify the Process that started another process (for example you might want to find out which process launched your application) ? Well I’ve finally got some code working that gets the ID of the process that created the process you specify.
I recently had a need in one of my programs to loop through all network drives on a computer and get the drive letter and the UNC path that the network drive mapped to. I thought this would be easy with the System.IO.DriveInfo.GetDrives method but unfortunately it was not as the DriveInfo class offers no way to retrieve the UNC path that the network drive maps to. After a bit of research I could find no better way than using a Windows API (I’m not a fan of WMI!). Here is some example VB.NET code that I have written that will do this:
Here’s some code I wrote recently that uses the GetProfileType Windows API to determine what type of profile the currently logged on user is using.
The example code below will detect when a USB drive is connected and will then look for a file named test.txt in the USB drive.