VB.NET IMAP BODY ENVELOPE separating to class - structure

I would like separating the IMAP BODY ENVELOPE to own class.
(IMAP command: UID FETCH 4 (BODY ENVELOPE))
My class:
Class ENVELOPE
Class PL
Dim Name As String
Dim Value As String
End Class
Dim Type As String
Dim SubType As String
Dim PList() As PL 'Parameter list
Dim ID As String
Dim FileName As String 'Description
Dim Encoding As String
Dim Length As Integer
End Class
My IMAP result: https://pastebin.com/fv7yajsq
Try separating with:
Sub Separator(ByVal str As String)
Dim level As Integer = 0
Dim InAtt As String = ""
Dim lastl As Integer = 0
Dim skiplevel As Boolean = False
Dim levelss() As String = {0, 0, 0, 0, 0, 0}
For Each c As Char In str
If Not lastl = level Then
Debug.WriteLine(String.Join(",", levelss) & " - " & InAtt)
InAtt = ""
If lastl < level Then levelss(lastl) += 1
lastl = level
End If
If c = "("c And skiplevel = False Then
level += 1
ElseIf c = ")"c And skiplevel = False Then
levelss(level) = 0
level -= 1
ElseIf c = """" Then
skiplevel = Not skiplevel
InAtt &= c
Else
InAtt &= c
End If
Next
End Sub
Use Visual Basic 2015 with .NET 2.0 and IMAPv4
My question: How can separate IMAP result to my, or other class?

Related

Combining two string and insert element VB.Net

I tried to build a combination algorithm between 2 strings, unfortunately it has some errors.
Dim strWordsA() As String = TextBox1.Text.Split(",")
Dim strWordsB() As String = TextBox2.Text.Split(",")
Dim str As String = TextBox1.Text
Dim arr As String() = TextBox1.Text.Split(","c)
For i As Integer = 0 To TextBox1.Text.Split(",").Length - 1
Dim index As Integer = str.IndexOf(strWordsA(i))
TextBox1.Text = str.Insert(index + 2, "," & strWordsB(i))
str = TextBox1.Text
Next
so if we have Textbox1.Text = 1,2,3,4,5,6,7,8,9 and Textbox2.Text = a,b,c,f,d,b,i,h, and so on... I need to display this in a 3rd textbox
Textbox3.Text = 1,a,2,b,3,c,4,f and so on
so do I combine these 2 strings?
the first element in the index displays it incorrectly, otherwise it seems to work ok.
Try this:
Private Function MergeStrings(s1 As String, s2 As String) As String
Dim strWordsA() As String = s1.Split(","c)
Dim strWordsB() As String = s2.Split(","c)
Dim i As Integer = 0
Dim OutputString As String = String.Empty
While i < strWordsA.Length OrElse i < strWordsB.Length
If i < strWordsA.Length Then OutputString &= "," & strWordsA(i)
If i < strWordsB.Length Then OutputString &= "," & strWordsB(i)
i += 1
End While
If Not OutputString = String.Empty Then Return OutputString.Substring(1)
Return OutputString
End Function
Usage:
Dim s As String = MergeStrings("1,2,3,4,5,6,7,8,9", "a,b,c,f,d,b,i,h")
You will need to add your own validation to allow for trailing commas or no commas etc but it should work with different length input strings
EDIT: amended as per Mary's comment

How to read protected registry key using vb.net

I am using the following code, however it doesn't seem to matter how I call the registry, I always get a fail or fail to default value on protected registry keys.
The interesting part, is that when running a VBScript, I have no issues accessing these keys.
Const Key As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
Public Overloads ReadOnly Property ProductName As String
Get
Return DirectCast(My.Computer.Registry.LocalMachine.GetValue(Key & "ProductName"), String)
' Dim WshShell As Object = CreateObject("WScript.Shell")
' Return WshShell.RegRead(Key & "ProductName")
End Get
End Property
Public ReadOnly Property DigitalID As String
Get
Return DirectCast(My.Computer.Registry.LocalMachine.GetValue(Key & "DigitalProductId"), String)
' Dim WshShell As Object = CreateObject("WScript.Shell")
' Return WshShell.RegRead(Key & "DigitalProductId")
End Get
End Property
Public ReadOnly Property PID As String
Get
Return DirectCast(My.Computer.Registry.LocalMachine.GetValue(Key & "ProductID"), String)
' Dim WshShell As Object = CreateObject("WScript.Shell")
' Return WshShell.RegRead(Key & "ProductID")
End Get
End Property
Public ReadOnly Property ProductKey As String
Get
Try
Dim pKey As Byte() = System.Text.Encoding.Default.GetBytes(DigitalID)
Dim Chars As String = "BCDFGHJKMPQRTVWXY2346789"
Dim i As Integer = 24
Dim isWin8 As Integer = (pKey(66) \ 6) And 1
Dim Cur As Integer = 0
Dim x As Integer = 14
Dim Last As Integer = 0
Dim keypart1 As String = ""
Dim insert As String = ""
Dim a As String = ""
Dim b As String = ""
Dim c As String = ""
Dim d As String = ""
Dim e As String = ""
Dim KeyOutput As String = ""
Const KeyOffset = 52
pKey(66) = (pKey(66) And &HF7) Or ((isWin8 And 2) * 4)
i = 24
Chars = "BCDFGHJKMPQRTVWXY2346789"
Do
Cur = 0
x = 14
Do
Cur = Cur * 256
Cur = pKey(x + KeyOffset) + Cur
pKey(x + KeyOffset) = (Cur \ 24)
Cur = Cur Mod 24
x = x - 1
Loop While x >= 0
i = i - 1
KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
Last = Cur
Loop While i >= 0
If (isWin8 = 1) Then
keypart1 = Mid(KeyOutput, 2, Last)
insert = "N"
KeyOutput = Replace(KeyOutput, keypart1, keypart1 & insert, 2, 1, 0)
If Last = 0 Then KeyOutput = insert & KeyOutput
End If
a = Mid(KeyOutput, 1, 5)
b = Mid(KeyOutput, 6, 5)
c = Mid(KeyOutput, 11, 5)
d = Mid(KeyOutput, 16, 5)
e = Mid(KeyOutput, 21, 5)
Return a & "-" & b & "-" & c & "-" & d & "-" & e
Catch er As Exception
Return er.Message
End Try
End Get
End Property
When testing with WshShell, I had 'Key' set to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ and also tried HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ . I also tried with or without a slash at the beginning, and end, and even hardcoding the entire key.
The problem I am getting is that all of these registry keys are returning nothing, and I can not find how to set the permission necessary to be able to open them, as I can see them using RegEdit, and I can also access them using VBScript.
Thanks in advance.
You need to open the subkey before you can read the value:
Const Key As String = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\"
Public Overloads ReadOnly Property ProductName As String
Get
Return My.Computer.Registry.LocalMachine.OpenSubKey(Key).GetValue("ProductName").ToString
End Get
End Property
EDIT: As noted above by Hans Passant, the build platform needs to be set to the "native" platform for the Windows installation (x64 or Any CPU for a 64-bits Windows), as the DigitalProductId (amongst others) value only exists in the "native" branch of the registry. The best bet is to leave it to AnyCPU to be sure to be able to read the value on any client PC.

Reading and writing from a csv file

Structure TownType
Dim Name As String
Dim County As String
Dim Population As Integer
Dim Area As Integer
End Structure
Sub Main()
Dim TownList As TownType
Dim FileName As String
Dim NumberOfRecords As Integer
FileName = "N:\2_7_towns(2).csv"
FileOpen(1, FileName, OpenMode.Random, , , Len(TownList))
NumberOfRecords = LOF(1) / Len(TownList)
Console.WriteLine(NumberOfRecords)
Console.ReadLine()
There are only 12 records in the file but this returns a value of 24 for number of records. How do I fix this?
Contents of csv file:
Town, County,Pop, Area
Berwick-upon-tweed, Nothumberland,12870,468
Bideford, devon,16262,430
Bognor Regis, West Sussex,62141,1635
Bridlington, East Yorkshire,33589,791
Bridport, Dorset,12977,425
Cleethorpes, Lincolnshire,31853,558
Colwyn bay, Conway,30269,953
Dover, Kent,34087,861
Falmouth, Cornwall,21635,543
Great Yarmouth, Norfolk,58032,1467
Hastings, East Sussex,85828,1998
This will read the contents into a collection and you can get the number of records from the collection.
Sub Main()
Dim FileName As String
Dim NumberOfRecords As Integer
FileName = "N:\2_7_towns(2).csv"
'read the lines into an array
Dim lines As String() = System.IO.File.ReadAllLines(FileName)
'read the array into a collection of town types
'this could also be done i a loop if you need better
'parsing or error handling
Dim TownList = From line In lines _
Let data = line.Split(",") _
Select New With {.Name = data(0), _
.County = data(1), _
.Population = data(2), _
.Area = data(3)}
NumberOfRecords = TownList.Count
Console.WriteLine(NumberOfRecords)
Console.ReadLine()
End Sub
Writing to the console would be accomplished with something like:
For Each town In TownList
Console.WriteLine(town.Name + "," + town.County)
Next
Many ways to do that
Test this:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
dim FileName as string = "N:\2_7_towns(2).csv"
Dim Str() As String = System.IO.File.ReadAllLines(filename)
'Str(0) contains : "Town, County,Pop, Area"
'Str(1) contains : "Berwick-upon-tweed, Nothumberland,12870,468"
'Str(2) contains : "Bideford, devon,16262,430"
' etc...
'Sample code for string searching :
Dim Lst As New List(Of String)
Lst.Add(Str(0))
Dim LookingFor As String = "th"
For Each Line As String In Str
If Line.Contains(LookingFor) Then Lst.Add(Line)
Next
Dim Result As String = ""
For Each St As String In Lst
Result &= St & Environment.NewLine
Next
MessageBox.Show(Result)
'Sample code creating a grid :
Dim Grid = New DataGridView
Me.Controls.Add(Grid)
Grid.ColumnCount = Str(0).Split(","c).GetUpperBound(0) + 1
Grid.RowCount = Lst.Count - 1
Grid.RowHeadersVisible = False
For r As Integer = 0 To Lst.Count - 1
If r = 0 Then
For i As Integer = 0 To Lst(r).Split(","c).GetUpperBound(0)
Grid.Columns(i).HeaderCell.Value = Lst(0).Split(","c)(i)
Next
Else
For i As Integer = 0 To Lst(r).Split(","c).GetUpperBound(0)
Grid(i, r - 1).Value = Lst(r).Split(","c)(i)
Next
End If
Next
Grid.AutoResizeColumns()
Grid.AutoSize = True
End Sub

VB.net get the first word after a specified string

I need to get the first word only, after a specified string like so (pseudo):
my_string = "Hello Mr. John, how are you today?"
my_search_string = "are"
result = "you"
I tried to do it by using the following approach but i get the rest of the string after my "key" string and not a single word.
Dim search_string As String = "key"
Dim x As Integer = InStr(Textbox1.text, search_string)
Dim word_after_key As String = Textbox1.text.Substring(x + search_string.Length - 1)
Try this:
Dim str = "Hello Mr. John, how are you today?"
Dim key = " are "
Dim i = str.IndexOf(key)
If i <> -1 Then
i += key.Length
Dim j = str.IndexOf(" ", i)
If j <> -1 Then
Dim result = str.Substring(i, j - i)
End If
End If
Or this perhaps:
Dim str = "Hello Mr. John, how are you today?"
Dim key = "are"
Dim words = str.Split(" "C)
Dim i = Array.IndexOf(words, key)
If i <> -1 AndAlso i <> words.Length - 1 Then
Dim result = words(i + 1)
End If
This works too.
Dim my_string as String = "Hello Mr. John, how are you today?"
Dim SearchString As String = "are"
Dim StartP As Integer = InStr(my_string, SearchString) + Len(SearchString) + 1
' to account for the space
If StartP > 0 Then
Dim EndP As Integer = InStr(StartP, my_string, " ")
MsgBox(Mid(my_string, StartP, EndP - StartP))
End If
Dim sa As String
Dim s As String
Dim sp() As String
sa = TextBox1.Text 'this text box contains value **Open Ended Schemes(Debt Scheme - Banking and PSU Fund)**
sp = sa.Split("(") 'Here u get the output as **Debt Scheme - Banking and PSU Fund)** which means content after the open bracket...
sp = sp(1).Split(")") 'Here u get the output as Debt Scheme - Banking and PSU Fund which means content till the close bracket...
s = Split(sp(0))(0) 'Here it will take the first word, which means u will get the output as **Debt**
s = Split(sp(0))(1) 'Change the index as per the word u want, here u get the output as **Scheme**

Name Proper Casing

Can you help me in having a proper casing,
I have this code...
Private Function NameCsing(ByVal sValue As String) As String
Dim toConvert As String() = sValue.Split(" ")
Dim lst As New List(Of String)
For i As Integer = 0 To toConvert.Length - 1
Dim converted As String = ""
If toConvert(i).Contains("~") Then
Dim toName As String() = toConvert(i).Split("~")
Dim sName As String = ""
For n As Integer = 0 To toName.Length - 1
Dim sconvert As String = ""
If n = 0 Then
sName = StrConv(toName(n), VbStrConv.ProperCase)
Else
sName += StrConv(toName(n), VbStrConv.ProperCase)
End If
Next
converted = sName
Else
converted = toConvert(i)
End If
lst.Add(converted)
Next
Dim ret As String = ""
For i As Integer = 0 To lst.Count - 1
If i = 0 Then
ret = lst(0)
Else
ret += " " + lst(i)
End If
Next
Return ret
End Function
My codes will just output like this "McDonalds" is you input "mc~donalds"
now my problem is eh I input "evalue", my output must be "eValue"
The only way to know how to treat a special string is to code it yourself from a list of rules:
Private Function NameCsing(ByVal sValue As String) As String
If sValue.Trim.ToLower = "evalue" Then Return "eValue"
'Then process any other special cases
End Function