Get filenames from opendialogform in a string vb.net - vb.net

How can i get all the filnames in array as a string?
Dim paths As Array
Dim npaths As String
od.ShowDialog()
paths = od.FileNames
npaths = ArrayToDelimited(paths, ",")

You're looking for String.Join:
npaths = String.Join(",", paths)

Related

Decode mail encoded-words =?utf-8?B?xxxx?=, =?utf-8?Q?xxxx?=

Is there a way to decode email subjects that are encoded? I know the dirty way of doing it is to get the string character between =?utf-8?B? xxx ?= and decoding that. But I have a program where I can get encoded strings like
=?utf-8?Bxxxx?= =?UTF-8?B?xxxx?= ...
Right now I'm doing something like this
If codedString.ToUpper().StartsWith("=?UTF-8?B?") Then
Dim temp As String = codedString.SubString(10)
Dim data = Convert.FromBase64String(temp)
Dim decodedString = ASCIIEncoding.ASCII.GetString(data)
'do something with decodedString
End If
But this doesn't work when the same string has multiple =?utf-8?B? encode like above. Also I can get strings with =?utf-8?Q encoding and =?windows-1252. Is there a way to tackle all of these encoding? I'm using Visual Studios 2017
I've never had trouble using this function to decode a email field value:
It finds matching utf-8 strings for types B or Q, and if type B, runs FromBase64String.
I'm sure you can manipulate for windows-1252.
Private Function DecodeEmailField(byVal strString as String) as String
DecodeEmailField = strString.toString()
Dim strMatch
Dim arrEncodeTypes = New String() {"B","Q"}
Dim strEncodeType as String
For Each strEncodeType in arrEncodeTypes
Dim objRegexB as RegEx = new RegEx("(?:\=\?utf\-8\?" & strEncodeType & "\?)(?:.+?)(?:\?=\s)", _
RegexOptions.Multiline or RegexOptions.IgnoreCase)
if (objRegexB.IsMatch(DecodeEmailField)) then
Dim thisMatch as Match = objRegexB.Match(DecodeEmailField)
For Each strMatch in thisMatch.Groups
Dim strMatchHold as String = strMatch.toString().Substring(("=?utf-8?" & strEncodeType & "?").length)
strMatchHold = strMatchHold.SubString(0,(strMatchHold.Length)-("?= ".Length))
If strEncodeType = "B" Then
Dim data() As Byte = System.Convert.FromBase64String(strMatchHold)
strMatchHold = System.Text.UTF8Encoding.UTF8.GetString(data)
End If
DecodeEmailField = Replace(DecodeEmailField,strMatch.toString(),strMatchHold)
Next
End If
Next
End Function

VB.NET: GetFiles() in Specific Directory

Currently the line of code looks like this:
Dim files() As String = System.IO.Directory.GetFiles(path, filehead & ".*.*.fsi")
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
Dim file As String = ""
For Each file In files
args = Split(file, ".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
The problem is, sometimes, in my case, the path looks something like:
C:\Users\c.brummett\Downloads
and the split causes a split in the username. How can I avoid this problem but still split by periods? I'm sorry I don't how to make this more relatable.
My idea was to use a DirectoryInfo and do something like:
Dim di As DirectoryInfo
di = New DirectoryInfo(path)
Dim files() As String = di.GetFiles(filehead & ".*.*.fsi")
Edit: The problem with this second bit of code, is that it returns the error
Value of type '1-dimensional array of System.IO.FileInfo' cannot be converted to '1-> dimensional array of String' because 'System.IO.FileInfo' is not derived from 'String'.`
You can forget about getting an array of file names (you don't need that anyway) and iterate on the array of FileInfo:
Dim files() As FileInfo = New DirectoryInfo(path).GetFiles(filehead & ".*.*.fsi")
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
For Each file As FileInfo In files
args = Split(file.Name, ".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
Note AllDirectories and change in the line doing the splitting. I didn't look at your array structure stuff.
Dim files() As String = System.IO.Directory.GetFiles("C:\temp", "*.doc", IO.SearchOption.AllDirectories)
Dim seqfsi() As Integer
ReDim seqfsi(files.GetUpperBound(0))
Dim args() As String
Dim file As String = ""
For Each file In files
args = file.Substring(file.LastIndexOf("\") + 1).Split(".")
If args.Length = 4 Then
seqfsi(System.Array.IndexOf(files, file)) = CInt(args(args.GetUpperBound(0) - 1))
End If
Next file

Collection to delimited string

when I do
String.Join(";", lst.Items)
I get a string of object descriptors instead of item of values.
But when I iterate the collection, I end up with a delimiter at the front or back and need to a Substring call afterwards.
Dim res As String = "" 'or use stringbuilder
For Each s As String In lst.Items
s &= ";" & s
Next
res = res.Substring(1)
This applies to other cases as well where you want to turn a shared property within a collection into a delimited string. Is there a nice way to do this?
Can I do this with LINQ and would it be faster?
You'll have to convert the items to strings then:
String.Join(";", lst.Items.Select(Function(item) item.ToString()));
How about
Dim res As String = String.Join(";", lst.Items.OfType(Of String))
This does work:
Dim col As New Collection
col.Add("One")
col.Add("Two")
col.Add("Three")
Dim res = String.Join(";", col.OfType(Of String))
See also this question

How i can get selected parts from string?

How i get string like
'EJ0004','EK0001','EA0001'
from string like
{Emaster.Emp_Code}='EJ0004' OR {Emaster.Emp_Code}='EK0001' OR {Emaster.Emp_Code}='EA0001'
in VB.NET?
You can use a regular expression.
Example:
Sub Main
Dim s = "{Emaster.Emp_Code}='EJ0004' OR {Emaster.Emp_Code}='EK0001' OR {Emaster.Emp_Code}='EA0001'"
Dim pattern = "('\w*')"
Dim matches = Regex.Matches(s, pattern)
Dim values = matches.OfType(Of Match).Select(Function(m) m.Value)
For Each v in values
Console.WriteLine(v)
Next
Console.WriteLine(String.Join(",", values))
End Sub
Output:
'EJ0004'
'EK0001'
'EA0001'
'EJ0004','EK0001','EA0001'
there are many ways you could do this, here i offer one simple way:
dim yourString as string = 'This is the variable which holds your initial string
dim newString as string = yourString.replace("{Emaster.Emp_Code}=", "").replace(" OR ",",")
Now newString will hold 'EA0001' Or whatever.
If you want it without the '' then do
dim newString as string = yourString.replace("{Emaster.Emp_Code}=", "").replace("'","").replace(" OR ",",")

How to traverse a concatenated string in VB.NET?

I have a concatenated string like
str = 1010,5050,6079
I want to separate the string. In JavaScript, I can do
string = Srt.split(","):
and get the result
string[2]=5050
Is there a way to do this in VB.NET?
It works the same in VB.NET
Dim input As String = "1010,5050,6079"
Dim result As String() = input.Split(",")
result(0) //contains 1010
result(1) //contains 5050
result(2) //contains 6079
There is no diference. Try with the same code in VB.NET:
Dim shoppingList As String = "Milk,Eggs,Bread"
Dim shoppingItem(3) As String
shoppingItem = shoppingList.Split(",")