Microsoft.VisualBasic

Published August 11th 2008

One of my recent projects involved copying large files (10 MBs or more) over a network connection, usually a VPN connection over the internet. I wanted a way to display progress but didn't want to have to spend too long writing the function. Then I remembered ShellUI.dll and the Explorer dialogs it creates and how those would be perfect for what I was doing. I immediately googled for the extern signatures necessary to summon the file copy dialog when I discovered from some research that this functionality was already wrapped in the Microsoft.VisualBasic assembly!

Scott Hanselman recently wrote about Single Instance WinForms and how you could essentially recreate the old Win32/VB6 single app instance to handle all requests - so multiple attempts at opening a document result in the same application servicing them. This is very useful functionality and its already nicely wrapped up in the Microsoft.VisualBasic assembly.

I spent a good two hours digging into the assembly to see just what old favorites of mine were still around. Beep is in there and Screen, another old favorite, among others I recall with fond memories. There's also some new functionality I either was never aware of or have just simply forgotten.

So here's a brief overview of what I found in there digging around. Some of the functionality you can do in C# with some effort and a lot of this functionality is very useful.

Microsoft.VisualBasic

MyGroupCollectionAttribute - here is one of those very interesting "supports Visual Basic and not intended to be used by your code." After trying to get this guy to work, reading up on the VB.NET language reference, and generally Googling it, I get the idea. Basically it uses strings to make a "group type family" of classes and acts like a compile time template to add the necessary methods to subclasses of the class marked with this attribute, and then gives you some syntaxical sugar where you don't even need to invoke your singleton GetInstance (or equivalent method); instead something that was once:

var x = MyClass.GetInstance(); x.DoSomething();

becomes:

MyClass.DoSomething();

without MyClass or DoSomething being declared as static (or Shared in VB). It also allows you to alias these instances into a collection, a la My.Classes.MyClass and My.Classes.AnotherClass (assuming both MyClass and AnotherClass share the same ancestor and that ancestor is decorated with this attribute properly). All in all a pretty neat idea, but I couldn't get it to work, the documentation was lacking and I tried every combination of C#, VB.NET, and C# and VB.NET to make this happen but obviously I was unable to devise the correct one.

Microsoft.VisualBasic.FileSystem

Kill - who doesn't like a function named Kill that does exactly what you'd expect it to? (Assuming you'd expect it to delete a file and not terminate a process that is. Or not kill a person.)

Microsoft.VisualBasic.Financial

* - all of these functions look very interesting if you are a financial programmer. They don't have a whole lot of use for me, but still, a nice addition.

Microsoft.VisualBasic.Interaction

InputBox - a throwback to the old Windows 3.1 UI design days, an input box is a modal dialog (a #32770 IIRC) with a prompt, a text area, and buttons to confirm or dismiss the input box. Need a quick and easy way to get a value while testing your UI? The InputBox is effectively a simple - albeit perhaps not pretty - way of doing a Console.ReadLine from within a non-console WinForms application. Because the return value of the "InputBox" method is a string, an empty (not null) string indicates that the cancel button was clicked which can be easily confused with the user entering or blanking the answer text box. Also a notable throwback - the x and y positional arguments are in twips not pixels.

Partition - now this one is interesting and it has nothing to do with your file system. Its basically a function that allows you to explode a single value into the range in which it belongs by defining the complete set and range intervals. Microsoft provides a good example of what this does, and why the return value is an easily parsible string.

Switch - memories! You provide an even number of items in an object[] with each odd numbered item a bool value or expression that evaluates to bool, and the even numbered items are the associated return values for the first expression which evaluates to true. Basically a dynamic switch/case statement though certainly nowhere near as efficient. Could be useful at some point as long as I remember its there.

Microsoft.VisualBasic.Strings

StrReverse - returns a string with the character positions reversed such that index 0 is now the last index and the last index is now index 0.

Microsoft.VisualBasic.Devices.ComputerInfo

* - contains some of the general level stuff (like physical memory available, OS version) that people sometimes ask for without resorting to WMI or Windows API calls.

Microsoft.VisualBasic.Devices.Keyboard

SendKeys - this gives you some level of easy automation with other applications in that it allows you to send key presses to an application as if the keyboard were being pressed. You'll want to pair this with *.Interaction.AppActivate(string) which allows you to activate a window by caption. Tip: if you don't want to make it look like your app is ghost writing when you are sending a lot of text, just preserve the clipboard, copy the text to the clipboard, then send keys "^v" which is your Control+v key. Use this as a reference for all of the key command strings.

Microsoft.VisualBasic.Devices.Network

DownloadFile/UploadFile - allows you to specify URIs for remote addresses, local paths, and walla! instant download or upload of a file. This seems to be restricted to ftp, http, and https so other later installed protocols likely don't work.

Ping - does what you think, with a boolean return value indicating if the ping request was successful.

IsAvailable - a boolean method that tells answers whether a network connection is available and working.

NetworkAvailabilityChanged - an event you can subscribe to to be notified when the network availability changes without having to perform any complex Win API P/Invokes.

Microsoft.VisualBasic.FileIO.FileSystem

* - the CopyFile and MoveFile and DeleteFile all use the ShellUI.dll functionality to allow you to automatically add support for progress and user controllable long operations (such as allowing the user to cancel a copy operation). The DeleteFile methods even allow you to specify recycling bin options so you don't just unlink files, but send them to the recycling bin instead.

FindInFiles - the basic "find in files" ShellUI implementation which I believe does not make use of the indexing service.

GetTempFileName - a method which returns a temporary file name in the temporary directory for the current context (user's temporary directory for a user and one can assume system temporary directory for a service) and also creates a 0 byte file on disk so you can just open it up immediately without first creating it. I'm sure we've all dug into the API to get environment variables like the temporary directory and then had a temporary naming convention we implemented to do this in the past.

Microsoft.VisualBasic.FileIO.TextFieldParser

This is a great class which allows you to read all manner of text files, whether they are comma delimited, tab delimited, or fixed width record files. Yes, read, unfortunately not write. It has support for pretty much every text file situation you may run into - qualifiers, separators, and even comment characters. A useful quick, down and dirty class for upconverting old systems to a new format.