Introduction
A recent project I was working on allowed users to select and delete files. I wanted to allow the user to be able to gracefully recover if the wrong file was deleted. The Windows recycle bin seemed to be the perfect solution but C# did not have a function to send deleted files to the recycle bin but Visual Basic did. The obvious answer was to call the Visual Basic routines from the C# program. The solution was to create a Visual Basic class with the required functionality and compile the class as a DLL so it could be called from C#.
Background
Both C# and Visual Basic produce assemblies containing CIL code. CIL code is a platform and CPU independent intermediate language which, at runtime, is compiled to platform and CPU specific instructions. Consequently, the two languages become the same after the compile process. In fact, all .NET languages compile to CIL code so you could call Java routines or have Visual Basic call C# functions. I will show you how to create a DLL in VB that may be called by C#.
Using the Code - The Visual Basic Class
The first task was to write the deletion routines in Visual Basic. The source shown in this article will concentrate on file deletion. The class in the demo project contains code to delete either files or directories.
The simplest version of the Visual Basic delete file function requires only the path and name of the file to be deleted. The other version of the file delete function displays dialogs to track the delete operation's progress and also allows you to request an exception be thrown if the user cancels the delete operation or some other error occurs. I wanted to provide both versions to the C# programmer.
The simplest function passes the filename
variable from C# to the equivalent Visual Basic function. The Visual Basic code is:
Public Shared Sub DelFile(ByVal filename As String)
Try
My.Computer.FileSystem.DeleteFile(filename, _
FileIO.UIOption.OnlyErrorDialogs, _
FileIO.RecycleOption.SendToRecycleBin, _
FileIO.UICancelOption.ThrowException)
Catch ex As Exception
Throw ex
End Try
End Sub
I created all subroutines using the Shared
keyword so you will not have to create an instance of the VBFileIO
class to access the function calls.
The other version of the function provides several options. You may elect to see the "Confirm File Delete" dialog or only error dialogs. You may also suppress or request exceptions in the event of errors. You select between the various options using Enum
values. The Visual Basic Enum
definitions were not displayed by the Visual Studio's IntelliSense when typing in values for the C# functions so I added my own Enum
definitions and code to transform my Enum
values to the equivalent Visual Basic values. My Enum
definitions are:
Enum UIDialogs
OnlyErrorDialogs
AllDialogs
End Enum
Enum CancelOptions
ThrowException
DoNothing
End Enum
The second variation of the delete function is:
Public Shared Sub DelFile(ByVal filename As String, _
ByVal uiOption As UIDialogs, _
ByVal cancelOption As CancelOptions)
This information must be placed in a Visual Basic project. I am using Visual Basic 2008 Express Edition and Visual C# 2008 Express Edition. In Visual Basic, select File->New Project and use the Class Library template. Specify a class name of VBFileIO
. Change the name of the class from Class1.vb to Recycle.vb by right clicking on the Class1.vb entry under the Solution Explorer panel and selecting the Rename option. Add the code to the class. Your class will look like this:
Public Class Recycler
Enum UIDialogs
OnlyErrorDialogs
AllDialogs
End Enum
Enum CancelOptions
ThrowException
DoNothing
End Enum
Public Shared Sub DelFile(ByVal filename As String)
Try
My.Computer.FileSystem.DeleteFile(filename, _
FileIO.UIOption.OnlyErrorDialogs, _
FileIO.RecycleOption.SendToRecycleBin, _
FileIO.UICancelOption.ThrowException)
Catch ex As Exception
Throw ex
End Try
End Sub
Public Shared Sub DelFile(ByVal filename As String, _
ByVal uiOption As UIDialogs, _
ByVal cancelOption As CancelOptions)
Dim cancel As FileIO.UICancelOption
Dim uoption As FileIO.UIOption
Select Case cancelOption
Case CancelOptions.ThrowException
cancel = FileIO.UICancelOption.ThrowException
Case CancelOptions.DoNothing
cancel = FileIO.UICancelOption.DoNothing
End Select
Select Case uiOption
Case UIDialogs.AllDialogs
uoption = FileIO.UIOption.AllDialogs
Case UIDialogs.OnlyErrorDialogs
uoption = FileIO.UIOption.OnlyErrorDialogs
End Select
Try
My.Computer.FileSystem.DeleteFile(filename, _
uoption, _
FileIO.RecycleOption.SendToRecycleBin, _
cancel)
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
Build the class to create a DLL file. The Visual Basic portion of the project is complete.
Using the Code - The C# Interface
Use of the Visual Basic class in C# is straight forward. Choose a project of your own or look at the code in the MixedLanguageDemo. Begin by registering the VBFileIO.dll by right clicking the References tab in the Solution Explorer panel for the C# project. Select "Add Reference" and choose the Browse tab. Navigate to the bin.Release folder contained in the VBFileIO
project created above and select VBFileIO.dll. The DLL is now registered as a referenced external assembly. You do not need to copy the DLL to the C# project folder. Visual Studio will automatically copy the DLL to the appropriate target folders when the C# project is built.
Add using VBFileIO;
to the using
list In your C# program. One, the statement clearly documents the program is using the VBFileIO
interface. Two, it saves key strokes when programming.
You may now delete a file using either of the following functions:
Recycler.DelFile(fileName);
Recycler.DelFile
(fileName, Recycler.UIDialogs.AllDialogs, Recycler.CancelOptions.ThrowException);
Directories may be deleted with either of the following functions:
Recycler.DelDir(dirName);
Recycler.DelDir
(dirName, Recycler.UIDialogs.AllDialogs, Recycler.CancelOptions.ThrowException);
Points of Interest
Use try
/catch
logic when calling DelFile
from C# to determine if the user cancelled the delete via the dialog or if an error occurred. I also noticed Read Only files are deleted without a warning.
History
- 21st March, 2008: Version 1.0.0.0 (It has been tested on Windows XP and Vista.)