Un ejemplo de uso muy básico de la librería NCalc ~> http://ncalc.codeplex.com/
Código (vbnet) [Seleccionar]
Dim MathExpression As String = "(2 + 3) * 2" ' Result: 10
Dim NCalcExpression As New NCalc.Expression(MathExpression)
MsgBox(NCalcExpression.Evaluate().ToString)
Una forma de comprobar si un archivo es un ensamblado .NET:
Código (vbnet) [Seleccionar]
' Usage Examples:
'
' MsgBox(IsNetAssembly("C:\File.exe"))
' MsgBox(IsNetAssembly("C:\File.dll"))
''' <summary>
''' Gets the common language runtime (CLR) version information of the specified file, using the specified buffer.
''' </summary>
''' <param name="filepath">Indicates the filepath of the file to be examined.</param>
''' <param name="buffer">Indicates the buffer allocated for the version information that is returned.</param>
''' <param name="buflen">Indicates the size, in wide characters, of the buffer.</param>
''' <param name="written">Indicates the size, in bytes, of the returned buffer.</param>
''' <returns>System.Int32.</returns>
<System.Runtime.InteropServices.DllImport("mscoree.dll",
CharSet:=System.Runtime.InteropServices.CharSet.Unicode)>
Private Shared Function GetFileVersion(
ByVal filepath As String,
ByVal buffer As System.Text.StringBuilder,
ByVal buflen As Integer,
ByRef written As Integer
) As Integer
End Function
''' <summary>
''' Determines whether an exe/dll file is an .Net assembly.
''' </summary>
''' <param name="File">Indicates the exe/dll file to check.</param>
''' <returns><c>true</c> if file is an .Net assembly; otherwise, <c>false</c>.</returns>
Public Shared Function IsNetAssembly(ByVal [File] As String) As Boolean
Dim sb = New System.Text.StringBuilder(256)
Dim written As Integer = 0
Dim hr = GetFileVersion([File], sb, sb.Capacity, written)
Return hr = 0
End Function
Un simple efecto de máquina de escribir:
Código (vbnet) [Seleccionar]
' ***********************************************************************
' Author : Elektro
' Modified : 03-08-2014
' ***********************************************************************
' <copyright file="TypeWritter.vb" company="Elektro Studios">
' Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
#Region " Usage Examples "
'Sub Main()
' Console.WriteLine()
' TypeWritter.WriteLine("[ Typewritter ] - By Elektro")
' TypeWritter.WriteLine()
' TypeWritter.WriteLine()
' TypeWritter.WriteLine("Hola a todos!, les presento este humilde y simple efecto de máquina de escribir")
' TypeWritter.WriteLine()
' TypeWritter.WriteLine("Si os fijais aténtamente, quizás ya habreis notado, que hay pausas realistas, al escribir signos de puntuación...")
' TypeWritter.WriteLine()
' TypeWritter.WriteLine("[+] Podemos establecer la velocidad de escritura, por ejemplo, a 20 ms. :")
' TypeWritter.WriteLine("abcdefghijklmnopqrstuvwxyz", 20)
' TypeWritter.WriteLine()
' TypeWritter.WriteLine("[+] Podemos establecer la velocidad de las pausas, por ejemplo, a 2 seg. :")
' TypeWritter.WriteLine(".,;:", , 2 * 1000)
' TypeWritter.WriteLine()
' TypeWritter.WriteLine("[+] El efecto corre en una tarea asíncrona, por lo que se pueden hacer otras cosas mientras tanto, sin frezzear una GUI, y también podemos cancelar la escritura en cualquier momento, gracias al Token de cancelación.")
' TypeWritter.WriteLine()
' TypeWritter.WriteLine()
' TypeWritter.WriteLine("Esto es todo por ahora.")
' Console.ReadKey()
'End Sub
#End Region
#Region " TypeWritter "
''' <summary>
''' Simulates text-typying effect like a Typewritter.
''' </summary>
Public Class TypeWritter
#Region " Properties "
''' <summary>
''' When set to 'True', the running 'Typewritter' task will be cancelled.
''' ( The property is set again to 'False' automatically after a 'Task' is cancelled )
''' </summary>
Public Shared Property RequestCancel As Boolean = False
#End Region
#Region " Task Objects "
''' <summary>
''' The typewritter asynchronous Task.
''' </summary>
Private Shared TypeWritterTask As Threading.Tasks.Task
''' <summary>
''' The typewritter Task Cancellation TokenSource.
''' </summary>
Private Shared TypeWritterTaskCTS As New Threading.CancellationTokenSource
''' <summary>
''' The typewritter Task Cancellation Token.
''' </summary>
Private Shared TypeWritterTaskCT As Threading.CancellationToken = TypeWritterTaskCTS.Token
#End Region
#Region " Private Methods "
''' <summary>
''' Writes text simulating a Typewritter effect.
''' </summary>
''' <param name="CancellationToken">Indicates the cancellation token of the Task.</param>
''' <param name="Text">Indicates the text to type.</param>
''' <param name="TypeSpeed">Indicates the typying speed, in ms.</param>
''' <param name="PauseDuration">Indicates the pause duration of the punctuation characters, in ms.</param>
Private Shared Sub TypeWritter(ByVal CancellationToken As Threading.CancellationToken,
ByVal [Text] As String,
ByVal TypeSpeed As Integer,
ByVal PauseDuration As Integer)
' If Text is empty then write an empty line...
If String.IsNullOrEmpty([Text]) Then
' If not cancellation is already requested then...
If Not CancellationToken.IsCancellationRequested Then
' Write an empty line.
Console.WriteLine()
' Wait-Speed (empty line).
Threading.Thread.Sleep(PauseDuration)
End If ' CancellationToken.IsCancellationRequested
End If ' String.IsNullOrEmpty([Text])
' For each Character in Text to type...
For Each c As Char In [Text]
' If not cancellation is already requested then...
If Not CancellationToken.IsCancellationRequested Then
' Type the character.
Console.Write(CStr(c))
' Type-Wait.
Threading.Thread.Sleep(TypeSpeed)
If ".,;:".Contains(c) Then
' Pause-Wait.
Threading.Thread.Sleep(PauseDuration)
End If
Else ' want to cancel.
' Exit iteration.
Exit For
End If ' CancellationToken.IsCancellationRequested
Next c ' As Char In [Text]
End Sub
#End Region
#Region " Public Methods "
''' <summary>
''' Writes text simulating a Typewritter effect.
''' </summary>
''' <param name="Text">Indicates the text to type.</param>
''' <param name="TypeSpeed">Indicates the typying speed, in ms.</param>
''' <param name="PauseDuration">Indicates the pause duration of the punctuation characters, in ms.</param>
Public Shared Sub Write(ByVal [Text] As String,
Optional ByVal TypeSpeed As Integer = 75,
Optional ByVal PauseDuration As Integer = 400)
' Run the asynchronous Task.
TypeWritterTask = Threading.Tasks.
Task.Factory.StartNew(Sub()
TypeWritter(TypeWritterTaskCT, [Text], TypeSpeed, PauseDuration)
End Sub, TypeWritterTaskCT)
' Until Task is not completed or is not cancelled, do...
Do Until TypeWritterTask.IsCompleted OrElse TypeWritterTask.IsCanceled
' If want to cancel then...
If RequestCancel Then
' If not cancellation is already requested then...
If Not TypeWritterTaskCTS.IsCancellationRequested Then
' Cancel the Task.
TypeWritterTaskCTS.Cancel()
' Renew the cancellation token and tokensource.
TypeWritterTaskCTS = New Threading.CancellationTokenSource
TypeWritterTaskCT = TypeWritterTaskCTS.Token
End If
' Reset the cancellation flag var.
RequestCancel = False
' Exit iteration.
Exit Do
End If
Loop ' TypeTask.IsCompleted OrElse TypeTask.IsCanceled
End Sub
''' <summary>
''' Writes text simulating a Typewritter effect, and adds a break-line at the end.
''' </summary>
''' <param name="Text">Indicates the text to type.</param>
''' <param name="TypeSpeed">Indicates the typying speed, in ms.</param>
''' <param name="PauseDuration">Indicates the pause duration of the punctuation characters, in ms.</param>
Public Shared Sub WriteLine(ByVal [Text] As String,
Optional ByVal TypeSpeed As Integer = 75,
Optional ByVal PauseDuration As Integer = 400)
Write([Text], TypeSpeed, PauseDuration)
Console.WriteLine()
End Sub
''' <summary>
''' Writes an empty line.
''' </summary>
''' <param name="PauseDuration">Indicates the pause duration of the empty line, in ms.</param>
Public Shared Sub WriteLine(Optional ByVal PauseDuration As Integer = 750)
Write(String.Empty, 1, PauseDuration)
End Sub
#End Region
End Class
#End Region